seniorPyTorch
How does PyTorch autograd handle multiple backward passes on the same graph?
Updated May 17, 2026
Short answer
By default, PyTorch frees the computation graph after backward; multiple backward passes require retain_graph=True.
Deep explanation
Autograd frees intermediate buffers after backward to save memory. If a second backward pass is needed on the same graph, retain_graph=True must be used to preserve saved tensors. Otherwise, PyTorch raises an error because required intermediate values no longer exist.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro