fn

hessian

Callable
hessian(func: Callable[..., Tensor], argnums: int | tuple[int, ...] = 0)
source

Build a function returning the Hessian of a scalar-valued func.

Computes the matrix of second partial derivatives by composing forward-mode over reverse-mode differentiation (jacfwd(jacrev(func))). The result captures the local curvature used by Newton-style optimisers, natural-gradient methods, and second-order analyses such as eigenvalue spectra of the loss.

Parameters

funcCallable
Function returning a scalar Tensor.
argnumsint or tuple of int= 0
Argument(s) to differentiate twice. Default 0.

Returns

Callable

Function returning the Hessian tensor with shape arg.shape + arg.shape.

Notes

For f:RnRf : \mathbb{R}^n \to \mathbb{R},

Hij=2fxixj,HRn×n.H_{ij} = \frac{\partial^2 f}{\partial x_i \, \partial x_j}, \quad H \in \mathbb{R}^{n \times n}.

Cost is dominated by nn JVPs over the reverse-mode gradient graph, retained via create_graph=True.

Examples

>>> import lucid
>>> from lucid.func import hessian
>>> f = lambda x: (x ** 3).sum()  # H = diag(6 * x)
>>> hessian(f)(lucid.tensor([1.0, 2.0, 3.0]))