Skip to content
fn

jacobian

→Tensor, tuple of Tensor or tuple of tuple of Tensor
jacobian(func: Callable[..., Tensor | tuple[Tensor, ...]], inputs: Tensor | tuple[Tensor, ...], create_graph: bool = False, strict: bool = False, vectorize: bool = False, strategy: str = 'reverse-mode')

Compute the Jacobian matrix of func with respect to each input.

The Jacobian of a vector-valued function f:Rn→Rmf : \mathbb{R}^n \to \mathbb{R}^m is

Jij=∂fi∂xj,J∈Rm×n.J_{ij} = \frac{\partial f_i}{\partial x_j}, \qquad J \in \mathbb{R}^{m \times n}.

Lucid evaluates it row by row with reverse-mode passes — one per output element — seeding each pass with a row of the identity so the resulting input gradient is exactly the corresponding Jacobian row. The cost therefore scales with the output dimension mm; prefer vjp when only v⊤Jv^\top J is needed and jvp when only JvJ v is needed.

Parameters

funccallable
A function mapping Tensor inputs to a Tensor (or tuple of Tensor). Must be differentiable w.r.t. each positional input.
inputsTensor or tuple of Tensor
Input tensor(s) at which the Jacobian is evaluated. They are left as they are: the Jacobian is taken at copies that require grad, so the inputs' requires_grad and .grad do not change.
create_graphbool= False
If True the Jacobian itself is differentiable — with respect to the inputs that require grad — enabling higher-order derivatives. Defaults to False.
strictbool= False
If True, refuse an output that does not require grad, or an input some output does not depend on, instead of giving it a block of zeros — and, under create_graph, a block that is a constant. Defaults to False.
vectorizebool= False
Reserved for a future vmap-based implementation. Defaults to False.
strategystr= 'reverse-mode'
Differentiation strategy ('reverse-mode' or 'forward-mode'). 'forward-mode' requires vectorize=True and strict=False.

Returns

Tensor, tuple of Tensor or tuple of tuple of Tensor

One block per output out and input x, of shape out.shape + x.shape. Packed over the inputs as inputs was, and over the outputs as func returned them: a bare tensor for one output and one input, J[i] when only one of the two is a tuple, J[i][j] for output i and input j when both are.

Notes

Reverse-mode differentiation makes the cost per row O(cost(f))O(\text{cost}(f)); the full Jacobian therefore costs O(m⋅cost(f))O(m \cdot \text{cost}(f)). An input the output does not depend on gets a block of zeros.

Examples

>>> import lucid
>>> from lucid.autograd import jacobian
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> def f(x):
...     return x * x
>>> jacobian(f, x)
tensor([[2., 0., 0.], [0., 4., 0.], [0., 0., 6.]])

Used by 2