No_grad()
torch.no_grad() is a PyTorch context manager and decorator that disables autograd gradient tracking for operations performed within its scope.
Inside Deep Learning: Math, Algorithms, Models
A PyTorch context manager that disables gradient tracking inside its block, commonly used for inference or evaluation steps.
- How it is used
- Keeps prediction, evaluation, or selected forward computations out of gradient tracking.
- How deep it goes
- Explains the context boundary through implementation examples and its time-and-memory motivation, without detailing gradient-tracking internals.
- Examples
- Shows linear-regression predictions, GAN sample inspection, sequence-to-sequence decoder selection, and held-out evaluation.
- What you can do after
- Apply it via PyTorch to place prediction or evaluation computations outside gradient tracking while preserving tracking where training needs it.
Read how Inside Deep Learning: Math, Algorithms, Models explains no_grad() →
Deep Learning with PyTorch, Second Edition
A PyTorch context manager that disables gradient tracking during inference to reduce memory use and overhead.
- How it is used
- Avoids tracking validation computations and manual parameter updates that do not need a backward pass.
- How deep it goes
- Explains context-manager use, graph-building costs, and a requires_grad exception involving views.
- Examples
- Shows a validation loss calculation and a training loop with an in-place parameter update.
- What you can do after
- Apply it via PyTorch to skip gradient tracking during validation or a manual parameter update.
Read how Deep Learning with PyTorch, Second Edition explains no_grad() →
Also covered in
- Build a DeepSeek Model (From Scratch)In early access (MEAP)
- Build a Text-to-Image Generator (from Scratch)
What to know first
- gradientA gradient is the vector of partial derivatives of a scalar function with respect to its variables, indicating its local rate and direction of change.
- language modelA language model is a computational model that represents patterns in linguistic sequences to assign probabilities, predict, generate, or evaluate language.
- objective functionAn objective function is a mathematical function that assigns values to candidate solutions or parameter settings, which an optimization procedure seeks to minimize or maximize.
- parameterA parameter is a value or named input that specifies or influences the behavior, computation, or output of a function, model, or system.
Related concepts
- teacher modeluses · A teacher model is a model whose outputs, behavior, or learned representations provide training signals for another model.
- gradientaddresses · A gradient is the vector of partial derivatives of a scalar function with respect to its variables, indicating its local rate and direction of change.
- automatic differentiationalternative to · Automatic differentiation is a computational technique that evaluates derivatives of a program by systematically applying the chain rule to its elementary operations.
- language modeluses · A language model is a computational model that represents patterns in linguistic sequences to assign probabilities, predict, generate, or evaluate language.
- freezeequivalent to · Freezing is the practice of keeping selected model parameters unchanged during training or adaptation, typically while allowing other parameters to be updated.
Questions about no_grad()
What is no_grad()?
torch.no_grad() is a PyTorch context manager and decorator that disables autograd gradient tracking for operations performed within its scope.
Which Manning books explain no_grad()?
Inside Deep Learning: Math, Algorithms, Models and Deep Learning with PyTorch, Second Edition.
What should I know before learning no_grad()?
Manning books that teach no_grad() usually introduce gradient, language model, objective function and parameter first.
What is the difference between no_grad() and automatic differentiation?
torch.no_grad() is a PyTorch context manager and decorator that disables autograd gradient tracking for operations performed within its scope. Automatic differentiation is a computational technique that evaluates derivatives of a program by systematically applying the chain rule to its elementary operations.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

