pub struct Tape { /* private fields */ }Expand description
A reverse-mode autodiff tape: the recorded operation graph plus the forward value of each node.
Build an expression by calling the operation methods, which append nodes and
return Var handles; the forward value is computed eagerly as each node
is pushed. Call grad on an output node to run the reverse
pass and accumulate the partial derivatives with respect to every input.
§Examples
use sim_lib_numbers_ad::Tape;
// f(a, b) = a * b + a, at a = 2, b = 5.
let mut tape = Tape::new();
let a = tape.input(0, 2.0);
let b = tape.input(1, 5.0);
let product = tape.mul(a, b);
let out = tape.add(product, a);
assert_eq!(tape.value(out), 12.0);
// df/da = b + 1 = 6, df/db = a = 2.
assert_eq!(tape.grad(out, 2), vec![6.0, 2.0]);Implementations§
Source§impl Tape
impl Tape
Sourcepub fn input(&mut self, slot: usize, value: f64) -> Var
pub fn input(&mut self, slot: usize, value: f64) -> Var
Records an independent input bound to gradient slot slot with the given
value, and returns its handle.
Sourcepub fn sub(&mut self, a: Var, b: Var) -> Var
pub fn sub(&mut self, a: Var, b: Var) -> Var
Records a - b and returns the handle of the difference.
Sourcepub fn mul(&mut self, a: Var, b: Var) -> Var
pub fn mul(&mut self, a: Var, b: Var) -> Var
Records a * b and returns the handle of the product.
Sourcepub fn div(&mut self, a: Var, b: Var) -> Var
pub fn div(&mut self, a: Var, b: Var) -> Var
Records a / b and returns the handle of the quotient.
Sourcepub fn grad(&self, out: Var, n_inputs: usize) -> Vec<f64>
pub fn grad(&self, out: Var, n_inputs: usize) -> Vec<f64>
Runs the reverse pass from output out and returns the gradient with
respect to the n_inputs input slots.
Seeds the adjoint of out with 1.0, walks the recorded nodes in
reverse, and accumulates each input slot’s partial derivative; the
returned vector has length n_inputs.