API symbolsautograd
Copy
detect_anomaly
- class tensorplay.autograd.anomaly_mode.detect_anomaly(check_nan=True)[source]
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_nanisTrue, any backward computation that generates “nan” value will raise an error. DefaultTrue.
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 "<stdin>", line 1, in <module> 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 <module> out = run_fn(inp) File "tmp.py", line 44, in run_fn out = MyFunc.apply(a) Traceback (most recent call last): File "<stdin>", line 4, in <module> RuntimeError: Some error in backward
Help improve this page
Found an error, an unclear step, or a missing example?
Was this page helpful?
