Learn R Programming

ggmlR (version 0.8.4)

check_grad_anomaly: 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
)

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".

Arguments

params

Named list of ag_param tensors (same as passed to optimizer).

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.

Examples

Run this code
# \donttest{
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)
# }

Run the code above in your browser using DataLab