jvp
→tuple of (Tensor or tuple of Tensor, Tensor or tuple of Tensor)jvp(func: Callable[..., Tensor], inputs: Tensor | tuple[Tensor, ...], v: Tensor | tuple[Tensor, ...], create_graph: bool = False, strict: bool = False)Jacobian-vector product (forward-mode directional derivative).
Given with Jacobian and a tangent vector , returns
along with the primal output . JVPs are the natural primitive of forward-mode AD and are useful for propagating tangent information (sensitivities) through a network in a single forward sweep, for computing directional derivatives, and as a building block for second-order methods.
Lucid currently realises the JVP via a symmetric central finite difference
with . This avoids the need for a true forward-mode implementation while still being accurate enough for testing and most applications.
Parameters
funccallableTensor inputs to a Tensor (or
tuple thereof).inputsTensor or tuple of TensorvTensor or tuple of Tensorcreate_graphbool= Falsestrictbool= FalseReturns
tuple of (Tensor or tuple of Tensor, Tensor or tuple of Tensor)(primals_out, tangents_out) where
primals_out = func(*inputs) and tangents_out has the
same shape as primals_out and holds .
Notes
The complementary operation is vjp, which computes
cheaply via reverse-mode. Use jvp
when the input dimension is small relative to the output
dimension; otherwise reverse-mode is more efficient.
Examples
>>> import lucid
>>> from lucid.autograd import jvp
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> v = lucid.tensor([1.0, 0.0, 0.0])
>>> def f(x):
... return x * x
>>> y, tangent = jvp(f, x, v)