check_grad_anomaly: Detect NaN / Inf gradients

View source: R/ag_training.R

check_grad_anomalyR Documentation

Detect NaN / Inf gradients

Description

Scans the gradient environment produced by backward() and reports every parameter whose gradient contains NaN or Inf values. Useful as a debugging hook right after backward() — it pinpoints the offending layer instead of leaving you with a model that silently turns to NaN several epochs later.

Usage

check_grad_anomaly(
  params,
  grads,
  action = c("warn", "stop", "silent"),
  max_abs = NULL
)

Arguments

params

List of ag_param tensors (same as passed to optimizer), named or not.

grads

Gradient environment returned by backward().

action

One of "warn" (default), "stop" or "silent" — what to do when an anomaly is found.

max_abs

Optional numeric threshold; gradients whose maximum absolute finite value exceeds it are also reported (as "large"). NULL (default) disables this check.

Value

A data frame (invisibly) with one row per parameter that has a gradient, containing columns param, n_nan, n_inf, max_abs and status. status is one of "ok", "nan", "inf", "nan+inf" or "large". Parameters without a gradient are reported with status = "missing".

Examples


w  <- ag_param(matrix(runif(4), 2, 2))
x  <- ag_tensor(matrix(c(1, 1), 2, 1))
with_grad_tape({
  out  <- ag_matmul(w, x)
  loss <- ag_mse_loss(out, matrix(0, 2, 1))
})
grads <- backward(loss)
check_grad_anomaly(list(w = w), grads)


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.