How to save grad values after every batch?

(new to pytorch)
Can anyone tell me how to save the gradients after every batch (and epoch) and also to load from the saved file?
The model.dict() only save the weights and other parameters is what I could understand. But my requirement is to save the weight.grad

Try out:

grad_dic = {x[0]:x[1].data.grad for x in model.named_parameters()}

and save grad_dic somewhere.

torch.save(grad_dic,<path>)
...
grad_dic = torch.load(<path>)
1 Like

Thank you for the reply! I modified a bit and now i am able to save the gradient values!