fn
vjp
→tuple of (Tensor, tuple of (Tensor or None))vjp(func: Callable[..., Tensor], inputs: Tensor | tuple[Tensor, ...], v: Tensor | tuple[Tensor, ...], create_graph: bool = False, strict: bool = False)Vector-Jacobian product (reverse-mode AD).
Given with Jacobian and a cotangent vector , returns
along with the primal output . This is the operation that backpropagation performs on every node: when a scalar loss is being differentiated against an intermediate , the upstream cotangent is and the result is .
Computing a full VJP costs the same as one backward pass — much cheaper than materialising when only the product is needed.
Parameters
funccallableFunction mapping
Tensor inputs to a Tensor (or
tuple thereof).inputsTensor or tuple of TensorPrimal point at which is evaluated.
Silently promoted to
requires_grad=True if needed.vTensor or tuple of TensorCotangent vector(s) matching the output shape(s) of
func. Scalar-valued v is broadcast for scalar
outputs.create_graphbool= FalseIf
True the returned VJP is itself differentiable,
enabling double-backward. Defaults to False.strictbool= FalseReserved for stricter validation. Currently unused.
Returns
tuple of (Tensor, tuple of (Tensor or None))(output, vjp_grads) where output = func(*inputs)
and vjp_grads[i] is projected onto
input i (or None if that input has no gradient
path).
Notes
The dual to vjp is jvp, which computes
via forward-mode (or finite differences in
Lucid's current implementation).
Examples
>>> import lucid
>>> from lucid.autograd import vjp
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> v = lucid.tensor([1.0, 1.0, 1.0])
>>> def f(x):
... return x * x
>>> y, (grad_x,) = vjp(f, x, v)