hessian(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 the jacobian of the gradient of func —
one forward pass, one backward pass that records its own graph, and
one backward pass through that graph per gradient coordinate. Cost
is therefore .
Parameters
funccallableFunction of one or more
Tensor inputs returning a tensor of
one element.Inputs at which is evaluated. They are left as they
are: the Hessian is taken at copies that require grad, so the
inputs'
requires_grad and .grad do not change.create_graphbool= FalseIf
True the Hessian itself is differentiable with respect to
the inputs that require grad (third-order derivatives). Defaults
to False.strictbool= FalseIf
True, refuse a gradient of func that does not depend on
the inputs (a linear func, say) and a Hessian block of an
input the gradient does not depend on, instead of filling them
with zeros. Defaults to False.vectorizebool= FalseReserved for a future vmap-based implementation. Currently
unused.
Returns
Raises
RuntimeErrorfunc does not return a single tensor of one element.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 ** 3).sum()
>>> hessian(f, x).tolist()
[[6.0, 0.0], [0.0, 12.0]]