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博客

相关内容

热门资讯

办法辅助!宁夏划水辅助(辅助)... 办法辅助!宁夏划水辅助(辅助)果然一直都是有辅助教程(哔哩哔哩)运宁夏划水辅助辅助工具,进入游戏界面...
第7分钟辅助!wpk辅助软件,... 第7分钟辅助!wpk辅助软件,约局吧怎么看有没有挂(有挂教程)-哔哩哔哩1、完成约局吧怎么看有没有挂...
指引辅助!微信小程序哥哥打大a... 指引辅助!微信小程序哥哥打大a辅助器(辅助)切实真的是有辅助工具(哔哩哔哩)1、这是跨平台的微信小程...
九分钟辅助!wepoker私人... 九分钟辅助!wepoker私人局辅助器怎么用,wepokerh5破解(存在有挂)-哔哩哔哩1)wep...
4分钟辅助!wepoker有没... 4分钟辅助!wepoker有没有机器人,有哪些免费的wpk透视挂码(详细教程)-哔哩哔哩小薇(辅助器...
大纲辅助!新悠悠辅助微信(辅助... 大纲辅助!新悠悠辅助微信(辅助)一贯是有辅助软件(哔哩哔哩)1、新悠悠辅助微信透视辅助软件激活码多个...
9分钟辅助!aapoker怎么... 9分钟辅助!aapoker怎么控制牌,wepoker俱乐部辅助(有挂总结)-哔哩哔哩1、这是跨平台的...
妙招辅助!欢乐对决合作辅助(辅... 妙招辅助!欢乐对决合作辅助(辅助)原来真的有辅助神器(哔哩哔哩)1、在欢乐对决合作辅助插件功能辅助器...
四分钟辅助!xpoker辅助怎... 四分钟辅助!xpoker辅助怎么用,we poker游戏下(有挂技巧)-哔哩哔哩1、该软件可以轻松地...
经验辅助!werplan外开挂... 经验辅助!werplan外开挂(辅助)一贯一直都是有辅助app(哔哩哔哩)1、下载好werplan外...