Learn R Programming

cgraph (version 3.0.1)

cgraph: Computational Graph

Description

The cgraph class facilitates the construction, evaluation, and differentiation of computaiontal graphs in R.

Arguments

Usage

x <- cgraph$new()

Members

$nodes

named list, symbols of the nodes.

$values

environment, values of the nodes.

Methods

$initialize

initialize a computational graph, see cg_initialize.

$const

add a constant node to the graph, see cg_const.

$input

add an input node to the graph, see cg_input.

$parm

add a parameter node to the graph, see cg_parm.

$get_parms

list all parameters and their values, see cg_get_parms.

$add_parms

add parameters to the graph, see cg_add_parms.

$opr

add an operation node to the graph, see cg_opr.

$active

set the graph to be the active graph, see cg_active.

$val

get the value of a node in the graph, see cg_val.

$set

set the value of a node in the graph, see cg_set.

$run

evaluate a node in the graph, see cg_run.

$gradients

differentiate the graph by reverse automatic differentiation, see cg_gradients.

$adj_mat

retrieve the adjacency matrix of the graph, see cg_adj_mat.

$plot

plot the topology of the graph, see cg_plot.

Examples

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

# Add an input with name 'a' to the graph.
a <- input(name = "a")

# Add a parameter with value 4 and name 'b' to the graph.
b <- parm(4, name = "b")

# Perform some operations (i.e. c = exp(a * b)).
c <- cg_exp(a * b, name = "c")

# Evaluate c at a = 2.
values <- run(c, list(a = 2))

# Retrieve the value of c.
values$c

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

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

# }

Run the code above in your browser using DataLab