pytorch中求导可以借助tensor.backward()
和torch.autograd.grad()
来完成,backward()函数应该很熟悉了,模型的的参数求导一般是使用这个函数来完成。backward()
默认是标量对参数求导,如果是向量对参数求导,要传入一个维度和向量一样的tensor。
torch.autograd.grad(outputs, inputs, grad_outputs, retain_graph, create_graph, only_inputs)
也可用来对参数求导,这函数的参数解释如下:
import torch
x = torch.randn(3, 4).requires_grad_(True)
y = x ** 2
weight = torch.ones(y.size())
y.backward(weight, retain_graph=True)
print('backward grad')
print(x.grad)
dydx = torch.autograd.grad(outputs=y,
inputs=x,
grad_outputs=weight,
retain_graph=True,
create_graph=True,
only_inputs=True)
print('autograd grad')
print(dydx[0])
d2ydx2 = torch.autograd.grad(outputs=dydx[0],
inputs=x,
grad_outputs=weight,
retain_graph=True,
create_graph=True,
only_inputs=True)
print('Second Derivative')
print(d2ydx2[0])
'''
backward grad
tensor([[-1.6736, -4.1714, -0.8464, -3.2346],
[-2.3770, -3.3405, 1.2205, 0.0100],
[-0.9843, -0.6907, -0.8425, 0.6817]])
autograd grad
tensor([[-1.6736, -4.1714, -0.8464, -3.2346],
[-2.3770, -3.3405, 1.2205, 0.0100],
[-0.9843, -0.6907, -0.8425, 0.6817]], grad_fn=)
Second Derivative
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]], grad_fn=)
'''