torch-ac icon indicating copy to clipboard operation
torch-ac copied to clipboard

Allow frozen parameters with requires_grad=False

Open andrewli77 opened this issue 5 years ago • 3 comments

Hi Lucas, thanks for open-sourcing your code for this project. I think it would be nice to be able to manually freeze some network parameters (I was doing this in the context of transfer learning). Currently, the gradient norm is computed over parameters with requires_grad=False as well and p.grad is None.

andrewli77 avatar Oct 15 '20 22:10 andrewli77

Hi @andrewli77, Thank you for your PR! How would you define the value of requires_grad?

lcswillems avatar Nov 01 '20 18:11 lcswillems

Hi Lucas, sorry for the late reply. requires_grad is an attribute for every parameter of nn.Module which defaults to true. My code sets it by subclassing ACModel and running

for param in self.rnn.parameters():
         param.requires_grad = False

andrewli77 avatar Nov 27 '20 09:11 andrewli77

Hi @andrewli77,

I completely forgot about your PR. I am wondering if your modifications change something.

Your first modification:

grad_norm = sum(p.grad.data.norm(2).item() ** 2 for p in self.acmodel.parameters() if p.requires_grad) ** 0.5

If p.requires_grad is false, then p.grad.data would be an array of zeros, right? In this case, removing if p.requires_grad would not change anything right?

Your second modification:

torch.nn.utils.clip_grad_norm_([p for p in self.acmodel.parameters() if p.requires_grad], self.max_grad_norm)

If p.requires_grad is false, then p.grad.data would be an array of zeros and therefore clipping would change nothing.

lcswillems avatar Aug 17 '21 20:08 lcswillems