| clip_grad_value | R Documentation |
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.
clip_grad_value(params, grads, clip_value)
params |
List of ag_param tensors (same as passed to optimizer), named or not. |
grads |
Gradient environment returned by |
clip_value |
Positive numeric; maximum absolute value per element. |
Call this after backward() and before
optimizer$step().
Numeric: the largest absolute gradient value before clipping (invisibly). Returns 0 when no gradients were found.
clip_grad_norm
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.