运行时错误:试图第二次反向传播,但已保存的中间值已被释放。请在第一次调用backward时指定retain_graph=True。
RuntimeError: Trying to backward through the graph a second time, but the saved intermediate values have already been freed. Specify retain_graph=True when calling backward the first time.
ID: pytorch/strided-gradient-issue
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| torch>=1.0.0 | active | — | — | — |
根因分析
在同一个计算图上两次调用backward(),而没有设置retain_graph=True,导致第一次反向传播后中间缓冲区被释放。
English
Calling backward() twice on the same computation graph without setting retain_graph=True, causing the intermediate buffers to be freed after the first backward pass.
官方文档
https://pytorch.org/docs/stable/autograd.html#torch.autograd.backward解决方案
-
loss.backward(retain_graph=True) # Then later loss.backward() # This now works
-
loss1.backward(retain_graph=True) loss2.backward() optimizer.step() # Or compute both losses together
无效尝试
常见但无效的做法:
-
Setting requires_grad=False on all tensors
90% 失败
This disables gradient computation entirely, which is not the intended fix. The error is about graph retention, not about needing gradients.
-
Calling zero_grad() between backward passes
70% 失败
zero_grad() only resets gradients to zero, it does not retain the computation graph. The intermediate values are still freed after the first backward.
-
Recreating the model and optimizer after each backward
80% 失败
This is extremely inefficient and does not address the root cause. The graph is still freed after the first backward unless retain_graph is set.