clip_grad_value: Clip gradients element-wise by value

View source: R/ag_training.R

clip_grad_valueR Documentation

Clip gradients element-wise by value

Description

Clamps every gradient element into [-clip_value, clip_value]. Unlike clip_grad_norm this does not preserve the gradient direction — each element is treated independently. Modifies the grads environment in-place.

Usage

clip_grad_value(params, grads, clip_value)

Arguments

params

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

grads

Gradient environment returned by backward().

clip_value

Positive numeric; maximum absolute value per element.

Details

Call this after backward() and before optimizer$step().

Value

Numeric: the largest absolute gradient value before clipping (invisibly). Returns 0 when no gradients were found.

See Also

clip_grad_norm

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)
clip_grad_value(list(w = w), grads, clip_value = 0.5)


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