ag_grad: Gradient of a tensor as an R matrix

View source: R/autograd.R

ag_gradR Documentation

Gradient of a tensor as an R matrix

Description

After backward(), x$grad holds the gradient – as an R matrix on the closure path, but as a device handle when the graph backward kept it on the GPU (the default there). ag_grad() returns the value as a matrix either way, so code that reads gradients does not depend on where they live. In graph mode it is a synchronisation point.

Usage

ag_grad(x)

Arguments

x

An ag_tensor (usually a parameter).

Value

A numeric matrix, or NULL when x has no gradient.

Examples

w <- ag_param(matrix(1:4 + 0, 2, 2))
with_grad_tape({ loss <- ag_sum(ag_mul(w, w)) })
backward(loss)
ag_grad(w)            # 2 * w

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