0Pricing
Deep Learning Academy · Lesson

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

  1. requires_grad and the Computation Graph
  2. Call backward() to Get Gradients
  3. Reading and Zeroing .grad
  4. torch.no_grad() for Inference
← Back to Deep Learning Academy