私は自分のネットワークを実行したとき。私はエラーを得た、勾配の計算に必要な変数の一つは、インプレース操作によって変更されました

feiyuanyu:

これは私のコードの一部ですtrain.py

output_c1[output_c1 > 0.5] = 1.
output_c1[output_c1 < 0.5] = 0.

dice = DiceLoss()
loss = dice(output_c1,labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()

エラーは次のとおりです。

Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-78553e2886de>", line 1, in <module>
    runfile('F:/experiment_code/U-net/train.py', wdir='F:/experiment_code/U-net')
  File "D:\pycharm\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\pycharm\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "F:/experiment_code/U-net/train.py", line 99, in <module>
    loss.backward()
  File "D:\Anaconda3\lib\site-packages\torch\tensor.py", line 107, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "D:\Anaconda3\lib\site-packages\torch\autograd\__init__.py", line 93, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 2, 224, 224]], which is output 0 of SigmoidBackward, is at version 2; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

私はこのコードの2行をコメントする場合:

output_c1[output_c1 > 0.5] = 1.
output_c1[output_c1 < 0.5] = 0.

それが実行することができます。私は、エラーがここから来ていると思いますが、私はそれを解決する方法がわかりません。

Lescurel:

output_c1[output_c1 > 0.5] = xそれは代わりに、直接修正したコピーを返すのテンソルを変更することを意味し、インプレース演算です。したがって、それは不可能勾配の計算を行います。

あなたのユースケースを解決するには、関数を使用することができtorch.round、あなたの出力を2値化します。

binary_output_c1 = torch.round(output_c1)
# and don't forget to change the computation of your loss function!
dice = DiceLoss()
loss = dice(binary_output_c1,labels)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=351447&siteId=1