# inference_mode

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

# inference_mode

class tensorplay.autograd.grad_mode.inference_mode(mode=True)[[source]](../_modules/tensorplay/autograd/grad_mode.html#inference_mode)

Context manager that enables or disables inference mode.

InferenceMode is analogous to [no_grad](tensorplay.autograd.grad_mode.no_grad.html#tensorplay.autograd.grad_mode.no_grad) and should be used
when you are certain your operations will not interact with autograd
(e.g., during data loading or model evaluation). Compared to
[no_grad](tensorplay.autograd.grad_mode.no_grad.html#tensorplay.autograd.grad_mode.no_grad), it removes additional overhead by disabling view
tracking and version counter bumps. It is also more restrictive, in
that tensors created in this mode cannot be used in computations
recorded by autograd.

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

Also functions as a decorator.

Note

Inference mode is one of several mechanisms that can locally enable
or disable gradients. See [Locally disabling gradient computation](../upstream_labels.html#locally-disable-grad-doc) for a
comparison. If avoiding the use of tensors created in inference mode
in autograd-tracked regions is difficult, consider benchmarking your
code with and without inference mode to weigh the performance benefits
against the trade-offs. You can always use [no_grad](tensorplay.autograd.grad_mode.no_grad.html#tensorplay.autograd.grad_mode.no_grad) instead.

Note

Unlike some other mechanisms that locally enable or disable grad,
entering inference_mode also disables [forward-mode AD](../upstream_labels.html#forward-mode-ad).

Parameters:

mode ([bool](https://docs.python.org/3/library/functions.html#bool) or function) – Either a boolean flag to enable or disable
inference mode, or a Python function to decorate with inference
mode enabled.

Example::

```
>>> import tensorplay
>>> x = tensorplay.ones(1, 2, 3, requires_grad=True)
>>> with tensorplay.inference_mode():
...     y = x * x
>>> y.requires_grad
False
>>> y._version
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Inference tensors do not track version counter.
>>> @tensorplay.inference_mode()
... def func(x):
...     return x * x
>>> out = func(x)
>>> out.requires_grad
False
>>> @tensorplay.inference_mode()
... def doubler(x):
...     return x * 2
>>> out = doubler(x)
>>> out.requires_grad
False
```

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

Create a copy of this class
