# How can I do secound gradient in pytorch?

**URL:** <https://discuss.pytorch.org/t/how-can-i-do-secound-gradient-in-pytorch/19486>\
**Category:** autograd\
**Created:** [June 10, 2018, 5:24pm UTC](https://discuss.pytorch.org/t/how-can-i-do-secound-gradient-in-pytorch/19486 "2018-06-10T17:24:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![isalirezag](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/fbc32d/32.png) [@isalirezag](https://discuss.pytorch.org/u/isalirezag)\
**Post date:** [June 10, 2018, 5:24pm UTC](https://discuss.pytorch.org/t/how-can-i-do-secound-gradient-in-pytorch/19486/1 "2018-06-10T17:24:31Z")

</div>

is there a way to compute the second, third gradient in pytorch?  
can I do `( loss.backward()).backward()`

---

<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:** [June 11, 2018, 2:30pm UTC](https://discuss.pytorch.org/t/how-can-i-do-secound-gradient-in-pytorch/19486/2 "2018-06-11T14:30:13Z")

</div>

Yes if you give the right arguments to these functions: see the `create_graph` argument in the [doc](https://pytorch.org/docs/stable/autograd.html#module-torch.autograd).  
Also remember that `.backward()` will accumulate gradients in all parameters (it does not return them), so you will need to get them from your model.  
You can use `autograd.grad` if you want gradients for specific inputs and have them being returned to you directly.
