Skip to content

Commit

Permalink
Update dcgan_faces_tutorial.py (pytorch#816)
Browse files Browse the repository at this point in the history
* 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.

* Update dcgan_faces_tutorial.py

Co-authored-by: Brian Johnson <[email protected]>
Co-authored-by: holly1238 <[email protected]>
  • Loading branch information
3 people authored Apr 16, 2021
1 parent ffcaefd commit f35c04a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions beginner_source/dcgan_faces_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,10 @@ def forward(self, input):
output = netD(fake.detach()).view(-1)
# Calculate D's loss on the all-fake batch
errD_fake = criterion(output, label)
# Calculate the gradients for this batch
# Calculate the gradients for this batch, accumulated (summed) with previous gradients
errD_fake.backward()
D_G_z1 = output.mean().item()
# Add the gradients from the all-real and all-fake batches
# Compute error of D as sum over the fake and the real batches
errD = errD_real + errD_fake
# Update D
optimizerD.step()
Expand Down

0 comments on commit f35c04a

Please sign in to comment.