Learn R Programming

cgraph (version 6.0.1)

cg_graph_backward: Backward Pass

Description

Perform a backward pass to evaluate the partial derivatives of a given target node with respect to the nodes in a graph.

Usage

cg_graph_backward(graph, target, index = NULL)

Arguments

graph

cg_graph object, graph that is differentiated.

target

cg_node object, node in the graph that is differentiated. Alternatively, argument target can be a character scalar denoting the name of the node in the graph that is differentiated.

index

numerical scalar, index of the target node that is differentiated. Defaults to NULL (i.e. all elements are differentiated element-wise).

Value

None.

Examples

Run this code
# NOT RUN {
# Initialize a computational graph
graph <- cg_graph()

# Add an input
a <- cg_input(name = "a")

# Add a parameter
b <- cg_parameter(4, name = "b")

# Perform some operations
c <- cg_sin(a) + cg_cos(b) - cg_tan(a)

# Set a equal to 2
a$value <- 2

# Perform forward pass
cg_graph_forward(graph, c)

# Perform backward pass
cg_graph_backward(graph, c)

# Retrieve the derivative of c with respect to b
b$grad

# }

Run the code above in your browser using DataLab