Pytorch中nn.Sequential()函数创建网络的几种方法
创始人
2025-01-11 08:36:00
0

1. 创作灵感

在创建大型网络的时候,如果使用nn.Sequential()将几个有紧密联系的运算组成一个序列,可以使网络的结构更加清晰。

2.应用举例

为了记录nn.Sequential()的用法,搭建以下测试网络:

2.1 方法一

把网络分成3个Sequential序列,分别实现:第一个是卷积序列,第二个是铺平成一维的操作,第3个包含了两个线性层。

 class TestNet(nn.Module):     def __init__(self, in_channels, mid_channels):         super(TestNet,self).__init__()         self.conv = nn.Sequential(             nn.Conv2d(in_channels,in_channels*2,kernel_size=3,stride=2,padding=1),             nn.BatchNorm2d(in_channels*2),             nn.ReLU(inplace=True),             nn.Conv2d(in_channels*2,in_channels*4,kernel_size=3,stride=2,padding=1),             nn.BatchNorm2d(in_channels*4),             nn.ReLU(inplace=True)         )         self.flat = nn.Flatten()         self.linaer = nn.Sequential(             nn.Linear(49*4, 64),             nn.Linear(64, 10),             nn.Linear(10, 1)         )      def forward(self,x):         x = self.conv(x)         print(x.shape)         x = self.flat(x)         print(x.shape)         x = self.linaer(x)         print(x.shape)         return x 

运行结果:

从维度上判断,网络符合预期。

2.2  方法二

第二种方法,在网络结构比较复杂且重复的单元比较多,为了自动化生成网络,通常会先定义一个列表,在列表中添加网络,再使用nn.Sequential()。

使用方法二所需要的代码如下:

class TestNet2(nn.Module):     def __init__(self, in_channels):         super(TestNet2,self).__init__()         layer1 = []         layer2 = []         layer1.append(nn.Conv2d(in_channels,in_channels*2,kernel_size=3,stride=2,padding=1))         layer1.append(nn.BatchNorm2d(in_channels*2))         layer1.append(nn.ReLU(inplace=True))         layer1.append(nn.Conv2d(in_channels*2,in_channels*4,kernel_size=3,stride=2,padding=1))         layer1.append(nn.BatchNorm2d(in_channels*4))         layer1.append(nn.ReLU(inplace=True))         self.conv = nn.Sequential(*layer1)         self.flat = nn.Flatten()         layer2.append(nn.Linear(49*4, 64))         layer2.append(nn.Linear(64, 10))         layer2.append(nn.Linear(10, 1))         self.linaer = nn.Sequential(*layer2)      def forward(self,x):         x = self.conv(x)         print(x.shape)         x = self.flat(x)         print(x.shape)         x = self.linaer(x)         print(x.shape)         return x

运行结果如下:

与第一种方法的结果相同。

参考文献:

nn.Sequential、nn.ModuleList、nn.ModuleDict区别及使用技巧-CSDN博客

相关内容

热门资讯

2分钟透视“aapoker脚本... 2分钟透视“aapoker脚本”wepoker-h5下载(解谜开挂作弊辅助神器)1、金币登录送、破产...
7分钟了解“宝宝浙江游戏开挂器... 7分钟了解“宝宝浙江游戏开挂器”专业开挂作弊辅助插件-一贯真的是有挂进入游戏-大厅左侧-新手福利-激...
5分钟透视“德扑圈有透视”aa... 5分钟透视“德扑圈有透视”aapoker透视插件(分享开挂作弊辅助插件)1、让任何用户在无需aapo...
4分钟了解“微信小程序途游辅助... 4分钟了解“微信小程序途游辅助免费”分析开挂作弊辅助挂-竟然是有挂1、操作简单,无需注册,只需要使用...
7分钟了解“一起宁德钓蟹技巧”... 7分钟了解“一起宁德钓蟹技巧”普及开挂作弊辅助软件-总是是真的挂1、一起宁德钓蟹技巧ai辅助优化,一...
第四分钟了解“新芒果监利开机贴... 第四分钟了解“新芒果监利开机贴吧”安装开挂作弊辅助器-原来有挂1、进入游戏-大厅左侧-新手福利-激活...
2分钟了解“广东雀神挂件脚本开... 2分钟了解“广东雀神挂件脚本开挂”专业开挂作弊辅助神器-确实是有挂1、很好的工具软件,可以解锁游戏的...
第六分钟了解“余干小程序中至五... 第六分钟了解“余干小程序中至五十j有挂装”普及开挂作弊辅助挂-好像是有挂1、下载好余干小程序中至五十...
2分钟了解“天天互娱网络科技”... 2分钟了解“天天互娱网络科技”分析开挂作弊辅助神器-本来真的是有挂该软件可以轻松地帮助玩家将天天互娱...
第三分钟了解“乐乐围棋入门辅助... 第三分钟了解“乐乐围棋入门辅助器”专业开挂作弊辅助软件-本来是有挂1、金币登录送、破产送、升级送、活...