You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I read your code, and it seems that you forgot to self.zero_grad() before self.criterion(self.model(samples), targe). Is that a bug? Or I just miss something?
I'm sorry for a late response.
No, actually, there is no bug in the code.
Typically:
zero_grad
backward
step
My implementation:
backward
step
zero_grad
In general, this is the same, but in my implementation zero_grad will be called on the next step.
By "typical" implementation zero_grad will be redundant in the first step (and then it will work like mine).
So, my point is that both these approaches are cycled :)
Sorry for the late reply. :)
Yes, I understand that putting the zero_grad() in the end is also cycled.
I realized that I didn't clear my question.
The normal SAM procedure is:
1st backward
compute e_w
zero_grad
2nd backward
update the original params
zero_grad
So my point is that there should be a zero_grad() before the 2nd backward.
But there is no self.zero_grad() before self.criterion(self.model(samples), targe).backward() in line 77.
I followed pseudo-code in LookSAM paper and I was thinking that we need to accumulate gradients under that condition. Therefore, I avoided step to zero gradients here.
Now, I rechecked SAM's code and they do zero gradients both times.
Apparently, you're right and I have a mistake here.
Have you ever tried this LookSAM solution with "under condition" zero_grad step, as you mentioned (perhaps you added it manually), or without it, as in my code? Does it work for you?
I use your code in my project (thank you for the code, it is a big help!), and I find that with or without the zero_grad() step before the 2nd backward() will both bring benefits in different situations.
e.g., if you want to include the original loss as well ($L_{origin}+L_{perturbed}$) to compute the final gradient at every $k$ steps, you can avoid the zero_grad(), that will improve the performance in some cases, depending on your model design.
@draym28 How did you resolve an issue of "gv" key absence here while putting zero gradient after first backward?
Due to zero_grad after first backward, all the gradients will be None during next loop...
And we can't avoid condition with p.grad is None, because SAM has the same structure here, which makes sense.
I don't deny that I could have made a mistake in the code :)
@draym28 How did you resolve an issue of "gv" key absence here while putting zero gradient after first backward?
Due to zero_grad after first backward, all the gradients will be None during next loop... And as I understood it's okay, because SAM has the same line here. I don't deny that I could have made a mistake in the code :)
For LookSAM, the middle zero_grad() should only be conducted when if not k % self.k before 2n backward. So when k % self.k, the "gv" key still exist (not conduct zero_grad()), there will be no issue here.
Activity
rollovd commentedon Jul 19, 2023
Hey, man!
I'm sorry for a late response.
No, actually, there is no bug in the code.
Typically:
My implementation:
In general, this is the same, but in my implementation zero_grad will be called on the next step.
By "typical" implementation zero_grad will be redundant in the first step (and then it will work like mine).
So, my point is that both these approaches are cycled :)
draym28 commentedon Apr 13, 2024
Sorry for the late reply. :)
Yes, I understand that putting the
zero_grad()in the end is also cycled.I realized that I didn't clear my question.
The normal SAM procedure is:
So my point is that there should be a
zero_grad()before the 2nd backward.But there is no
self.zero_grad()beforeself.criterion(self.model(samples), targe).backward()in line 77.rollovd commentedon Apr 15, 2024
Ciao @draym28!
Yeah, it makes sense.
I followed pseudo-code in LookSAM paper and I was thinking that we need to accumulate gradients under that condition. Therefore, I avoided step to zero gradients here.
Now, I rechecked SAM's code and they do zero gradients both times.
Apparently, you're right and I have a mistake here.
Have you ever tried this LookSAM solution with "under condition"
zero_gradstep, as you mentioned (perhaps you added it manually), or without it, as in my code? Does it work for you?Thank you for being in touch haha!
draym28 commentedon Apr 15, 2024
I use your code in my project (thank you for the code, it is a big help!), and I find that with or without the
zero_grad()step before the 2nd backward() will both bring benefits in different situations.e.g., if you want to include the original loss as well ($L_{origin}+L_{perturbed}$ ) to compute the final gradient at every $k$ steps, you can avoid the
zero_grad(), that will improve the performance in some cases, depending on your model design.rollovd commentedon Apr 15, 2024
Glad to hear it helps you! :)
Maybe in this case that's reasonable to add a boolean option "zero gradient before 2nd backward" :)
I'll do it right now.
Thank you for your contribution!
I hope project goes well hehe
rollovd commentedon Apr 15, 2024
@draym28 How did you resolve an issue of "gv" key absence here while putting zero gradient after first backward?
Due to zero_grad after first backward, all the gradients will be None during next loop...
And we can't avoid condition with
p.grad is None, because SAM has the same structure here, which makes sense.I don't deny that I could have made a mistake in the code :)
draym28 commentedon Apr 15, 2024
For LookSAM, the middle
zero_grad()should only be conducted whenif not k % self.kbefore 2n backward. So whenk % self.k, the "gv" key still exist (not conductzero_grad()), there will be no issue here.rollovd commentedon Apr 15, 2024
@draym28 Yeah, definitely. I made a typo :)
Thank you!
rollovd commentedon Apr 15, 2024
@draym28 once again thank you for your contribution.
I updated code. Commit can be found here.
I guess this issue has resolved. Therefore, I can close this thread.