Skip to main content

grad

Function grad 

Source
pub fn grad<F: Float + TapeThreadLocal>(
    f: impl FnOnce(&[Reverse<F>]) -> Reverse<F>,
    x: &[F],
) -> Vec<F>
Expand description

Compute the gradient of a scalar function f : R^n → R using reverse mode.

let g = echidna::grad(|x: &[echidna::Reverse<f64>]| {
    x[0] * x[0] + x[1] * x[1]
}, &[3.0, 4.0]);
assert!((g[0] - 6.0).abs() < 1e-10);
assert!((g[1] - 8.0).abs() < 1e-10);