# Why cant I see .grad of an intermediate variable?

**URL:** <https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94>\
**Category:** Uncategorized\
**Created:** [January 21, 2017, 1:34am UTC](https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94 "2017-01-21T01:34:26Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [January 21, 2017, 4:10am UTC](https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94/2 "2017-01-21T04:10:12Z")

</div>

Hi Kalamaya,

By default, gradients are only retained for leaf variables. non-leaf variables’ gradients are not retained to be inspected later. This was done by design, to save memory.

However, you can inspect and extract the gradients of the intermediate variables via `hooks`.  
You can register a function on a `Variable` that will be called when the backward of the variable is being processed.

More documentation on hooks is here: [http://pytorch.org/docs/autograd.html#torch.autograd.Variable.register\_hook](http://pytorch.org/docs/autograd.html#torch.autograd.Variable.register_hook)

Here’s an example of calling the `print` function on the variable `yy` to print out it’s gradient (you can also define your own function that copies the gradient over else-where or modifies the gradient, for example.

```python
from __future__ import print_function
from torch.autograd import Variable
import torch

xx = Variable(torch.randn(1,1), requires_grad = True)
yy = 3*xx
zz = yy**2

yy.register_hook(print)
zz.backward()

```

Output:

```bash
Variable containing:
-3.2480
[torch.FloatTensor of size 1x1]

```

---

_[View the full topic](https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94)._
