Learn R Programming

ggmlR (version 0.6.1)

backward: Run backward pass from a scalar loss tensor

Description

Traverses the gradient tape in reverse and accumulates gradients into tensor$grad for all leaf tensors with requires_grad = TRUE.

Usage

backward(loss)

Value

Named environment: tensor id -> gradient matrix (for use by optimizer$step)

Arguments

loss

Scalar ag_tensor

Examples

Run this code
# \donttest{
w <- ag_param(matrix(runif(4), 2, 2))
x <- ag_tensor(matrix(c(1, 2), 2, 1))
y <- ag_tensor(matrix(c(0, 1), 2, 1))
with_grad_tape({
  out  <- ag_matmul(w, x)
  loss <- ag_mse_loss(out, y)
})
grads <- backward(loss)
# }

Run the code above in your browser using DataLab