No_grad()
no_grad() is a PyTorch context manager and decorator that disables autograd gradient tracking for enclosed tensor operations.
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
- Uses a scoped context to suppress gradient tracking during inference, evaluation, prediction, and frozen-model computation.
- How deep it goes
- Explains block scoping, time and memory savings, evaluation placement, and freezing tradeoffs without detailing autograd internals.
- Examples
- Demonstrates linear-regression predictions, GAN image generation, Seq2Seq decoding, held-out evaluation, and a frozen-BERT classifier.
- What you can do after
- Apply it via PyTorch to run prediction or evaluation efficiently and prevent unwanted gradient use in selected computation blocks.
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
- Disables autograd where gradients are unnecessary, notably during validation and hand-written parameter updates.
- How deep it goes
- Explains context-manager placement through training and validation loops, including graph overhead and a view-related `requires_grad` caveat.
- Examples
- Shows a linear-model parameter update, a validation-loss check, and a classification validation loop that collects metrics.
- What you can do after
- Operate/configure it to omit gradient tracking from validation or an in-place parameter update without disabling the training backward pass.
Read how Deep Learning with PyTorch, Second Edition explains no_grad() →
Related concepts
- teacher modeluses · A teacher model is a model whose outputs, labels, or learned behavior provide training signals or targets 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 tokens to estimate sequence probabilities and predict or generate language from context.
- freezeequivalent to · Freezing is the practice of keeping selected model parameters unchanged during training or adaptation, typically while allowing other parameters to be updated.
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.

