Learn R Programming

cgraph (version 3.0.1)

cg_gradients: Calculate Gradients

Description

Differentiate the graph with respect to node name by reverse automatic differentiation.

Arguments

name

character scalar, name of the node that is differentiated.

values

named list or environment, values that are subsituted for the nodes in the graph.

index

numeric scalar, index of the target node that is differentiated. Defaults to the first element.

Value

environment, the gradients of all nodes with respect to target node name.

Details

$gradients(name, values = new.env(), index = 1)

Examples

Run this code
# NOT RUN {
# Initialize a new computational graph.
x <- cgraph$new()

# Add some parameters.
a <- x$parm(2, name = "a")
b <- x$parm(4, name = "b")

# Perform some operations on the parameters.
c <- cg_sin(a) + cg_cos(b) - cg_tan(a)

# Differentiate the graph with respect to c.
grads <- x$gradients(c, x$run(c))

# Retrieve the gradient of c with respect to a.
grads$a

# }

Run the code above in your browser using DataLab