Skip to main content

grad_flat

Function grad_flat 

Source
pub fn grad_flat<F>(f: F, argnums: &[usize]) -> impl Fn(&[Array])
where F: Fn(&[Array]) -> Result<Array, Error>,
Expand description

Returns the gradient function of f with respect to the positional arguments at argnums (the flat form of grad).

use nuvec_core::{Array, Result, array, grad_flat};

let g = grad_flat(|xs: &[Array]| Ok(xs[0].multiply(&xs[1]).sum(..)), &[1]);
let grads = g(&[array![2.0], array![3.0]])?;
assert_eq!(grads[0].to_vec::<f32>()?, [2.0]);