Skip to content

Commit b65fcc6

Browse files
authored
Update dcgan_faces_tutorial.py
Previous comment might have been slightly misleading. A bit easier to understand, if made explicit, that gradients of errD_real and errD_fake w.r.t. parameters of netD get added up/accumulated because of the successive backward calls without a zero_grad() in between.
1 parent 16e8be2 commit b65fcc6

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

beginner_source/dcgan_faces_tutorial.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,12 @@ def forward(self, input):
611611
# Calculate D's loss on the all-fake batch
612612
errD_fake = criterion(output, label)
613613
# Calculate the gradients for this batch
614+
# Without a zero_grad() call, this accumulates (sums)
615+
# the gradients from this call with the previously computed
616+
# gradients from errD_real
614617
errD_fake.backward()
615618
D_G_z1 = output.mean().item()
616-
# Add the gradients from the all-real and all-fake batches
619+
# Compute error of D as sum over the fake and the real batches
617620
errD = errD_real + errD_fake
618621
# Update D
619622
optimizerD.step()

0 commit comments

Comments
 (0)