pytorch autograd_error ai_generated true

运行时错误:试图第二次反向传播,但已保存的中间值已被释放。请在第一次调用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

其他格式: JSON · Markdown 中文 · English
95%修复率
88%置信度
1证据数
2023-06-10首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://pytorch.org/docs/stable/autograd.html#torch.autograd.backward

解决方案

  1. loss.backward(retain_graph=True)
    # Then later
    loss.backward()  # This now works
  2. loss1.backward(retain_graph=True)
    loss2.backward()
    optimizer.step()
    # Or compute both losses together

无效尝试

常见但无效的做法:

  1. 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.

  2. 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.

  3. 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.