hvp(func: Callable[..., Tensor], inputs: Tensor | tuple[Tensor, ...], v: Tensor | tuple[Tensor, ...] | None = None, create_graph: bool = False, strict: bool = False)Hessian-vector product of a scalar-valued func.
For with Hessian and a vector , returns
along with , without forming — the directional
derivative of the gradient, taken exactly as jvp takes one.
Parameters
funccallableFunction of the inputs returning one single-element tensor, as
for
vhp.The direction, one tensor per input, packed like
inputs.
Optional only for one single-element input (seeded with 1).create_graphbool= FalseKeep the output and the product differentiable. Defaults to
False.strictbool= FalseRefuse what
vhp refuses under strict. Defaults to
False.Returns
Raises
(RuntimeError, ShapeMismatch, DeviceMismatch)For the reasons
vhp gives.Notes
Three reverse passes against vhp's two; prefer vhp
when func is twice continuously differentiable, where the Hessian
is symmetric and the two agree up to rounding.
Examples
>>> import lucid
>>> from lucid.autograd import hvp
>>> x = lucid.tensor([1.0, 2.0])
>>> def f(x):
... return (x ** 3).sum()
>>> out, product = hvp(f, x, lucid.tensor([1.0, 0.0]))
>>> product
tensor([6., 0.])