pub fn value_and_grad<F>(f: F) -> ValueAndGrad<F>Expand description
Returns a function that computes f(params, args) and its gradient with
respect to params, like jax.value_and_grad(f, has_aux=True).
f takes the parameters (a pytree of float values, which are
differentiated) and other arguments (which are not), and returns a
pytree whose first leaf is the loss, a float scalar. The other leaves are
auxiliary values, returned as they are. The gradient has the structure of
the parameters.
ยงExamples
use ruxla_core::{F32, Trace, value_and_grad};
// d/dx (x * x * y) = 2 * x * y
let f = value_and_grad(|x: F32<Trace>, y: F32<Trace>| x * x * y);
let (value, grad) = f.call(&F32::new(3.0), &F32::new(2.0));
assert_eq!((value.value(), grad.value()), (18.0, 12.0));