Skip to main content

record

Function record 

Source
pub fn record<F: Float + BtapeThreadLocal>(
    f: impl FnOnce(&[BReverse<F>]) -> BReverse<F>,
    x: &[F],
) -> (BytecodeTape<F>, F)
Available on crate feature bytecode only.
Expand description

Record a function into a BytecodeTape that can be re-evaluated at different inputs without re-recording.

Returns the tape and the output value from the recording pass.

§Limitations

The tape records one execution path. If f contains branches (if x > 0 { ... } else { ... }), re-evaluating at inputs that take a different branch produces incorrect results.

§Example

let (mut tape, val) = echidna::record(
    |x| x[0] * x[0] + x[1] * x[1],
    &[3.0, 4.0],
);
assert!((val - 25.0).abs() < 1e-10);

let g = tape.gradient(&[3.0, 4.0]);
assert!((g[0] - 6.0).abs() < 1e-10);
assert!((g[1] - 8.0).abs() < 1e-10);