torch.autograd.detect_anomaly
function detect_anomaly<T>(fn: () => T, options?: { check_nan?: boolean }): TContext manager for enabling anomaly detection.
This is a context manager version of set_detect_anomaly that automatically restores the previous state when done.
Parameters
fn() => T- Function to execute with anomaly detection enabled
options{ check_nan?: boolean }optional- Additional options
Returns
T– The result of the functionExamples
// Enable anomaly detection for a specific section
torch.autograd.detect_anomaly(() => {
const x = torch.randn(3, { requires_grad: true });
const y = x.div(torch.zeros(3)); // Will produce NaN
y.sum().backward(); // Will raise error with stack trace
});