一、问题
如何使用Pytorch计算样本张量的基本梯度呢?考虑一个样本数据集,且有两个展示变量,在给定初始权重的基础上,如何在每次迭代中计算梯度呢?
二、如何运行
假设有x_data 和 y_data 列表,计算两个列表需要计算损失函数,一个forward通道以及一个循环中的训练。
forward函数计算权重矩阵和输入张量的乘积。
python
from torch import FloatTensor
from torch.autograd import Variable # 引入Variable方法是为了计算变量的梯度
a = Variable(FloatTensor([5]))
weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (12, 53, 91, 73)]
w1, w2, w3, w4 = weights #权重赋值
b = w1 * a
c = w2 * a
d = w3 * b + w4 * c
Loss = (10 - d)
Loss.backward() #从loss 开始反向传播
for index, weight in enumerate(weights, start=1):
gradient, *_ = weight.grad.data #取出梯度
print(f"Gradient of w{index} w.r.t to Loss: {gradient}")
Gradient of w1 w.r.t to Loss: -455.0
Gradient of w2 w.r.t to Loss: -365.0
Gradient of w3 w.r.t to Loss: -60.0
Gradient of w4 w.r.t to Loss: -265.0
# 使用forward
def forward(x):
return x * w #forwar过程
import torch
from torch.autograd import Variable
x_data = [11.0, 22.0, 33.0]
y_data = [21.0, 14.0, 64.0]
w = Variable(torch.Tensor([1.0]), requires_grad=True) # 初始化为任意值;
# 训练前打印
print("predict (before training)", 4, forward(4).data[0])
# 定义损失函数
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) * (y_pred - y)
#运行训练循环
for epoch in range(10):
for x_val, y_val in zip(x_data, y_data):
l = loss(x_val, y_val)
l.backward()
print("\tgrad: ", x_val, y_val, w.grad.data[0])
w.data = w.data - 0.01 * w.grad.data
# 训练后,人工设置梯度为0,否则梯度会累加;
w.grad.data.zero_()
print("progress:", epoch, l.data[0])
#结果
grad: 11.0 21.0 tensor(-220.)
grad: 22.0 14.0 tensor(2481.6001)
grad: 33.0 64.0 tensor(-51303.6484)
progress: 0 tensor(604238.8125)
progress: 1 ................................................
..........................................................................................
#训练后的预测 权重已更新
print("predict (after training)", 4, forward(4).data[0])
#结果
predict (after training) 4 tensor(-9.2687e+24)
下面的程序展示了如何用Variable 变量从损失函数计算梯度:
python
a = Variable(FloatTensor([5]))
weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (12, 53, 91, 73)]
w1, w2, w3, w4 = weights
b = w1 * a
c = w2 * a
d = w3 * b + w4 * c
Loss = (10 - d)
Loss.backward()