# PyTorch计算损失函数对模型参数的Hessian矩阵示例

## 模型定义

```class ANN(nn.Module):
def __init__(self):
super(ANN, self).__init__()
self.sigmoid = nn.Sigmoid()
self.fc1 = nn.Linear(3, 4)
self.fc2 = nn.Linear(4, 5)

def forward(self, data):
x = self.fc1(data)
x = self.fc2(x)

return x
```

```model = ANN()
for param in model.parameters():
print(param.size())
```

```torch.Size([4, 3])
torch.Size([4])
torch.Size([5, 4])
torch.Size([5])
```

## 求解Hessian矩阵

```data = torch.tensor([1, 2, 3], dtype=torch.float)
label = torch.tensor([1, 1, 5, 7, 8], dtype=torch.float)
pred = model(data)
loss_fn = nn.MSELoss()
loss = loss_fn(pred, label)
```

`grads = torch.autograd.grad(loss, model.parameters(), retain_graph=True, create_graph=True)`

```(tensor([[-1.0530, -2.1059, -3.1589],
[ 2.3615,  4.7229,  7.0844],
[-1.5046, -3.0093, -4.5139],
[-2.0272, -4.0543, -6.0815]], grad_fn=), tensor([-1.0530,  2.3615, -1.5046, -2.0272], grad_fn=), tensor([[ 0.2945, -0.2725, -0.8159, -0.6720],
[ 0.1936, -0.1791, -0.5362, -0.4416],
[ 1.0800, -0.9993, -2.9918, -2.4641],
[ 1.3448, -1.2444, -3.7255, -3.0683],
[ 1.2436, -1.1507, -3.4450, -2.8373]], grad_fn=), tensor([-0.6045, -0.3972, -2.2165, -2.7600, -2.5522],
```

```hessian_params = []
# 判断是w还是b
# w