
Computational graph with reverse-mode differentiation (Geron Ch 12, morie.fn hmcgrf)
Source:R/geron_ml2_native.R
morie_geron_computational_graph.RdBuilds the expression DAG, evaluates it forwards, then makes ONE reverse adjoint sweep that yields every partial derivative at once – whatever the number of variables. A central-difference check at h = 1e-5 is returned alongside, so the analytic gradient is verified by an independent route rather than asserted.
Value
List with value, grad, gradient, node_grads,
nodes, n_nodes, topo_order, fd_check, estimate, n.
Details
Expressions are nested lists: list("mul", "x", list("add", "y", 2)),
with bare strings for variables and bare numbers for constants.
Supported ops: add, sub, mul, div, pow, neg, exp, log, sin, cos,
tanh, sqrt, square.
Examples
morie_geron_computational_graph(expr = 5L)
#> $value
#> [1] 5
#>
#> $grad
#> list()
#>
#> $gradient
#> list()
#>
#> $node_grads
#> [1] 1
#>
#> $nodes
#> $nodes[[1]]
#> $nodes[[1]]$op
#> [1] "const"
#>
#> $nodes[[1]]$value
#> [1] 5
#>
#> $nodes[[1]]$inputs
#> integer(0)
#>
#>
#>
#> $n_nodes
#> [1] 1
#>
#> $topo_order
#> [1] 0
#>
#> $fd_check
#> list()
#>
#> $estimate
#> [1] 5
#>
#> $n
#> [1] 1
#>
#> $method
#> [1] "expression DAG with forward evaluation and reverse-mode adjoint sweep"
#>