# tensorplay.autograd.anomaly_mode API Source: https://www.tensorplay.cn/docs/api/tensorplay.autograd.anomaly_mode.html ## Classes 2 [#](#api-tensorplay.autograd.anomaly_mode.detect_anomaly) ### detect_anomaly class[Full reference ↗](/docs/generated/tensorplay.autograd.anomaly_mode.detect_anomaly.html) ```python class tensorplay.autograd.anomaly_mode.detect_anomaly(check_nan=True) ``` Context-manager that enables anomaly detection for the autograd engine. This does two things: - Running the forward pass with detection enabled will allow the backward pass to print the traceback of the forward operation that created the failing backward function. - If check_nan is True, any backward computation that generates “nan” value will raise an error. Default True. > **Warning** > > This mode should be enabled only for debugging as the different tests will slow down your program execution. Example ``` >>> import tensorplay >>> from tensorplay import autograd >>> class MyFunc(autograd.Function): ... @staticmethod ... def forward(ctx, inp): ... return inp.clone() ... ... @staticmethod ... def backward(ctx, gO): ... # Error during the backward pass ... raise RuntimeError("Some error in backward") ... return gO.clone() >>> def run_fn(a): ... out = MyFunc.apply(a) ... return out.sum() >>> inp = tensorplay.rand(10, 10, requires_grad=True) >>> out = run_fn(inp) >>> out.backward() Traceback (most recent call last): File "", line 1, in out.backward() RuntimeError: Some error in backward >>> with autograd.detect_anomaly(): ... inp = tensorplay.rand(10, 10, requires_grad=True) ... out = run_fn(inp) ... out.backward() Traceback of forward call that caused the error: File "tmp.py", line 53, in out = run_fn(inp) File "tmp.py", line 44, in run_fn out = MyFunc.apply(a) Traceback (most recent call last): File "", line 4, in RuntimeError: Some error in backward ``` [#](#api-tensorplay.autograd.anomaly_mode.set_detect_anomaly) ### set_detect_anomaly class[Full reference ↗](/docs/generated/tensorplay.autograd.anomaly_mode.set_detect_anomaly.html) ```python class tensorplay.autograd.anomaly_mode.set_detect_anomaly(mode: bool, check_nan: bool = True) ``` Context-manager that sets the anomaly detection for the autograd engine on or off. set_detect_anomaly will enable or disable the autograd anomaly detection based on its argument mode. It can be used as a context-manager or as a function. See detect_anomaly above for details of the anomaly detection behaviour. Parameters: - mode ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – Flag whether to enable anomaly detection (True), or disable (False). - check_nan ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – Flag whether to raise an error when the backward generate “nan”