# Understand with torch.no_grad() with Examples – PyTorch Tutorial

By | April 27, 2022

We often see with torch.no_grad(): in some pytorch script. What does it mean? In this tutorial, we will use an example to explain.

torch.no_grad() is a context manager, in order to undertand python context manager, you can view:

Create Customized Context Manager for Python With Statement: A Completed Guide – Python Tutorial

It will disable all gradient calculation in its context.

For example:

import torch

y = x * 2
z = x * 2
print(z.requires_grad)

Run this code, we will see:

True
True
False
True

In this example, tensor x is gradient supported. However, tensor y is in torch.no_grad() context. We can find:

model.eval()
# you test / evaluate code