# \[Solved\]\[Pytorch1.5\] RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

**URL:** <https://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256>\
**Category:** autograd\
**Created:** [July 23, 2020, 9:52am UTC](https://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256 "2020-07-23T09:52:12Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![111367](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111367/32/27031_2.png) [@111367](https://discuss.pytorch.org/u/111367)\
**Post date:** [July 24, 2020, 7:47am UTC](https://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256/4 "2020-07-24T07:47:22Z")

</div>

Thanks albanD~  
I find a way that it won’t pop up the issue by rearranging the code like the following bellow:

```auto
value_loss = 0.5 * mse_loss(imaginated_values, lambda_target_values.detach())
value_optimizer.zero_grad()

action_loss = -1*(lambda_target_values.mean())
action_optimizer.zero_grad()

value_loss.backward(retain_graph=True)
action_loss.backward()

clip_grad_norm_(value_model.parameters(), clip_grad_norm)
clip_grad_norm_(action_model.parameters(), clip_grad_norm)

value_optimizer.step()
action_optimizer.step()

```

It won’t get the issue of missing weights again. Thanks~

---

_[View the full topic](https://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256)._
