Pytorch求Hessian矩阵及二阶导的方法

可以对梯度再次求导,用来惩罚梯度变化过快的情况。
也可以用来算Hessian矩阵。

官方举例说明惩罚梯度的范数:

import torch
from torchvision.models import resnet18
from torch.autograd import Variable

model = resnet18().cuda()

# dummy inputs for the example
input = Variable(torch.randn(2,3,224,224).cuda(), requires_grad=True)
target = Variable(torch.zeros(2).long().cuda())

# as usual
output = model(input)
loss = torch.nn.functional.nll_loss(output, target)

grad_params = torch.autograd.grad(loss, model.parameters(), create_graph=True)
# torch.autograd.grad does not accumuate the gradients into the .grad attributes
# It instead returns the gradients as Variable tuples.

# now compute the 2-norm of the grad_params
grad_norm = 0
for grad in grad_params:
    grad_norm += grad.pow(2).sum()
grad_norm = grad_norm.sqrt()

# take the gradients wrt grad_norm. backward() will accumulate
# the gradients into the .grad attributes
grad_norm.backward()

# do an optimization step
optimizer.step()

计算海森矩阵可以参考这个例子:
[使用python,pytorch求海森Hessian矩阵](https://www.cnblogs.com/chester-cs/p/11755279.html)

你可能感兴趣的:(DL,pytorch)