What's the easiest way to clean gradients before saving a net?

Hi, saving a nn.Module seems obscure, but by digging the code I can tell it should look like:

torch.save(net.state_dict(), 'net_name.pth')

right?

But how to clean/clear all the .grad fields of each Parameter (which is derived from Variable) before the saving, in case I just want a minimum net for inference next time? A user may want something like the shorthand net:clearState() in Torch 7.

Hi
I think the method you are using of considering

torch.save(net.state_dict(), 'net_name.pth')

is the current advised way of doing it, and it will only save the weights and not the gradients.
Indeed, since https://github.com/pytorch/pytorch/pull/451 the state_dict function only returns a tensor, and not the corresponding Variable.

Ah… yes it is, thanks @fmassa :slight_smile: