Skip to content

mariogeiger/hessian

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hessian

Install with

python -m pip install git+https://github.com/mariogeiger/hessian.git

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)

print(h)
# tensor([[12.5, 15],
#         [15,  4.5]], grad_fn=<CopySlices>)

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])

print(j)
# tensor([[34.1, -0.00,  3.77,  0.00],
#         [ 0.0, -0.04,  0.00,  0.02]])

About

hessian in pytorch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages