fn
hessian
→Tensor or tuple of tuple of Tensorhessian(func: Callable[..., Tensor], inputs: Tensor | tuple[Tensor, ...], create_graph: bool = False, strict: bool = False, vectorize: bool = False)Compute the Hessian matrix of a scalar-valued func.
The Hessian of a scalar function is
Implemented as jacobian of the gradient of func —
a forward pass produces the loss, a first backward (with
create_graph=True) builds the gradient graph, and a second
backward along each gradient coordinate yields the rows of
. Cost is therefore .
Parameters
funccallableScalar-valued function of one or more
Tensor inputs.inputsTensor or tuple of TensorInputs at which is evaluated. They are silently
promoted to
requires_grad=True if necessary.create_graphbool= FalseIf
True the Hessian itself remains differentiable
(third-order derivatives). Defaults to False.strictbool= FalseReserved for stricter validation. Currently unused.
vectorizebool= FalseReserved for a future vmap-based implementation. Currently
unused.
Returns
Tensor or tuple of tuple of TensorFor a single input the returned tensor has shape
(numel(x), numel(x)). For multiple inputs a nested
tuple of cross-Hessian blocks is returned, with
H[i][j] containing .
Notes
Symmetry holds in exact arithmetic when is . In floating-point the result is only approximately symmetric; symmetrize as if a strictly symmetric matrix is required.
Examples
>>> import lucid
>>> from lucid.autograd import hessian
>>> x = lucid.tensor([1.0, 2.0])
>>> def f(x):
... return (x * x).sum()
>>> H = hessian(f, x)
>>> H.shape
(2, 2)