The code looks correct. Note that lotal_g_loss.backward() would also calculate the gradients for D (if you haven’t set all requires_grad attributes to False), so you would need to call D.zero_grad() before updating it.