vhp(func: Callable[..., Tensor], inputs: Tensor | tuple[Tensor, ...], v: Tensor | tuple[Tensor, ...] | None = None, create_graph: bool = False, strict: bool = False)Vector-Hessian product of a scalar-valued func.
For with Hessian and a vector , returns
along with , without forming : one backward pass that records its own graph, and one backward pass through it.
Parameters
funccallableFunction of one or more
Tensor inputs returning a tensor of
one element.Point . Left as it is: the product is taken at copies
that require grad.
One vector per input, each of its input's shape and device. May
be left out when
inputs is one single-element tensor, which is
then seeded with 1.create_graphbool= FalseIf
True the output and the product are differentiable with
respect to the inputs that require grad (and v). Defaults to
False.strictbool= FalseIf
True, refuse an output that does not require grad, a
gradient that does not depend on the inputs (a linear func,
say) and a product that would be zeros for want of a path — and,
under create_graph, a product that is a constant. Defaults
to False.Returns
Raises
RuntimeErrorfunc does not return a single tensor of one element, v is
left out and inputs is not one single-element tensor, or
strict refuses a result.(ShapeMismatch, DeviceMismatch)An entry of
v does not have its input's shape or device.Notes
For a twice continuously differentiable func the Hessian is
symmetric and , so this and hvp
agree up to rounding; this one costs a pass less.
Examples
>>> import lucid
>>> from lucid.autograd import vhp
>>> x = lucid.tensor([1.0, 2.0])
>>> def f(x):
... return (x ** 3).sum()
>>> out, product = vhp(f, x, lucid.tensor([1.0, 1.0]))
>>> product
tensor([6., 12.])