本节课讲的是反向传播。
课堂代码:
#反向传播课上代码
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = torch.tensor([1.0]) # w的初值为1.0
w.requires_grad = True # 默认为False,True表示需要计算梯度
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).item())
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y) # l是一个张量,tensor主要是在建立计算图
l.backward()
print('\tgrad:', x, y, w.grad.item())
w.data = w.data - 0.01 * w.grad.data # 权重更新时,需要用到标量,注意grad也是一个tensor
w.grad.data.zero_() # 将梯度置为0
print('progress:', epoch, l.item()) # 取出loss使用l.item,直接使用l会构建计算图
print("predict (after training)", 4, forward(4).item())
运行结果图:
课后作业:
#import datetime
# 设置函数y = x^2+2x+3 4应对应得到27
x_data = [1.0,2.0,3.0]
y_data = [6.0,11.0,18.0]
w1 = torch.Tensor([0.0]) # w1的初值为0.0
w1.requires_grad = True # 需要计算梯度
w2 = torch.Tensor([1.0]) # w2的初值为1.0
w2.requires_grad = True
b = torch.Tensor([1.0]) # b的初值为1.0
b.requires_grad = True
def forward(x):
return w1 * x **2 + w2 * x + b
def loss(x,y): # 构建计算图
y_pred = forward(x)
return (y_pred - y) ** 2
starttime = datetime.datetime.now()
print("predict (before training)", 4, forward(4).item())
for epoch in range(10000):
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 # 权重更新
w2.data = w2.data - 0.01 * w2.grad.data
b.data = b.data - 0.01 * b.grad.data
w1.grad.data.zero_() # 释放之前计算的梯度
w2.grad.data.zero_()
b.grad.data.zero_()
print('progress:',epoch,l.item())
print("predict (after training)", 4, forward(4).item()) #目标得到27
#endtime = datetime.datetime.now()
#print('程序运行时间:')
#c = (endtime - starttime).seconds
#print(c)
运行结果图:
通过增加训练的批次,能够使得结果更加的准确。
参考文章:参考文章1
参考文章2