# Model partial derivatives into loss function

**URL:** <https://discuss.pytorch.org/t/model-partial-derivatives-into-loss-function/134454>\
**Category:** autograd\
**Created:** [October 17, 2021, 7:02pm UTC](https://discuss.pytorch.org/t/model-partial-derivatives-into-loss-function/134454 "2021-10-17T19:02:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Repetto](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/marco_repetto/32/42346_2.png) [@Marco\_Repetto](https://discuss.pytorch.org/u/Marco_Repetto)\
**Post date:** [October 17, 2021, 7:02pm UTC](https://discuss.pytorch.org/t/model-partial-derivatives-into-loss-function/134454/1 "2021-10-17T19:02:41Z")

</div>

I want to force a monotonic relationship between a set of features and the model’s outcome, and I would like to do it by controlling the model’s partial derivatives into the loss function, like a regularization term.

I tried the solution proposed [here](https://discuss.pytorch.org/t/how-to-include-gradient-of-nn-wrt-the-input-in-the-loss-function/103758), but I got an error:

```auto
one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [2, 1]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

```

Is there a way to include the model’s partial derivatives into the loss?

---

<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:** [October 18, 2021, 1:17pm UTC](https://discuss.pytorch.org/t/model-partial-derivatives-into-loss-function/134454/2 "2021-10-18T13:17:33Z")

</div>

Hi,

This is the same error as the one from the post you linked. Does the answer there help you?

---

<div class="post-metadata">

**Author:** ![Marco\_Repetto](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/marco_repetto/32/42346_2.png) [@Marco\_Repetto](https://discuss.pytorch.org/u/Marco_Repetto)\
**Post date:** [October 18, 2021, 2:31pm UTC](https://discuss.pytorch.org/t/model-partial-derivatives-into-loss-function/134454/3 "2021-10-18T14:31:44Z")

</div>

Hi Alban,

I tried the solution proposed (simply copy pasting) but is not working.
