# no_grad

Source: https://www.tensorplay.cn/docs/generated/tensorplay.autograd.grad_mode.no_grad.html

# no_grad

class tensorplay.autograd.grad_mode.no_grad(orig_func=None)[[source]](../_modules/tensorplay/autograd/grad_mode.html#no_grad)

Context-manager that disables gradient calculation.

Disabling gradient calculation is useful for inference, when you are sure
that you will not call Tensor.backward(). It will reduce memory
consumption for computations that would otherwise have requires_grad=True.

In this mode, the result of every computation will have
requires_grad=False, even when the inputs have requires_grad=True.
There is an exception! All factory functions, or functions that create
a new Tensor and take a requires_grad kwarg, will NOT be affected by
this mode.

This context manager is thread local; it will not affect computation
in other threads.

Also functions as a decorator.

Note

No-grad is one of several mechanisms that can enable or
disable gradients locally see [Locally disabling gradient computation](../upstream_labels.html#locally-disable-grad-doc) for
more information on how they compare.

Note

This API does not apply to [forward-mode AD](../upstream_labels.html#forward-mode-ad).
If you want to disable forward AD for a computation, you can unpack
your dual tensors.

Example::

```
>>> x = tensorplay.tensor([1.], requires_grad=True)
>>> with tensorplay.no_grad():
...     y = x * 2
>>> y.requires_grad
False
>>> @tensorplay.no_grad()
... def doubler(x):
...     return x * 2
>>> z = doubler(x)
>>> z.requires_grad
False
>>> @tensorplay.no_grad()
... def tripler(x):
...     return x * 3
>>> z = tripler(x)
>>> z.requires_grad
False
>>> # factory function exception
>>> with tensorplay.no_grad():
...     a = tensorplay.nn.Parameter(tensorplay.rand(10))
>>> a.requires_grad
True
```
