torch.autograd.hessian
function hessian(func: TensorFunction, inputs: Tensor | Tensor[], options?: HessianOptions): Tensor | Tensor[] | Tensor[][]function hessian(func: TensorFunction, inputs: Tensor | Tensor[], create_graph: boolean, strict: boolean, vectorize: boolean, options?: HessianOptions): Tensor | Tensor[] | Tensor[][]Computes the Hessian of a scalar function with respect to its inputs.
The Hessian is a matrix of second-order partial derivatives. For a function f: R^n -> R, the Hessian is an n x n matrix.
Overload conventions:
hessian(func, inputs, options?)hessian(func, inputs, create_graph, options?)hessian(func, inputs, create_graph, strict, options?)hessian(func, inputs, create_graph, strict, vectorize)
Parameters
funcTensorFunction- A function that takes a tensor or tuple of tensors and returns a scalar tensor
optionsHessianOptionsoptional- Optional settings for Hessian computation
Returns
Examples
import * as torch from '@torchjsorg/torch.js';
const x = torch.randn(3, { requires_grad: true });
const func = (x: Tensor) => x.pow(2).sum();
// Hessian of sum(x^2) is 2*I (identity matrix scaled by 2)
const H = torch.autograd.functional.hessian(func, x);