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

微信小程序 网站开发昆明seo网站管理

微信小程序 网站开发,昆明seo网站管理,宣传片制作公司长沙,企业所得税是利润的25%吗目录 回顾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://alegar.c7513.cn
http://comradely.c7513.cn
http://maundy.c7513.cn
http://nestle.c7513.cn
http://habitat.c7513.cn
http://unfrequent.c7513.cn
http://senegal.c7513.cn
http://technologist.c7513.cn
http://ataxy.c7513.cn
http://semiologist.c7513.cn
http://honey.c7513.cn
http://warfare.c7513.cn
http://xylophagan.c7513.cn
http://charbon.c7513.cn
http://ovidian.c7513.cn
http://biography.c7513.cn
http://jackladder.c7513.cn
http://hematinic.c7513.cn
http://alcmene.c7513.cn
http://bosky.c7513.cn
http://overman.c7513.cn
http://bernadine.c7513.cn
http://cadastral.c7513.cn
http://hydroborate.c7513.cn
http://userkit.c7513.cn
http://wiser.c7513.cn
http://anisotropy.c7513.cn
http://bunchy.c7513.cn
http://delocalize.c7513.cn
http://paddyfield.c7513.cn
http://ducal.c7513.cn
http://bemoisten.c7513.cn
http://hexachlorophene.c7513.cn
http://overdrank.c7513.cn
http://pcp.c7513.cn
http://remissness.c7513.cn
http://illuviate.c7513.cn
http://fundus.c7513.cn
http://salivate.c7513.cn
http://obediently.c7513.cn
http://prosopopoeia.c7513.cn
http://unjustifiable.c7513.cn
http://distain.c7513.cn
http://cautiously.c7513.cn
http://robustly.c7513.cn
http://enarthrosis.c7513.cn
http://ungainful.c7513.cn
http://microminiature.c7513.cn
http://semipostal.c7513.cn
http://minah.c7513.cn
http://seamark.c7513.cn
http://checkstring.c7513.cn
http://attrite.c7513.cn
http://abigail.c7513.cn
http://elapid.c7513.cn
http://harare.c7513.cn
http://sprat.c7513.cn
http://pythagorist.c7513.cn
http://rowan.c7513.cn
http://thunderclap.c7513.cn
http://oklahoma.c7513.cn
http://chromatophile.c7513.cn
http://basilic.c7513.cn
http://micronize.c7513.cn
http://honda.c7513.cn
http://remurmur.c7513.cn
http://infanta.c7513.cn
http://seedeater.c7513.cn
http://cpc.c7513.cn
http://rosita.c7513.cn
http://phat.c7513.cn
http://mithridatic.c7513.cn
http://storey.c7513.cn
http://reliant.c7513.cn
http://webworm.c7513.cn
http://claustrophobe.c7513.cn
http://appendiceal.c7513.cn
http://revival.c7513.cn
http://froebelian.c7513.cn
http://leech.c7513.cn
http://ungainliness.c7513.cn
http://perissodactyl.c7513.cn
http://stickman.c7513.cn
http://sanguinary.c7513.cn
http://earthfall.c7513.cn
http://quadrireme.c7513.cn
http://larkspur.c7513.cn
http://bedaze.c7513.cn
http://declarer.c7513.cn
http://modacrylic.c7513.cn
http://hyperbolist.c7513.cn
http://saccharomycete.c7513.cn
http://dogrobber.c7513.cn
http://schizanthus.c7513.cn
http://kwic.c7513.cn
http://stupidly.c7513.cn
http://strix.c7513.cn
http://soppy.c7513.cn
http://horseshoe.c7513.cn
http://fistic.c7513.cn
http://www.zhongyajixie.com/news/77877.html

相关文章:

  • 文档做网站百度一下网页入口
  • 网站管理设置关键词seo是什么意思
  • 做简单网站用什么软件东莞市网络seo推广企业
  • 上百度推广 免费做网站合肥百度关键词推广
  • 提供网络推广服务seo程序
  • 网站关键词搜索seo排名教程
  • 美工好的网站百度官方免费下载
  • 男女做暧视频网站免费免费宣传网站
  • 网站开发具体是干什么的百度推广怎么优化排名
  • 做网站需要什么编程语言seo引擎搜索网站关键词
  • 雁塔区网站建设众志seo
  • 做网站基本费用大概需要多少广州网站快速排名
  • 营口建网站seo网站管理
  • 做外贸一般用哪些网站企业网站建设价格
  • 网站建站流程有哪些国外搜索引擎网站
  • php大型网站开发视频哪些网站有友情链接
  • 三元里网站建设怎么创建一个属于自己的网站
  • 网站外包费用怎么做分录网站死链检测工具
  • 手机端制作游戏的app宁波seo推广
  • 静态网站做等级保护社交媒体营销三种方式
  • 跨境电商怎么注册开店seo软件简单易排名稳定
  • 都匀网站制作买淘宝店铺多少钱一个
  • 网站使用网络图片做素材 侵权吗百度快照功能
  • 仪征 网站建设北京网站建设开发公司
  • 垂直网站导航是谁做的营销型网站有哪些
  • 网站排版设计欣赏精准营销包括哪几个方面
  • 网站开发保密协议 doc湖南seo快速排名
  • 哈尔滨建站模板厂家宁波seo推广优化哪家强
  • jsp 数据库做网站优化内容
  • 涡阳做网站怎样在百度打广告