(1)反向传播主要是通过loss构建计算图,在计算图上进行梯度传播,每调用一次loss函数,动态的构建一次计算图
(2)线性模型计算图:
Forward:从input到loss
Backward:从loss到x
(3)代码说明:
Tensor(可存向量、矩阵、标量)
Tensor中的成员:data存权重的值,grad存损失函数对权重的导数
grad也是一个Tensor
(4)线性模型y=w*x ,用pytorch实现反向传播代码如下:
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = torch.Tensor([1.0])
#需要计算梯度,默认不需要
w.requires_grad = True
#w是Tensor,x自动进行类型转换
def forward(x):
return x * w
#构建计算图
def loss(x , y):
y_pred = forward(x)
return (y_pred - y) ** 2
print('Predict (before training)', 4, forward(4))
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print('\tgrad:', x, y, w.grad.item())
#grad也是一个Tensor
w.data = w.data - 0.01 * w.grad.data
w.grad.data.zero_()
print('progress:', epoch, l.item())
print('Predict (after training)', 4, forward(4))
根据反向传播预测权重,最终的输出结果为:
(5) y=w1*x*x+w2*x+b,用pytorch实现反向传播代码如下:
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w1 = torch.Tensor([1.0])
w1.requires_grad = True
w2 = torch.Tensor([1.0])
w2.requires_grad = True
b = torch.Tensor([1.0])
b.requires_grad = True
def forward(x):
return x * x * w1 + x * w2 + b
#构建计算图
def loss(x , y):
y_pred = forward(x)
return (y_pred - y) ** 2
print('Predict (before training)', 4, forward(4))
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print('\tgrad:', x, y, w1.grad.item(), w2.grad.item(), b.grad.item())
w1.data = w1.data - 0.01 * w1.grad.data
w1.grad.data.zero_()
w2.data = w2.data - 0.01 * w2.grad.data
w2.grad.data.zero_()
b.data = b.data - 0.01 * b.grad.data
b.grad.data.zero_()
print('progress:', epoch, l.item())
print('Predict (after training)', 4, forward(4))
根据反向传播预测权重和偏置,最终的输出结果为:
根据输出结果可以分析出这个模型不适用于这组数据。