买正品去哪个网站最好,16888精品货源入口,在深圳怎么进大公司网站,建设网站选择主机时费用最昂贵的方案是本文介绍对加载的网络的层进行增删改, 以alexnet网络为例进行介绍。
1. 加载网络
import torchvision.models as models alexnet models.alexnet(weightsmodels.AlexNet_Weights.DEFAULT)
print(alexnet)2. 删除网络
在做迁移学习的时候#xff0c;我们通常是在分类网络的…本文介绍对加载的网络的层进行增删改, 以alexnet网络为例进行介绍。
1. 加载网络
import torchvision.models as models alexnet models.alexnet(weightsmodels.AlexNet_Weights.DEFAULT)
print(alexnet)2. 删除网络
在做迁移学习的时候我们通常是在分类网络的基础上进行修改的。一般会把网络最后的几层删除掉主要是全局平均池化层、全连接层。只留前面的网络部分作为特征提取器再次基础上进行其他的任务。
2.1 删除网络任意层
将alexnet的classifier这一部分全删除掉 del alexnet.classifer
print(alexnet)删除classifer模块后打印结果如下 可以看到只剩下features和avgpool这两个模块了。刚才的classifier就已经被我们删除掉了。
删除classifier模块中的某一层
如果不想把classifier这一模块整体删除只想删除classifier中比如第6个层
# del alexnet.classifier
del alexnet.classifier[6]
print(alexnet)可以看到classifier中第6层就已经被删除掉了。
2.2 删除网络的最后多层
如果想把网络的连续几层给删除掉比如classifier中最后的几层删除掉
#------------------删除网络的最后多层--------------------------#
alexnet.classifier alexnet.classifier[:-2]
print(alexnet)
#-------------------------------------------------------------#打印信息如下
可以看出classifier看出最后2层(5,6)被删除掉了
可以使用切片的方式保留不需要被删除的层重新赋给classifier模块没有保留的就被删除了。
3. 修改网络的某一层
没有修改之前alexnet.classifier的第6层是个全连接层输入通道为4096, 输出通道为1000。 假设此时我们想最后一层全连接层的输出改为1024。此时你只需要重新定义这层全连接层。
#-----------------修改网络的某一层-----------------------------#
alexnet.classifier[6] nn.Linear(in_features4096,out_features1024)
print(alexnet)
#-------------------------------------------------------------#打印后可以看到最后一层的输出由原来的4096改为了1024
4. 在网络中添加某一层
4.1 每次添加一层
假设我们想在网络最后输出中再添加两层分别为ReLU和nn.Linear层
#-----------------修改网络的某一层-----------------------------#
alexnet.classifier[6] nn.Linear(in_features4096,out_features1024)
# print(alexnet)
#-------------------------------------------------------------##-------------网络添加层每次添加一层--------------------------#
alexnet.classifier.add_module(7,nn.ReLU(inplaceTrue))
alexnet.classifier.add_module(8,nn.Linear(in_features1024,out_features20))
print(alexnet)
#-------------------------------------------------------------#利用add_module来添加层第一个参数为层名称第二个参数为定义layer的内容我们在alexnet.classifier这个block中进行添加的添加后打印网络结构如下 可以看到成功的添加了最后2层。
4.2 一次添加多层
如果觉得一层层的添加层比较麻烦比如我们可以一次性添加一个大的模块new_block。
block nn.Sequential(nn.ReLU(inplaceTrue),nn.Linear(in_features1024,out_features20)
)alexnet.add_module(new_block,block)
print(alexnet)可以看到在alexnet网络中新增了new_block,该block中包括2层分别是ReLU层以及Linear层。
以上就是对Pytorch网络增删改的方法完整的代码如下
import torchvision.models as models
import torch.nn as nn alexnet models.alexnet(weightsmodels.AlexNet_Weights.DEFAULT)
# print(alexnet)#1.-----------------删除网络的最后一层-------------------------#
# del alexnet.classifier
# del alexnet.classifier[6]
# print(alexnet)
#-------------------------------------------------------------##------------------删除网络的最后多层--------------------------#
# alexnet.classifier alexnet.classifier[:-2]
# print(alexnet)
#-------------------------------------------------------------##-----------------修改网络的某一层-----------------------------#
alexnet.classifier[6] nn.Linear(in_features4096,out_features1024)
# print(alexnet)
#-------------------------------------------------------------##-------------网络添加层每次添加一层--------------------------#
# alexnet.classifier.add_module(7,nn.ReLU(inplaceTrue))
# alexnet.classifier.add_module(8,nn.Linear(in_features1024,out_features20))
# print(alexnet)
#-------------------------------------------------------------##-----------------------网络添加层一次性添加多层--------------#
block nn.Sequential(nn.ReLU(inplaceTrue),nn.Linear(in_features1024,out_features20)
)alexnet.add_module(new_block,block)
print(alexnet)
#-------------------------------------------------------------# 文章转载自: http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.dbrnl.cn.gov.cn.dbrnl.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.coatingonline.com.cn.gov.cn.coatingonline.com.cn http://www.morning.snrbl.cn.gov.cn.snrbl.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.yysqz.cn.gov.cn.yysqz.cn http://www.morning.bfgbz.cn.gov.cn.bfgbz.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn