【报错】:RuntimeError: もう一度グラフを逆方向に実行しようとしましたが、保存された中間リ

エラーの質問:

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling .backward() or autograd.grad() the first time.

このエラーは通常、トレーニング モデル内の複数 (2 つ以上) の損失関数計算によって発生するため、最初の loss.backward() に restart_graph=True を追加します。

即ち:loss.backward(retain_graph=True)

しかし、私の問題では、私のモデルはGANモデルに似ています

そのため、追加してもエラーが発生します。

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [2048]] 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 回使用されます。

参考ブログ:

python – インプレース操作によって変更された変数の1つ - コードログ

pytorch の逆伝播の loss.backward(retain_graph=True) エラー - StarZhai - 博客园 

おすすめ

転載: blog.csdn.net/qq_42792802/article/details/126415532
おすすめ