# 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`
- **Domain:** pytorch
- **Category:** autograd_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| torch>=1.0.0 | active | — | — |

## Workarounds

1. **loss.backward(retain_graph=True)
# Then later
loss.backward()  # This now works** (95% success)
   ```
   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** (85% success)
   ```
   loss1.backward(retain_graph=True)
loss2.backward()
optimizer.step()
# Or compute both losses together
   ```

## Dead Ends

- **Setting requires_grad=False on all tensors** — This disables gradient computation entirely, which is not the intended fix. The error is about graph retention, not about needing gradients. (90% fail)
- **Calling zero_grad() between backward passes** — zero_grad() only resets gradients to zero, it does not retain the computation graph. The intermediate values are still freed after the first backward. (70% fail)
- **Recreating the model and optimizer after each backward** — 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. (80% fail)
