import matplotlib.pyplot as plt
import torch
# y = w1*x2 + w2 * x + b,注意超参数学习率的设置,这里设置为0.01
x_data = [1, 2, 3]
y_data = [2, 4, 6]
loss_list = []
w1 = torch.Tensor([1])
w2 = torch.Tensor([1])
b = torch.Tensor([1])
w1.requires_grad = True
w2.requires_grad = True
b.requires_grad = True
def forward(x):
return w1*x**2+w2*x+b #W是tensor,tensor的运算后是建立计算图
def loss(x, y):
y_pre = forward(x)
return (y-y_pre)**2
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y) #建立计算图
l.backward() #每次调用backward后,计算图消失
print('\tgrad:', x, y, w1.grad.item(), w2.grad.item(), b.grad.item())
w1.data = w1.data - 0.01 * w1.grad.data #W是tensor,.data属性和W.grad.data也是tensor,但其不建立计算图
w2.data = w2.data - 0.01 * w2.grad.data
b.data = b.data - 0.01 * b.grad.data
w1.grad.data.zero_() #防止w的梯度累加起来,因此需要清空
w2.grad.data.zero_()
b.grad.data.zero_()
loss_list.append(l.item()) #item()将tensor转换成标量
print('progress:', epoch, l.item())
print("predict (after training)", 4, forward(4).item())
plt.plot(loss_list)
plt.ylabel('loss')
plt.show()