Register_hook and find the maximum gradient of non leaf variable

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)

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:

1 Like

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