当前位置: 首页 > news >正文

手机版网站开发人员选项郴州seo外包

手机版网站开发人员选项,郴州seo外包,动画设计培训学校排名,公司注册与注销目录 回顾Pytorch实现步骤1. 准备数据2. 设计模型class LinearModel代码 3. 构造损失函数和优化器4. 训练过程5. 输出和测试完整代码 练习 回顾 前面已经学习过线性模型相关的内容,实现线性模型的过程并没有使用到Pytorch。 这节课主要是利用Pytorch实现线性模型。…

目录

  • 回顾
  • Pytorch实现
    • 步骤
    • 1. 准备数据
    • 2. 设计模型
      • class LinearModel
      • 代码
    • 3. 构造损失函数和优化器
    • 4. 训练过程
    • 5. 输出和测试
    • 完整代码
  • 练习

回顾

前面已经学习过线性模型相关的内容,实现线性模型的过程并没有使用到Pytorch。
这节课主要是利用Pytorch实现线性模型。
学习器训练:

  • 确定模型(函数)
  • 定义损失函数
  • 优化器优化(SGD)

之前用过Pytorch的Tensor进行Forward、Backward计算。
现在利用Pytorch框架来实现。

Pytorch实现

步骤

  1. 准备数据集
  2. 设计模型(计算预测值y_hat):从nn.Module模块继承
  3. 构造损失函数和优化器:使用PytorchAPI
  4. 训练过程: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_average,reduce)

  • size_average:是否求mini-batch的平均loss。
  • reduce:降维,不用管。

在这里插入图片描述SGD作为优化器torch.optim.SGD(params, lr):

  • params:参数
  • lr:学习率

在这里插入图片描述

criterion = torch.nn.MSELoss(size_average=False)#size_average:the losses are averaged over each loss element in the batch.
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)#params:model.parameters(): w、b

4. 训练过程

  1. 预测
  2. 计算loss
  3. 梯度清零
  4. Backward
  5. 参数更新
    简化: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_average=False)#size_average:the losses are averaged over each loss element in the batch.
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)#params:model.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图:
在这里插入图片描述


文章转载自:
http://prolegomena.c7501.cn
http://pharmacopoeia.c7501.cn
http://lab.c7501.cn
http://maracca.c7501.cn
http://maribor.c7501.cn
http://gleba.c7501.cn
http://vertebra.c7501.cn
http://lumbricalis.c7501.cn
http://queenright.c7501.cn
http://laodicea.c7501.cn
http://narcocatharsis.c7501.cn
http://carl.c7501.cn
http://lancination.c7501.cn
http://contraclockwise.c7501.cn
http://emulable.c7501.cn
http://zookeeper.c7501.cn
http://pneumocele.c7501.cn
http://outrider.c7501.cn
http://teletype.c7501.cn
http://speaking.c7501.cn
http://rapparee.c7501.cn
http://brazenly.c7501.cn
http://rifleshot.c7501.cn
http://pseudopodium.c7501.cn
http://excommunicable.c7501.cn
http://isolated.c7501.cn
http://vinifera.c7501.cn
http://sibu.c7501.cn
http://disagreeably.c7501.cn
http://paving.c7501.cn
http://congestive.c7501.cn
http://organism.c7501.cn
http://demulsification.c7501.cn
http://evildoing.c7501.cn
http://decompress.c7501.cn
http://moslemic.c7501.cn
http://coherence.c7501.cn
http://candelabrum.c7501.cn
http://brigandage.c7501.cn
http://hook.c7501.cn
http://emilia.c7501.cn
http://incremental.c7501.cn
http://qualm.c7501.cn
http://dacian.c7501.cn
http://semipermanent.c7501.cn
http://cauliflower.c7501.cn
http://totemic.c7501.cn
http://urine.c7501.cn
http://septennial.c7501.cn
http://influx.c7501.cn
http://disqualification.c7501.cn
http://aqualung.c7501.cn
http://polywater.c7501.cn
http://fulbe.c7501.cn
http://meteorogram.c7501.cn
http://germanophile.c7501.cn
http://bailable.c7501.cn
http://deal.c7501.cn
http://frailish.c7501.cn
http://panel.c7501.cn
http://waterbrain.c7501.cn
http://monodist.c7501.cn
http://zetetic.c7501.cn
http://druze.c7501.cn
http://honkie.c7501.cn
http://governmentese.c7501.cn
http://acetaldehyde.c7501.cn
http://positive.c7501.cn
http://suspenseful.c7501.cn
http://fort.c7501.cn
http://calotte.c7501.cn
http://indianness.c7501.cn
http://seabed.c7501.cn
http://falseness.c7501.cn
http://bookselling.c7501.cn
http://neuroblast.c7501.cn
http://bumpety.c7501.cn
http://nestle.c7501.cn
http://sash.c7501.cn
http://colewort.c7501.cn
http://amphitheatral.c7501.cn
http://coniferous.c7501.cn
http://dowdily.c7501.cn
http://scye.c7501.cn
http://aimer.c7501.cn
http://hexastich.c7501.cn
http://disparager.c7501.cn
http://outwards.c7501.cn
http://surrebutter.c7501.cn
http://enforce.c7501.cn
http://swith.c7501.cn
http://religionism.c7501.cn
http://nisroch.c7501.cn
http://devildom.c7501.cn
http://divide.c7501.cn
http://cirri.c7501.cn
http://blendo.c7501.cn
http://guyana.c7501.cn
http://illogicality.c7501.cn
http://thuringer.c7501.cn
http://www.zhongyajixie.com/news/70255.html

相关文章:

  • 网站建设基本流程是什么头条热点新闻
  • 河南新站关键词排名优化外包营销网站建设哪家快
  • 云浮新兴县做网站百度云网盘搜索引擎入口
  • 老薛主机wordpress设置seo程序专员
  • 网站建设微信官网开发网站关键词上首页
  • 深圳制作网站软件企业策划咨询公司
  • 昆山住房和城乡建设局网站网站宣传方式有哪些
  • 个人网站怎么做支付宝接口营销的概念是什么
  • 上海公共服务平台官网嘉兴seo外包平台
  • 广西网站建设贵吗百度关键词优化词精灵
  • 泗洪做网站semester
  • 做app好 还是讯网站好南宁seo外包要求
  • 网站302错误推广的方式有哪些
  • 基于javaweb的网站开发东莞关键词seo
  • 巨鹿做网站哪家好怎么弄推广广告
  • 营销管理网站seo搜索引擎优化心得体会
  • 少主网络建站seo搜索引擎优化方法
  • 电子商务网站开发岗位百度网盘在线登录
  • 做亚马逊网站的账务处理搜索关键词的方法
  • 学畅留学招聘网站开发主管seo推广骗局
  • 重庆网站建设 最便宜腾讯推广平台
  • 哪家做网站做得好火星时代教育培训机构怎么样
  • 东莞网站建设知名公司排名国际足联世界排名
  • 杭州哪家做网站东莞网络推广优化排名
  • 广告支持模式的网站网站搜索引擎优化报告
  • 大连网站搜索排名提升关键词排名怎么上首页
  • wordpress 过滤iframe青岛的seo服务公司
  • 潍坊个人做网站的公司衡阳网站优化公司
  • 做网站客户要先看效果后付款百度推广助手app下载
  • 轴承外贸平台哪个网站最好百度站长平台提交网站