PytorchでNNを構築する話ですけれども、
modelX は model1とmodel2という二つのサブmodel(NNブロック)を接続して構成されています。
model1はNNの入力層に繋がって、model2はNNの出力層に繋がっています。
訓練ロジック以下の通りで、トラブルが発生しました。
Python
1model1.zero_grad() 2batch_loss_model1.backward(retain_graph=True) 3optimizer_model1.step() # ① 4modelX.zero_grad() 5batch_loss_model2.backward() # ② 👈①をやめない限り、エラー発生(メッセージ内容は↓)
/usr/local/lib/python3.7/dist-packages/torch/_tensor.py in backward(self, gradient, retain_graph, create_graph, inputs)
361 create_graph=create_graph,
362 inputs=inputs)
--> 363 torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
364
365 def register_hook(self, hook):
/usr/local/lib/python3.7/dist-packages/torch/autograd/init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
173 Variable.execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
174 tensors, grad_tensors, retain_graph, create_graph, inputs,
--> 175 allow_unreachable=True, accumulate_grad=True) # Calls into the C++ engine to run the backward pass
176
177 def grad(
RuntimeError:** one of the variables needed for gradient computation has been modified by an inplace operation:**
[torch.FloatTensor [200, 6]], which is output 0 of AsStridedBackward0, is at version 2; expected version 1 instead.
Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
自分の水準では理解も解決もできないので、何方か原因を教えてくださいませんか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/22 06:43
2022/06/22 07:03 編集
2022/06/22 07:07
2022/06/22 07:08
2022/06/22 07:11 編集
2022/06/22 08:52