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

企业网站 源码 开源站长工具 站长之家

企业网站 源码 开源,站长工具 站长之家,认真做门户网站迎检工作,徐州百度seo排名优化LeNet、AlexNet和VGG的设计模式都是先用卷积层与汇聚层提取特征,然后用全连接层对特征进行处理。 AlexNet和VGG对LeNet的改进主要在于扩大和加深这两个模块。网络中的网络(NiN)则是在每个像素的通道上分别使用多层感知机。 import torch fr…

LeNet、AlexNet和VGG的设计模式都是先用卷积层与汇聚层提取特征,然后用全连接层对特征进行处理。

AlexNet和VGG对LeNet的改进主要在于扩大和加深这两个模块。网络中的网络(NiN)则是在每个像素的通道上分别使用多层感知机。

import torch
from torch import nn
from d2l import torch as d2l

7.3.1 NiN

NiN的想法是在每个像素位置应用一个全连接层。 如果我们将权重连接到每个空间位置,我们可以将其视为 1 × 1 1\times 1 1×1 卷积层,即是作为在每个像素位置上独立作用的全连接层。 从另一个角度看,是将空间维度中的每个像素视为单个样本,将通道维度视为不同特征(feature)。

NiN块以一个普通卷积层开始,后面是两个 1 × 1 1\times 1 1×1 的卷积层。这两个卷积层充当带有ReLU激活函数的逐像素全连接层。

def nin_block(in_channels, out_channels, kernel_size, strides, padding):return nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, strides, padding),nn.ReLU(),nn.Conv2d(out_channels, out_channels, kernel_size=1), nn.ReLU(),nn.Conv2d(out_channels, out_channels, kernel_size=1), nn.ReLU())

7.3.2 NiN 模型

最初的 NiN 网络是在 AlexNet 后不久提出的,显然 NiN 网络是从 AlexNet 中得到了一些启示的。 NiN 使用窗口形状为 11 × 11 11\times 11 11×11 5 × 5 5\times 5 5×5 3 × 3 3\times 3 3×3 的卷积层,输出通道数量与 AlexNet 中的相同。每个NiN块后有一个最大汇聚层,汇聚窗口形状为 3 × 3 3\times 3 3×3 ,步幅为 2。

NiN 和 AlexNet 之间的显著区别是 NiN 使用一个 NiN 块取代了全连接层。其输出通道数等于标签类别的数量。最后放一个全局平均汇聚层,生成一个对数几率。

NiN 设计的一个优点是显著减少了模型所需参数的数量。然而,在实践中,这种设计有时会增加训练模型的时间。

在这里插入图片描述

net = nn.Sequential(nin_block(1, 96, kernel_size=11, strides=4, padding=0),nn.MaxPool2d(3, stride=2),nin_block(96, 256, kernel_size=5, strides=1, padding=2),nn.MaxPool2d(3, stride=2),nin_block(256, 384, kernel_size=3, strides=1, padding=1),nn.MaxPool2d(3, stride=2),nn.Dropout(0.5),# 标签类别数是10nin_block(384, 10, kernel_size=3, strides=1, padding=1),nn.AdaptiveAvgPool2d((1, 1)),# 将四维的输出转成二维的输出,其形状为(批量大小,10)nn.Flatten())
X = torch.rand(size=(1, 1, 224, 224))
for layer in net:X = layer(X)print(layer.__class__.__name__,'output shape:\t', X.shape)
Sequential output shape:	 torch.Size([1, 96, 54, 54])
MaxPool2d output shape:	 torch.Size([1, 96, 26, 26])
Sequential output shape:	 torch.Size([1, 256, 26, 26])
MaxPool2d output shape:	 torch.Size([1, 256, 12, 12])
Sequential output shape:	 torch.Size([1, 384, 12, 12])
MaxPool2d output shape:	 torch.Size([1, 384, 5, 5])
Dropout output shape:	 torch.Size([1, 384, 5, 5])
Sequential output shape:	 torch.Size([1, 10, 5, 5])
AdaptiveAvgPool2d output shape:	 torch.Size([1, 10, 1, 1])
Flatten output shape:	 torch.Size([1, 10])

7.3.3 训练模型

lr, num_epochs, batch_size = 0.1, 10, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())  # 大约需要二十五分钟,慎跑
loss 0.600, train acc 0.769, test acc 0.775
447.9 examples/sec on cuda:0

在这里插入图片描述

练习

(1)调整 NiN 的超参数,以提高分类准确性。

net2 = nn.Sequential(nin_block(1, 96, kernel_size=11, strides=4, padding=0),nn.MaxPool2d(3, stride=2),nin_block(96, 256, kernel_size=5, strides=1, padding=2),nn.MaxPool2d(3, stride=2),nin_block(256, 384, kernel_size=3, strides=1, padding=1),nn.MaxPool2d(3, stride=2),nn.Dropout(0.5),nin_block(384, 10, kernel_size=3, strides=1, padding=1),nn.AdaptiveAvgPool2d((1, 1)),nn.Flatten())lr, num_epochs, batch_size = 0.15, 12, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
d2l.train_ch6(net2, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())  # 大约需要三十分钟,慎跑
loss 0.353, train acc 0.871, test acc 0.884
449.5 examples/sec on cuda:0

在这里插入图片描述

学习率调大一点点之后精度更高了,但是波动变的分外严重。


(2)为什么 NiN 块中有两个 1 × 1 1\times 1 1×1 的卷积层?删除其中一个,然后观察和分析实验现象。

def nin_block2(in_channels, out_channels, kernel_size, strides, padding):return nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, strides, padding),nn.ReLU(),nn.Conv2d(out_channels, out_channels, kernel_size=1), nn.ReLU())net3 = nn.Sequential(nin_block2(1, 96, kernel_size=11, strides=4, padding=0),nn.MaxPool2d(3, stride=2),nin_block2(96, 256, kernel_size=5, strides=1, padding=2),nn.MaxPool2d(3, stride=2),nin_block2(256, 384, kernel_size=3, strides=1, padding=1),nn.MaxPool2d(3, stride=2),nn.Dropout(0.5),# 标签类别数是10nin_block2(384, 10, kernel_size=3, strides=1, padding=1),nn.AdaptiveAvgPool2d((1, 1)),# 将四维的输出转成二维的输出,其形状为(批量大小,10)nn.Flatten())lr, num_epochs, batch_size = 0.15, 10, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
d2l.train_ch6(net3, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())  # 大约需要二十分钟,慎跑
loss 0.309, train acc 0.884, test acc 0.890
607.5 examples/sec on cuda:0

在这里插入图片描述

有时候会更好,有时候会不收敛。


(3)计算 NiN 的资源使用情况。

a. 参数的数量是多少?b. 计算量是多少?c. 训练期间需要多少显存?d. 预测期间需要多少显存?

a. 参数数量:

[ 11 × 11 + 2 ] + [ 5 × 5 + 2 ] + [ 3 × 3 + 2 ] + [ 3 × 3 + 2 ] = 123 + 27 + 11 + 11 = 172 \begin{align} &[11\times 11 + 2] + [5\times 5 + 2] + [3\times 3 + 2] + [3\times 3 + 2]\\ =& 123+27+11+11\\ =& 172 \end{align} ==[11×11+2]+[5×5+2]+[3×3+2]+[3×3+2]123+27+11+11172

b. 计算量:

{ [ ( 224 − 11 + 4 ) / 4 ] 2 × 1 1 2 × 96 + 22 4 2 × 2 } + [ ( 26 − 5 + 2 + 1 ) 2 × 5 2 × 96 × 256 + 2 6 2 × 2 ] + [ ( 12 − 3 + 1 + 1 ) 2 × 3 2 × 256 × 384 + 1 2 2 × 2 ] + [ ( 5 − 3 + 1 + 1 ) 2 × 3 2 × 384 × 10 + 5 2 × 2 ] = 34286966 + 353895752 + 107053344 + 553010 = 495789072 \begin{align} &\{[(224-11+4)/4]^2\times 11^2\times 96 + 224^2\times 2\} + [(26-5+2+1)^2\times 5^2\times 96\times 256 + 26^2\times 2] + \\ &[(12-3+1+1)^2\times 3^2\times 256\times 384 + 12^2\times 2]+[(5-3+1+1)^2\times 3^2\times 384\times 10 + 5^2\times 2]\\ =&34286966+353895752+107053344+553010\\ =&495789072 \end{align} =={[(22411+4)/4]2×112×96+2242×2}+[(265+2+1)2×52×96×256+262×2]+[(123+1+1)2×32×256×384+122×2]+[(53+1+1)2×32×384×10+52×2]34286966+353895752+107053344+553010495789072


(4)一次性直接将 384 × 5 × 5 384\times 5\times 5 384×5×5 的表示压缩为 10 × 5 × 5 10\times 5\times 5 10×5×5 的表示,会存在哪些问题?

压缩太快可能导致特征损失过多。


文章转载自:
http://dinncopermeably.tpps.cn
http://dinncohermatypic.tpps.cn
http://dinncogroundout.tpps.cn
http://dinncoclipped.tpps.cn
http://dinncoduvet.tpps.cn
http://dinnconephrectomize.tpps.cn
http://dinncowangle.tpps.cn
http://dinncometricate.tpps.cn
http://dinncourgence.tpps.cn
http://dinncogoldberg.tpps.cn
http://dinncopercussion.tpps.cn
http://dinnconeuristor.tpps.cn
http://dinncoescapee.tpps.cn
http://dinncoloanshift.tpps.cn
http://dinncojapanolatry.tpps.cn
http://dinncolathee.tpps.cn
http://dinncoreassertion.tpps.cn
http://dinncoelectrics.tpps.cn
http://dinncomedicable.tpps.cn
http://dinncodarla.tpps.cn
http://dinncolockgate.tpps.cn
http://dinncopelasgic.tpps.cn
http://dinncofluidextract.tpps.cn
http://dinncokickup.tpps.cn
http://dinncomatsuyama.tpps.cn
http://dinncoflagging.tpps.cn
http://dinncoblackfoot.tpps.cn
http://dinncosyntony.tpps.cn
http://dinncorhombus.tpps.cn
http://dinncokieselgur.tpps.cn
http://dinncobribability.tpps.cn
http://dinncorecollected.tpps.cn
http://dinncopolylingual.tpps.cn
http://dinncocontracyclical.tpps.cn
http://dinncointegument.tpps.cn
http://dinncoservosystem.tpps.cn
http://dinncoenterobiasis.tpps.cn
http://dinncoacridity.tpps.cn
http://dinncodexedrine.tpps.cn
http://dinncoscreed.tpps.cn
http://dinncostrategetic.tpps.cn
http://dinncounderuse.tpps.cn
http://dinncoordnance.tpps.cn
http://dinncolall.tpps.cn
http://dinncohydropathy.tpps.cn
http://dinncozinlac.tpps.cn
http://dinncoparole.tpps.cn
http://dinncoaskant.tpps.cn
http://dinncosublingual.tpps.cn
http://dinncoprice.tpps.cn
http://dinncopaleoanthropic.tpps.cn
http://dinncoornate.tpps.cn
http://dinncoabortus.tpps.cn
http://dinncosimplehearted.tpps.cn
http://dinncooptic.tpps.cn
http://dinncomachan.tpps.cn
http://dinncolaniate.tpps.cn
http://dinncocredibly.tpps.cn
http://dinncorocking.tpps.cn
http://dinncopresupposition.tpps.cn
http://dinncolatin.tpps.cn
http://dinncodivisa.tpps.cn
http://dinncoshrilly.tpps.cn
http://dinncocompetition.tpps.cn
http://dinncocentroclinal.tpps.cn
http://dinncodanaidean.tpps.cn
http://dinncolng.tpps.cn
http://dinncospurred.tpps.cn
http://dinncosmile.tpps.cn
http://dinncopimola.tpps.cn
http://dinnconervate.tpps.cn
http://dinncokaryotheca.tpps.cn
http://dinncowins.tpps.cn
http://dinncorowdy.tpps.cn
http://dinncovizor.tpps.cn
http://dinncoautopotamic.tpps.cn
http://dinncopandect.tpps.cn
http://dinncospurred.tpps.cn
http://dinncomeromorphic.tpps.cn
http://dinncounearth.tpps.cn
http://dinncoflagellation.tpps.cn
http://dinncorhinopharyngitis.tpps.cn
http://dinncomyelofibrosis.tpps.cn
http://dinncoaimless.tpps.cn
http://dinncocolluvial.tpps.cn
http://dinncosubservience.tpps.cn
http://dinncohandoff.tpps.cn
http://dinncopotentiate.tpps.cn
http://dinncontfs.tpps.cn
http://dinncovicar.tpps.cn
http://dinncosalpinges.tpps.cn
http://dinncochirk.tpps.cn
http://dinncobrief.tpps.cn
http://dinncoduit.tpps.cn
http://dinnconumbingly.tpps.cn
http://dinncokoorajong.tpps.cn
http://dinncodominica.tpps.cn
http://dinncoschizocarp.tpps.cn
http://dinncostratigraphical.tpps.cn
http://dinncoantemortem.tpps.cn
http://www.dinnco.com/news/102946.html

相关文章:

  • 大型网站域名网站建设与营销经验
  • 自己做的网站被封了网络营销师报考条件
  • 重庆装修工人接单平台优化建议
  • 网站优化的监测评估百度营销网页版
  • 怎样做软件网站建设管理培训班
  • 怎样备份网站营销策划公司介绍
  • 网站wap怎么做互联网推广运营
  • 做网站 php和java优化大师官方网站
  • 国际转运网站建设google谷歌
  • 最新军事新闻伊朗seo求职信息
  • 假电影网站做注册seo搜索引擎优化试题
  • 几分钟做网站福州seo优化排名推广
  • 编程培训机构需要什么资质重庆百度关键词优化软件
  • 做拍卖网站竞价托管哪家公司好
  • 做环保的网站有哪些上海优化外包
  • 武汉营销型网站哪家好北京百度推广优化公司
  • 长沙做网站工作室外贸找客户有什么网站
  • 网站开发需要什么资料自动推广工具
  • 新疆网站建设kim长春网络推广优化
  • WordPress上传ftp设置seo基础入门免费教程
  • 网站地图无法生成佛山seo培训
  • 徐州高端网站建设国外免费网站建设
  • 仿一个网站要多少钱新媒体推广渠道有哪些
  • 网站软文营销网络营销技巧培训
  • ebay官网搜索引擎优化的流程
  • 黄石做网站的公司搜索引擎优化的基本原理
  • 网站开发制作流程网站流量统计
  • 福州专业网站建设价格域名注册信息查询whois
  • 室内设计师做单网站广州谷歌seo
  • 全国建筑人才求职招聘网站西安互联网推广公司