# Register\_hook and find the maximum gradient of non leaf variable

**URL:** <https://discuss.pytorch.org/t/register-hook-and-find-the-maximum-gradient-of-non-leaf-variable/19565>\
**Category:** autograd\
**Created:** [June 12, 2018, 1:54pm UTC](https://discuss.pytorch.org/t/register-hook-and-find-the-maximum-gradient-of-non-leaf-variable/19565 "2018-06-12T13:54:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blade6570](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/blade6570/32/3508_2.png) [@Blade6570](https://discuss.pytorch.org/u/Blade6570)\
**Post date:** [June 12, 2018, 1:54pm UTC](https://discuss.pytorch.org/t/register-hook-and-find-the-maximum-gradient-of-non-leaf-variable/19565/1 "2018-06-12T13:54:44Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Castiglione](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/giuseppe_castiglione/32/5189_2.png) [@Giuseppe\_Castiglione](https://discuss.pytorch.org/u/Giuseppe_Castiglione)\
**Post date:** [June 12, 2018, 6:25pm UTC](https://discuss.pytorch.org/t/register-hook-and-find-the-maximum-gradient-of-non-leaf-variable/19565/2 "2018-06-12T18:25:56Z")

</div>

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

> [@Why cant I see .grad of an intermediate variable?](https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94):
>
> Hi! I am loving this framework… Since Im a noob, I am probably not getting something, but I am wondering why I cant get the gradient of an intermediate variable with .grad? Here is an example of what I mean: xx = Variable(torch.randn(1,1), requires\_grad = True) yy = 3\*xx zz = yy\*\*2 zz.backward() xx.grad # This is ok yy.grad # This gives 0! zz.grad # This should give 1! So I get the correct result for xx.grad, but why does yy.grad show 0, as does zz.grad? How can I get the yy.grad value in t…

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:

> [@Why cant I see .grad of an intermediate variable?](https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94/7):
>
> You can think of a function that also keeps some additional variables from the outer scope. For example in here hook is a closure that remembers a name given to the outer function: grads = {} def save\_grad(name): def hook(grad): grads[name] = grad return hook x = Variable(torch.randn(1,1), requires\_grad=True) y = 3\*x z = y\*\*2 # In here, save\_grad('y') returns a hook (a function) that keeps 'y' as name y.register\_hook(save\_grad('y')) z.register\_hook(save\_grad('z')) z.backward()…

---

<div class="post-metadata">

**Author:** ![Blade6570](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/blade6570/32/3508_2.png) [@Blade6570](https://discuss.pytorch.org/u/Blade6570)\
**Post date:** [June 13, 2018, 7:42am UTC](https://discuss.pytorch.org/t/register-hook-and-find-the-maximum-gradient-of-non-leaf-variable/19565/3 "2018-06-13T07:42:35Z")

</div>

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