| check_grad_anomaly | R Documentation |
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.
check_grad_anomaly(
params,
grads,
action = c("warn", "stop", "silent"),
max_abs = NULL
)
params |
List of ag_param tensors (same as passed to optimizer), named or not. |
grads |
Gradient environment returned by |
action |
One of |
max_abs |
Optional numeric threshold; gradients whose maximum absolute
finite value exceeds it are also reported (as |
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".
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.