https://github.com/mariogeiger/hessian
hessian in pytorch
https://github.com/mariogeiger/hessian
Last synced: 4 months ago
JSON representation
hessian in pytorch
- Host: GitHub
- URL: https://github.com/mariogeiger/hessian
- Owner: mariogeiger
- Created: 2018-01-11T18:12:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-27T07:57:11.000Z (over 4 years ago)
- Last Synced: 2024-12-14T22:50:15.671Z (4 months ago)
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 186
- Watchers: 6
- Forks: 17
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- Awesome-pytorch-list-CNVersion - hessian
- Awesome-pytorch-list - hessian
README
# hessian
Install with
```
python -m pip install git+https://github.com/mariogeiger/hessian.git
```## Compute Hessian
```python
import torch
from hessian import hessianx = 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=)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
```python
import torch
from hessian import jacobianx = 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]])
```