What is the difference between torch.no_grad() and requires_grad=False?

Updated May 17, 2026

Short answer

torch.no_grad() disables gradient tracking temporarily, while requires_grad=False disables it permanently for a tensor.

Deep explanation

requires_grad is a tensor property defining whether autograd tracks operations on it. torch.no_grad() is a context manager used during inference to prevent gradient computation and reduce memory usage.

Real-world example

Used during model evaluation to speed up inference and reduce memory consumption.

Common mistakes

  • Using requires_grad=False during training mistakenly or forgetting no_grad during evaluation.

Follow-up questions

  • What is torch.inference_mode()?
  • Does no_grad affect model parameters?

More PyTorch interview questions

View all →