liveBook · Manning

No_grad()

torch.no_grad() is a PyTorch context manager and decorator that disables autograd gradient tracking for operations performed within its scope.

How 2 Manning books explain it

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

What to know first

Related concepts

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.