jacobian
→Tensor or tuple of Tensorjacobian(func: Callable[..., Tensor], inputs: Tensor | tuple[Tensor, ...], create_graph: bool = False, strict: bool = False, vectorize: bool = False)Compute the Jacobian matrix of func with respect to each input.
The Jacobian of a vector-valued function is
Lucid evaluates it row-by-row by repeated reverse-mode
backward passes — one per output element — seeding each pass
with a one-hot cotangent so the resulting input gradient is
exactly the corresponding Jacobian row. The cost therefore
scales with the output dimension ; prefer vjp
when only is needed and jvp when only
is needed.
Parameters
funccallableTensor inputs to a Tensor (or
tuple of Tensor). Must be differentiable w.r.t. each
positional input.inputsTensor or tuple of Tensorrequires_grad=True if needed.create_graphbool= FalseTrue the Jacobian itself is differentiable, enabling
higher-order derivatives (e.g. building hessian on
top). Defaults to False.strictbool= Falsevectorizebool= FalseReturns
Tensor or tuple of TensorFor a single input x the returned tensor has shape
(prod(out_shape), prod(x.shape)). For multiple inputs
a tuple is returned, one Jacobian block per input.
Notes
Reverse-mode differentiation makes the cost per row ; the full Jacobian therefore costs . For square or wide Jacobians () forward-mode would be cheaper — Lucid does not yet ship a forward-mode implementation, so this routine is preferred for tall Jacobians ().
Examples
>>> import lucid
>>> from lucid.autograd import jacobian
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> def f(x):
... return x * x
>>> J = jacobian(f, x)
>>> J.shape
(3, 3)