Skip to main content

Tape

Struct Tape 

Source
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

Source

pub fn new() -> Self

Creates an empty tape.

Source

pub fn constant(&mut self, value: f64) -> Var

Records a constant node and returns its handle.

Source

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.

Source

pub fn add(&mut self, a: Var, b: Var) -> Var

Records a + b and returns the handle of the sum.

Source

pub fn sub(&mut self, a: Var, b: Var) -> Var

Records a - b and returns the handle of the difference.

Source

pub fn mul(&mut self, a: Var, b: Var) -> Var

Records a * b and returns the handle of the product.

Source

pub fn div(&mut self, a: Var, b: Var) -> Var

Records a / b and returns the handle of the quotient.

Source

pub fn sin(&mut self, arg: Var) -> Var

Records sin(arg) and returns its handle.

Source

pub fn cos(&mut self, arg: Var) -> Var

Records cos(arg) and returns its handle.

Source

pub fn exp(&mut self, arg: Var) -> Var

Records exp(arg) and returns its handle.

Source

pub fn ln(&mut self, arg: Var) -> Var

Records ln(arg) and returns its handle.

Source

pub fn sqrt(&mut self, arg: Var) -> Var

Records sqrt(arg) and returns its handle.

Source

pub fn recip(&mut self, arg: Var) -> Var

Records 1 / arg and returns its handle.

Source

pub fn value(&self, var: Var) -> f64

Returns the forward value recorded for var.

Source

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.

Trait Implementations§

Source§

impl Clone for Tape

Source§

fn clone(&self) -> Tape

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tape

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Tape

Source§

fn default() -> Tape

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Tape

§

impl RefUnwindSafe for Tape

§

impl Send for Tape

§

impl Sync for Tape

§

impl Unpin for Tape

§

impl UnsafeUnpin for Tape

§

impl UnwindSafe for Tape

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.