# enable_grad

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

# enable_grad

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

Context-manager that enables gradient calculation.

Enables gradient calculation, if it has been disabled via [no_grad](tensorplay.autograd.grad_mode.no_grad.html#tensorplay.autograd.grad_mode.no_grad)
or [set_grad_enabled](tensorplay.autograd.grad_mode.set_grad_enabled.html#tensorplay.autograd.grad_mode.set_grad_enabled).

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

Also functions as a decorator.

Note

enable_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).

Example::

```
>>> # xdoctest: +SKIP
>>> x = tensorplay.tensor([1.], requires_grad=True)
>>> with tensorplay.no_grad():
...     with tensorplay.enable_grad():
...         y = x * 2
>>> y.requires_grad
True
>>> y.backward()
>>> x.grad
tensor([2.])
>>> @tensorplay.enable_grad()
... def doubler(x):
...     return x * 2
>>> with tensorplay.no_grad():
...     z = doubler(x)
>>> z.requires_grad
True
>>> @tensorplay.enable_grad()
... def tripler(x):
...     return x * 3
>>> with tensorplay.no_grad():
...     z = tripler(x)
>>> z.requires_grad
True
```
