pub fn custom_jvp<B: Backend, I: Tree<B>, O: Tree<B>>(
f: impl Fn(I) -> O,
jvp: impl Fn(I, I) -> (O, O),
primals: I,
) -> OExpand description
f(primals), whose derivative in forward mode (and, by linearization,
in reverse mode) is given by the rule jvp, like jax.custom_jvp.
jvp takes the primals and their tangents (of the same structure; the
tangents of non-float values are zeros) and returns the outputs and their
tangents, which must be linear in the input tangents for reverse mode.
Natively, and when the function is not differentiated, f is computed.
Values that the functions capture are not differentiated.
ยงExamples
use ruxla_core::{F32, Trace, grad, lax};
// log(1 + e^x), with the derivative computed as sigmoid(x) * t.
fn softplus<B: ruxla_core::Backend>(x: F32<B>) -> F32<B> {
lax::custom_jvp(
|x: F32<B>| (x.exp() + 1.0).ln(),
|x: F32<B>, t: F32<B>| ((x.exp() + 1.0).ln(), t / ((-x).exp() + 1.0)),
x,
)
}
assert_eq!(softplus(F32::<ruxla_core::Native>::new(0.0)).value(), 2f32.ln());
let g = grad(|x: F32<Trace>, (): ()| softplus(x)).call(&F32::new(0.0), &());
assert_eq!(g.value(), 0.5);