以下のコードでエラーが起きてしまいます.もしよろしければ,ご教授のほどよろしくお願いいたします.
質問し慣れていないので,至らないところもあるかもしれませんが,何卒よろしくお願いいたします.
該当コード(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
エラーメッセージ
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", line 489, 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", line 489, 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/python3.6/site-packages/torch/tensor.py", line 102, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/autograd/__init__.py", line 90, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
私にはインプレース操作を回避しているように見えるのですが,回避できていないようです.この理由をご教授お願いします
参考URL http://www.yongfengli.tk/2018/04/13/inplace-operation-in-pytorch.html