# set_grad_enabled

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

# set_grad_enabled

class tensorplay.autograd.grad_mode.set_grad_enabled(mode: [bool](https://docs.python.org/3/library/functions.html#bool))[[source]](../_modules/tensorplay/autograd/grad_mode.html#set_grad_enabled)

Context-manager that sets gradient calculation on or off.

set_grad_enabled will enable or disable grads based on its argument mode.
It can be used as a context-manager or as a function.

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

Parameters:

mode ([bool](https://docs.python.org/3/library/functions.html#bool)) – Flag whether to enable grad (True), or disable
(False). This can be used to conditionally enable
gradients.

Note

set_grad_enabled 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)
>>> is_train = False
>>> with tensorplay.set_grad_enabled(is_train):
...     y = x * 2
>>> y.requires_grad
False
>>> _ = tensorplay.set_grad_enabled(True)
>>> y = x * 2
>>> y.requires_grad
True
>>> _ = tensorplay.set_grad_enabled(False)
>>> y = x * 2
>>> y.requires_grad
False
```

clone() &#x2192; [set_grad_enabled](#tensorplay.autograd.grad_mode.set_grad_enabled)[[source]](../_modules/tensorplay/autograd/grad_mode.html#set_grad_enabled.clone)

Create a copy of this class
