x = torch.tensor([1., 2.], requires_grad=True)
# x: tensor([1., 2.], requires_grad=True)
y = 100*x
# y: tensor([100., 200.], grad_fn=<MulBackward0>)
loss = y.sum(). # tensor(300., grad_fn=<SumBackward0>)
# Compute gradients of the parameters respect to the loss
print(x.grad) # None, 反向传播前,梯度不存在
loss.backward()
print(x.grad) # tensor([100., 100.]) loss对y的梯度为1, 对x的梯度为100
optim = torch.optim.SGD([x], lr=0.001) # 随机梯度下降, 学习率0.001
print(x) # tensor([1., 2.], requires_grad=True)
optim.step() # 更新x
print(x) # tensor([0.9000, 1.9000], requires_grad=True) 变化量=梯度X学习率 0.1=100*0.001
下面是上述代码输出:
None
tensor([100., 100.])
tensor([1., 2.], requires_grad=True)
tensor([0.9000, 1.9000], requires_grad=True)