Description Usage Arguments Value Note Author(s) Examples
Perform a backward pass to evaluate the partial derivatives of a given target node with respect to the nodes in a graph.
1 | cg_graph_backward(graph, target, index = NULL)
|
graph |
cg_graph object, graph that is differentiated. |
target |
cg_node object, node in the graph that is differentiated. Alternatively, argument |
index |
numerical scalar, index of the target node that is differentiated. Defaults to NULL (i.e. all elements are differentiated element-wise). |
None.
All nodes required to compute the target node must first have been evaluated by calling cg_graph_forward. The target node is only differenated with respect to those nodes on which it directly or indirectly depends.
In case the value of the target node is a vector or an array, argument index
can be used to specify which element of the vector or array is differentiated.
The derivatives have the same shape as the values of the nodes. They can be retrieved via the grad
data member of a cg_node
object.
If the name of the target node is supplied to argument target
, a linear search is performed to retrieve the node from the graph. In case multiple nodes share the same name, the last node added to the graph is retrieved. Please note that this linear search can become relatively expensive for large graphs.
Ron Triepels
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # 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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.