optimizer closure, possible error in the docs: for discriminator in GAN example #10893
Replies: 2 comments 3 replies
-
Yes you are right it is not required to call the closure if you just want to skip the whole update for that optimizer_idx. However, the example code is implementing in addition also gradient accumulation (2 batches), which we can see from this part: if (batch_idx + 1) % 2 == 0:
# this condition is satisfied every other batch_idx, here we do the optimizer.step
else:
# while accumulating, don't do optimizer.step(), only accumulate gradients I agree, it can be confusing, but as best as I can tell, the example is correct. If you see the need, we can try to make the comments a bit better. |
Beta Was this translation helpful? Give feedback.
-
Thanks @awaelchli for your reply. Indeed making the comments better would help. But there's another issue as well. When I don't want to accumulate gradients, i.e. I just want to put |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Here in this page, in the following example
what is the reason for calling optimizer_closure() in the else? i.e. if we want to update the discriminator every 2 batches, shouldn’t that else just do nothing?
Yet in the example in the doc, we're going over training step and computing gradients (backward) although we're not gonna use them with optimizer.step()
That's my confusion. Why waste time and compute over a training_step and backward that are not gonna be used. (Because the next call to optimizer_closure would zero the gradients, so there doesn’t seem to be any point in computing them in the first place.) Or are they used and I've misunderstood?
The interesting thing is that if you omit that optimizer_closure in the else, it’ll raise an error asking something like, didn’t you forget to call optimizer_closure? And I’m like I don’t wanna call it! But it seems it’s kinda forced. Could you please help figure out what's going on there?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions