torch.no_grad() for Inference
Skip graph tracking to save memory.
Not Every Pass Needs Grads
You only need gradients while training. When you just want predictions, tracking the graph is wasted work, so PyTorch lets you turn it off. 🛑
Meet torch.no_grad
Wrap code in a torch.no_grad() block and autograd stops recording inside it. No graph is built, and no gradients are stored.
with torch.no_grad():
preds = model(x)All lessons in this course
- requires_grad and the Computation Graph
- Call backward() to Get Gradients
- Reading and Zeroing .grad
- torch.no_grad() for Inference