vjp(func: Callable[..., Tensor | tuple[Tensor, ...]], inputs: Tensor | tuple[Tensor, ...], v: Tensor | tuple[Tensor, ...] | None = None, create_graph: bool = False, strict: bool = False)Vector-Jacobian product (reverse-mode AD).
Given with Jacobian and cotangent vector , returns along with the primal output . Computing a full VJP costs one backward pass.
Parameters
funccallableFunction mapping
Tensor inputs to a Tensor (or tuple thereof).Primal point at which is evaluated. Left as
it is: the product is taken at copies that require grad.
Cotangent vector(s), one per output of
func, each of its
output's shape and device — the seed rules of lucid.autograd.grad.
May be left out when func returns one single-element tensor.create_graphbool= FalseIf
True the returned VJP is itself differentiable. Defaults to False.strictbool= FalseIf
True, refuse an output that does not require grad and an
input the outputs do not depend on, instead of giving it a zero
gradient. Defaults to False.Returns
Raises
RuntimeErrorThe number of entries of
v differs from the number of
outputs, v is left out and func does not return one
single-element tensor, or strict refuses a result.(ShapeMismatch, DeviceMismatch)An entry of
v does not have its output's shape or device.Notes
The dual to vjp is jvp, which computes .
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])
>>> y, grad_x = vjp(lambda x: x * x, x, v)
>>> grad_x
tensor([2., 4., 6.])