Register_hook and find the maximum gradient of non leaf variable


(Soumya Ranjan Tripathy) #1

I have put the register_hook() for a non_leaf variable and printing the gradient. How to access each element in the gradient tensor.

The syntax I am using is as follows:

y.register_hook(print)


(Giuseppe Castiglione) #2

You’ll find the gradient by looking at y.grad. Usually this isn’t saved though, so take a look here:

If you want to specifically use a hook, then you can register a closure as a hook and that will save the gradient, As was done here:


(Soumya Ranjan Tripathy) #3

Hi, Thanks for the response. I got it now.