# Special sum of loss functions for training

**URL:** <https://discuss.pytorch.org/t/special-sum-of-loss-functions-for-training/208534>\
**Category:** Uncategorized\
**Created:** [August 23, 2024, 3:37pm UTC](https://discuss.pytorch.org/t/special-sum-of-loss-functions-for-training/208534 "2024-08-23T15:37:06Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lucasegidio1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lucasegidio1/32/22210_2.png) [@lucasegidio1](https://discuss.pytorch.org/u/lucasegidio1)\
**Post date:** [August 24, 2024, 6:58pm UTC](https://discuss.pytorch.org/t/special-sum-of-loss-functions-for-training/208534/2 "2024-08-24T18:58:24Z")

</div>

When you perturb your parameters with `param.add_(perturbation)`, you are doing an in-place modification of the leaf tensor for which you want to calculate the gradient. The backward call need these original saved tensors to compute the gradient and modifying them in-place makes the saved values “invalid” for the backpropagation as [said here](https://discuss.pytorch.org/t/clone-and-detach-in-v0-4-0/16861/32)

As an alternative you can either create copies of your model (for each perturbation) or accept the perturbation vector as an input of the forward call and use it within there instead, without having to modify the parameters. Maybe defining a suitable [parametrization](https://pytorch.org/tutorials/intermediate/parametrizations.html) is also a cleaner solution, but not really necessary…

---

_[View the full topic](https://discuss.pytorch.org/t/special-sum-of-loss-functions-for-training/208534)._
