Solution questions regarding RunTimeError for Pythorch in-place operations

Asked 2 years ago, Updated 2 years ago, 83 views

The following code will cause an error. If you don't mind, I'd appreciate it if you could help me.
I'm not used to asking questions, so I may not be able to do so, but I appreciate your cooperation.

applicable code (where indicated by torch.autograd.set_detect_anomaly(True))

class UnNormfunc(nn.Module):
    def__init__(self):
        super(UnNormfunc,self).__init__()

    def forward (self, x):
        tempx=x.clone()
        for i in range (3):
            tempx[:, i, :, :] = tempx[:, i, :, :] * std[i] + mean[i]
        return tempx

error message

sys:1:RuntimeWarning:Traceback of forward call that caused the error:
  File "train.py", line 149, in<module>
    B_hat, B_hat_d1, B_hat_d2, B_hat_d3, B_hat_d4 = generator (torch.cat ([Norm(gamma_RF), Rmap, Norm(gamma_RF*Rmap), 1))
  File"~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line489, in__call__
    result=self.forward (*input,**kwargs)
  File "~/hoge/models.py", line 133, in forward
    D4 = self.unNorm (self.final4(x4_0))
  File"~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line489, in__call__
    result=self.forward (*input,**kwargs)
  File "~/hoge/util.py", line 30, in forward
    tempx[:, i, :, :] = tempx[:, i, :, :] * std[i] + mean[i]

Traceback (most recent call last):
  File "train.py", line 190, in<module>
    loss_G.backward()
  File "~/anaconda3/envs/pytorch/lib/python 3.6/site-packages/torch/tensor.py", line 102, inbackward
    torch.autograd.backward(self,gradient,retain_graph,create_graph)
  File "~/anaconda3/envs/pytorch/lib/python 3.6/site-packages/torch/autograd/_init__.py", line 90, inbackward
    allow_unreachable=True)#allow_unreachable flag
RuntimeError: one of the variables needed for gradient computer has been modified by an replace operation

It seems to me that they are avoiding in-place operations, but they are not. Please tell me why.
Reference URL http://www.yongfengli.tk/2018/04/13/inplace-operation-in-pytorch.html

python pytorch

2022-09-30 16:53

1 Answers

Sorry for the trouble.I have solved it.The reason seems to be the difference in version. I set the version of pytorch to 1.2.0, and it worked well.
Thank you very much for correcting my question carefully.


2022-09-30 16:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.