Course contentsShow
← Machine Learning and Deep Learning
Lesson 796 of 3,53819. PyTorch FundamentalsFree lesson

The torch.no_grad() Context Manager

Disabling gradient computation entirely for inference or evaluation to save memory and computation.

The torch.no_grad() Context Manager

What you'll learn: How to completely disable gradient tracking during inference or evaluation to save memory and speed up computations.

The Core Idea

When you're just using your model to make predictions (not training it), you don't need PyTorch to build the computational graph or track gradients. The torch.no_grad() context manager tells PyTorch: "Don't bother tracking any operations in this block—I'm not calling .backward() here."

Think of it like turning off your car's dashcam when parked. The camera still works, but you're not recording because you don't need that footage. Similarly, your model still computes outputs, but PyTorch stops recording the operations needed for backpropagation.

When and Why to Use It

Use torch.no_grad() during:

  • Model evaluation on validation or test sets
  • Inference when making predictions on new data
  • Any forward pass where you won't call .backward()

Benefits:

  • Reduces memory consumption (no computational graph stored)
  • Speeds up computation (less bookkeeping overhead)
  • Prevents accidental gradient accumulation

How It Works

import torch

x = torch.tensor([2.0], requires_grad=True)

# Normal operation: gradients tracked
y = x ** 2  # Computational graph built

# Inside no_grad: gradients NOT tracked
with torch.no_grad():
    z = x ** 2  # No computational graph
    # z still computed correctly, but can't backprop through it

Inside the with torch.no_grad(): block, all operations behave as if requires_grad=False for all tensors, regardless of their actual setting.

Key Takeaway: Use torch.no_grad() whenever you're not training (evaluation/inference) to save memory and computation by disabling gradient tracking entirely for that code block.