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 pricing

Real-world example

No real-world example available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Common mistakes

No common mistakes listed yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Follow-up questions

No follow-up questions available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

More PyTorch interview questions

View all →