by mariogeiger

mariogeiger /hessian

hessian in pytorch

143 Stars 14 Forks Last release: Not found 34 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Install with

python setup.py install

Compute Hessian

import torch
from hessian import hessian

x = torch.tensor([1.5, 2.5], requires_grad=True) h = hessian(x.pow(2).prod(), x, create_graph=True)


tensor([[12.5, 15],

[15, 4.5]], grad_fn=)

h2 = hessian(h.sum(), x) print(h2)

tensor([[4, 8],

[8, 4]])

The hessian is computed naively assuming the commutativity of the derivatives.

Compute Jacobian

import torch
from hessian import jacobian

x = torch.tensor([1.5, 2.5], requires_grad=True) y = torch.tensor([5.5, -4.], requires_grad=True) j = jacobian(x.pow(y), [x, y])


tensor([[34.1, -0.00, 3.77, 0.00],

[ 0.0, -0.04, 0.00, 0.02]])

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.