{
  "id": "pytorch/strided-gradient-issue",
  "signature": "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.",
  "signature_zh": "运行时错误：试图第二次反向传播，但已保存的中间值已被释放。请在第一次调用backward时指定retain_graph=True。",
  "regex": "RuntimeError: Trying to backward through the graph a second time, but the saved intermediate values have already been freed",
  "domain": "pytorch",
  "category": "autograd_error",
  "subcategory": null,
  "root_cause": "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "在同一个计算图上两次调用backward()，而没有设置retain_graph=True，导致第一次反向传播后中间缓冲区被释放。",
  "versions": [
    {
      "version": "torch>=1.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Setting requires_grad=False on all tensors",
      "why_fails": "This disables gradient computation entirely, which is not the intended fix. The error is about graph retention, not about needing gradients.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Calling zero_grad() between backward passes",
      "why_fails": "zero_grad() only resets gradients to zero, it does not retain the computation graph. The intermediate values are still freed after the first backward.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Recreating the model and optimizer after each backward",
      "why_fails": "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.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "loss.backward(retain_graph=True)\n# Then later\nloss.backward()  # This now works",
      "success_rate": 0.95,
      "how": "loss.backward(retain_graph=True)\n# Then later\nloss.backward()  # This now works",
      "condition": "",
      "sources": []
    },
    {
      "action": "loss1.backward(retain_graph=True)\nloss2.backward()\noptimizer.step()\n# Or compute both losses together",
      "success_rate": 0.85,
      "how": "loss1.backward(retain_graph=True)\nloss2.backward()\noptimizer.step()\n# Or compute both losses together",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "loss.backward(retain_graph=True)\n# Then later\nloss.backward()  # This now works",
    "loss1.backward(retain_graph=True)\nloss2.backward()\noptimizer.step()\n# Or compute both losses together"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/autograd.html#torch.autograd.backward",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2023-06-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}