fn
vjp
→tuplevjp(func: Callable[..., Tensor | tuple[Tensor, ...]], primals: Tensor = (), has_aux: bool = False)Compute the vector-Jacobian product of func at primals.
Evaluates func at the supplied primal inputs and returns both the
output and a callable vjp_fn that, given cotangent vectors
, returns — the
backward-mode contraction of the Jacobian against v. This is the
workhorse used internally by grad and jacrev, and is
useful directly when many backward passes are needed against the same
forward computation.
Parameters
funcCallableDifferentiable function taking the primals as positional inputs.
*primalsTensor= ()Points at which to linearise
func.has_auxbool= FalseIf
True, func must return (output, aux); the call
then yields ((output, aux), vjp_fn). Default False.Returns
tuple(outputs, vjp_fn). vjp_fn(*cotangents) returns a tuple of
input-gradient tensors of the same shapes as primals.
Notes
For and cotangent ,
Cost is one backward pass per call to vjp_fn — the forward
graph is retained so multiple cotangents can be applied cheaply.
Examples
>>> import lucid
>>> from lucid.func import vjp
>>> f = lambda x: x ** 2
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> y, vjp_fn = vjp(f, x)
>>> (grads,) = vjp_fn(lucid.ones_like(y)) # 2 * x