torch.autograd.no_grad类

2022-09-03 22:16:21 浏览数 (3)

torch.autograd.no_grad[source]

Context-manager that disabled 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.

This mode has no effect when using enable_grad context manager .

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

Also functions as a decorator.

Example:

代码语言:javascript复制
>>> x = torch.tensor([1], requires_grad=True)
>>> with torch.no_grad():
...   y = x * 2
>>> y.requires_grad
False
>>> @torch.no_grad()
... def doubler(x):
...     return x * 2
>>> z = doubler(x)
>>> z.requires_grad
False

0 人点赞