Skip to main content

grad

Function grad 

Source
pub fn grad<P, F, R>(f: F) -> impl Fn(&P) -> Result<P>
where P: Tree, F: Fn(&P) -> R, R: IntoResult<Ok = Array>,
Expand description

Returns the gradient function of f (a scalar function of a tree): the gradient with respect to the trainable leaves, as a tree of the argument’s type (see value_and_grad).

Gradient functions can be differentiated again:

use nuvec_core::{Array, Result, grad};

let d2 = grad(grad(|x: &Array| x.sin()));
let x = Array::from(1.0f32);
assert!((d2(&x)?.item::<f32>()? + 1f32.sin()).abs() < 1e-6); // −sin(x)