监控网站模版,企业商务网站设计与开发,威海seo网站推广,tp钱包下载目录 回顾Pytorch实现步骤1. 准备数据2. 设计模型class LinearModel代码 3. 构造损失函数和优化器4. 训练过程5. 输出和测试完整代码 练习 回顾
前面已经学习过线性模型相关的内容#xff0c;实现线性模型的过程并没有使用到Pytorch。 这节课主要是利用Pytorch实现线性模型。… 目录 回顾Pytorch实现步骤1. 准备数据2. 设计模型class LinearModel代码 3. 构造损失函数和优化器4. 训练过程5. 输出和测试完整代码 练习 回顾
前面已经学习过线性模型相关的内容实现线性模型的过程并没有使用到Pytorch。 这节课主要是利用Pytorch实现线性模型。 学习器训练
确定模型函数定义损失函数优化器优化SGD
之前用过Pytorch的Tensor进行Forward、Backward计算。 现在利用Pytorch框架来实现。
Pytorch实现
步骤
准备数据集设计模型计算预测值y_hat:从nn.Module模块继承构造损失函数和优化器使用PytorchAPI训练过程Forward、Backward、update
1. 准备数据
在PyTorch中计算图是通过mini-batch形式进行所以X、Y都是多维的Tensor。
import torch
x_data torch.Tensor([[1.0], [2.0], [3.0]])
y_data torch.Tensor([[2.0], [4.0], [6.0]])2. 设计模型
在之前讲解梯度下降算法时我们需要自己计算出梯度然后更新权重。 而使用Pytorch构造模型重点时在构建计算图和损失函数上。
class LinearModel
通过构造一个 class LinearModel类来实现所有的模型类都需要继承nn.Module,这是所有神经网络模块的基础类。 class LinearModel这种定义的模型类必须包含两个部分
init()构造函数进行初始化。 def __init__(self):super(LinearModel, self).__init__()#调用父类构造函数不用管照着写。# torch.nn.Linear(in_featuers, in_featuers)构造Linear类的对象其实就是实现了一个线性单元self.linear torch.nn.Linear(1, 1)forward():进行前馈计算 backward没有被写是因为在这种模型类里面会自动实现 Class nn.Linear 实现了magic method call()它使类的实例可以像函数一样被调用。通常会调用forward()。 def forward(self, x):y_pred self.linear(x)#调用linear对象输入x进行预测return y_pred代码
class LinearModel(torch.nn.Module):def __init__(self):super(LinearModel, self).__init__()#调用父类构造函数不用管照着写。# torch.nn.Linear(in_featuers, in_featuers)构造Linear类的对象其实就是实现了一个线性单元self.linear torch.nn.Linear(1, 1)def forward(self, x):y_pred self.linear(x)#调用linear对象输入x进行预测return y_predmodel LinearModel()#实例化LinearModel()3. 构造损失函数和优化器
采用MSE作为损失函数
torch.nn.MSELoss(size_averagereduce)
size_average是否求mini-batch的平均loss。reduce降维不用管。
SGD作为优化器torch.optim.SGD(params, lr)
params参数lr学习率 criterion torch.nn.MSELoss(size_averageFalse)#size_averagethe losses are averaged over each loss element in the batch.
optimizer torch.optim.SGD(model.parameters(), lr0.01)#paramsmodel.parameters() w、b4. 训练过程
预测计算loss梯度清零Backward参数更新 简化Forward–Backward–更新
#4. Training Cycle
for epoch in range(100):y_pred model(x_data)#Forward预测loss criterion(y_pred, y_data)#Forward计算lossprint(epoch, loss)optimizer.zero_grad()#梯度清零loss.backward()#backward计算梯度optimizer.step()#通过step函数进行参数更新5. 输出和测试
# Output weight and bias
print(w , model.linear.weight.item())
print(b , model.linear.bias.item())# Test Model
x_test torch.Tensor([[4.0]])
y_test model(x_test)
print(y_pred , y_test.data)完整代码
import torch
#1. Prepare dataset
x_data torch.Tensor([[1.0], [2.0], [3.0]])
y_data torch.Tensor([[2.0], [4.0], [6.0]])#2. Design Model
class LinearModel(torch.nn.Module):def __init__(self):super(LinearModel, self).__init__()#调用父类构造函数不用管照着写。# torch.nn.Linear(in_featuers, in_featuers)构造Linear类的对象其实就是实现了一个线性单元self.linear torch.nn.Linear(1, 1)def forward(self, x):y_pred self.linear(x)#调用linear对象输入x进行预测return y_predmodel LinearModel()#实例化LinearModel()# 3. Construct Loss and Optimize
criterion torch.nn.MSELoss(size_averageFalse)#size_averagethe losses are averaged over each loss element in the batch.
optimizer torch.optim.SGD(model.parameters(), lr0.01)#paramsmodel.parameters() w、b#4. Training Cycle
for epoch in range(100):y_pred model(x_data)#Forward预测loss criterion(y_pred, y_data)#Forward计算lossprint(epoch, loss)optimizer.zero_grad()#梯度清零loss.backward()#backward计算梯度optimizer.step()#通过step函数进行参数更新# Output weight and bias
print(w , model.linear.weight.item())
print(b , model.linear.bias.item())# Test Model
x_test torch.Tensor([[4.0]])
y_test model(x_test)
print(y_pred , y_test.data)输出结果 85 tensor(0.2294, grad_fn) 86 tensor(0.2261, grad_fn) 87 tensor(0.2228, grad_fn) 88 tensor(0.2196, grad_fn) 89 tensor(0.2165, grad_fn) 90 tensor(0.2134, grad_fn) 91 tensor(0.2103, grad_fn) 92 tensor(0.2073, grad_fn) 93 tensor(0.2043, grad_fn) 94 tensor(0.2014, grad_fn) 95 tensor(0.1985, grad_fn) 96 tensor(0.1956, grad_fn) 97 tensor(0.1928, grad_fn) 98 tensor(0.1900, grad_fn) 99 tensor(0.1873, grad_fn) w 1.711882472038269 b 0.654958963394165 y_pred tensor([[7.5025]]) 可以看到误差还比较大可以增加训练轮次训练1000次后的结果 980 tensor(2.1981e-07, grad_fn) 981 tensor(2.1671e-07, grad_fn) 982 tensor(2.1329e-07, grad_fn) 983 tensor(2.1032e-07, grad_fn) 984 tensor(2.0737e-07, grad_fn) 985 tensor(2.0420e-07, grad_fn) 986 tensor(2.0143e-07, grad_fn) 987 tensor(1.9854e-07, grad_fn) 988 tensor(1.9565e-07, grad_fn) 989 tensor(1.9260e-07, grad_fn) 990 tensor(1.8995e-07, grad_fn) 991 tensor(1.8728e-07, grad_fn) 992 tensor(1.8464e-07, grad_fn) 993 tensor(1.8188e-07, grad_fn) 994 tensor(1.7924e-07, grad_fn) 995 tensor(1.7669e-07, grad_fn) 996 tensor(1.7435e-07, grad_fn) 997 tensor(1.7181e-07, grad_fn) 998 tensor(1.6931e-07, grad_fn) 999 tensor(1.6700e-07, grad_fn) w 1.9997280836105347 b 0.0006181497010402381 y_pred tensor([[7.9995]]) 练习
用以下这些优化器替换SGD得到训练结果并画出损失曲线图。 比如说Adam的loss图