reverse_gradient

Function reverse_gradient 

Source
pub fn reverse_gradient<T, F, const N: usize>(
    f: F,
    point: [T; N],
) -> (T, [T; N])
where T: Float, F: FnOnce([Var<T>; N]) -> Var<T>,
Expand description

Computes the value and gradient of a multivariable function using reverse-mode AD.

This is the reverse-mode equivalent of gradient for functions f: ℝⁿ → ℝ.

§Examples

use autodiff::{reverse_gradient, Var};

// f(x, y) = x² + x*y at (3, 4)
let f = |[x, y]: [Var<f64>; 2]| x.clone() * x.clone() + x * y;

let (val, grad) = reverse_gradient(f, [3.0, 4.0]);
assert_eq!(val, 21.0);       // f(3, 4) = 9 + 12 = 21
assert_eq!(grad[0], 10.0);   // ∂f/∂x = 2x + y = 10
assert_eq!(grad[1], 3.0);    // ∂f/∂y = x = 3