# Cannot calculate second order gradients even though \`create\_graph=True\`

**URL:** <https://discuss.pytorch.org/t/cannot-calculate-second-order-gradients-even-though-create-graph-true/78711>\
**Category:** Uncategorized\
**Created:** [April 27, 2020, 7:29pm UTC](https://discuss.pytorch.org/t/cannot-calculate-second-order-gradients-even-though-create-graph-true/78711 "2020-04-27T19:29:01Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [April 30, 2020, 3:24pm UTC](https://discuss.pytorch.org/t/cannot-calculate-second-order-gradients-even-though-create-graph-true/78711/12 "2020-04-30T15:24:09Z")

</div>

I think the problem in your code is:

```auto
for p in m.parameters():
  p = p - LR * p.grad

```

This does not modify `p` inplace ! It just assigns the result to a variable name p that you override just after. So you could remove these lines and your code would run the same.  
This is why you don’t see the link.

Another issue you’re gonna face is that `nn.Parameter()` are built explicitly to be leaf Tensors (with no history) and so you won’t be able to change them with a Tensor you try to backprop through.

You can check the [higher](https://github.com/facebookresearch/higher) package to do this properly (they solve this problem of nn.Parameter for you). Otherwise, if you want to do it manually, you can find some info in this thread: [How does one have the parameters of a model NOT BE LEAFS?](https://discuss.pytorch.org/t/how-does-one-have-the-parameters-of-a-model-not-be-leafs/70076/9)

---

_[View the full topic](https://discuss.pytorch.org/t/cannot-calculate-second-order-gradients-even-though-create-graph-true/78711)._
