with_grad_tape: Run code with gradient tape enabled

View source: R/autograd.R

with_grad_tapeR Documentation

Run code with gradient tape enabled

Description

Records all ag_* operations inside expr for later backward(). When the default device is "gpu", the ggml context is reset at the start of each tape.

Usage

with_grad_tape(expr)

Arguments

expr

Expression to evaluate under gradient tape

Value

Value of last expression in expr (invisibly)

Inputs computed on the device

The reset frees every intermediate result of earlier ag_* calls; only ag_param weights (and tensors made from host matrices, which keep a host copy) survive it. An input built from ag_* operations on the GPU – an observation normalised with ag_sub/ag_mul, say – must therefore be built inside expr: built before the tape, it is destroyed on entry and the forward pass fails with "buffer was freed by a tape reset".

Examples


w <- ag_param(matrix(c(1, 0, 0, 1), 2, 2))
x <- ag_tensor(matrix(c(1, 2), 2, 1))
y <- ag_tensor(matrix(c(1, 2), 2, 1))
with_grad_tape({
  out  <- ag_matmul(w, x)
  loss <- ag_mse_loss(out, y)
})
backward(loss)


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