原始定义方式与 nn.Sequential 两种定义方式实例:
可以看到使用torch.nn.Sequential()搭建神经网络模型非常的方便,少写很多的code
import torch import torch.nn as nn # -------------------------方式一:传统网络定义方式-------------------------------- class Net(nn.Module): def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim): super(Net, self).__init__() self.linear1 = nn.Linear(in_dim, n_hidden_1) self.Relu1 = nn.ReLU(True) self.linear2 = nn.Linear(n_hidden_1, n_hidden_2) self.Relu2 = nn.ReLU(True) self.linear3 = nn.Linear(n_hidden_2, out_dim) def forward(self, x): x = self.linear1(x) x = self.Relu1(x) x = self.linear2(x) x = self.Relu2(x) x = self.linear3(x) return x # -------------------------方式二:使用nn.Sequential定义网络------------------------ class Net(nn.Module): def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim): super(Net, self).__init__() self.layer = nn.Sequential( nn.Linear(in_dim, n_hidden_1), # (18,15) nn.ReLU(True), nn.Linear(n_hidden_1, n_hidden_2), # (15,10) nn.ReLU(True), nn.Linear(n_hidden_2, out_dim) # (10,1) ) def forward(self, x): x = self.layer(x) return x # instantiation net = Net(18, 15, 10, 1) # create random input to model input = torch.randn(30, 18) # output the predicted value predict = net(input) print(predict.size()) print(net)
torch.nn.Sequential是一个Sequential容器,模块将按照构造函数中传递的顺序添加到模块中。通俗的话说,就是根据自己的需求,把不同的函数组合成一个(小的)模块使用或者把组合的模块添加到自己的网络中。
一、第一种方式(可以配合一些条件判断语句动态添加)
模板——torch.nn.Sequential()的一个对象.add_module(name, module)。
name:某层次的名字;module:需要添加的子模块,如卷积、激活函数等等。
添加子模块到当前模块中。
可以通过 name 属性来访问添加的子模块。
输出后每一层的名字:不是采用默认的命名方式(按序号 0,1,2,3…),而是按照name属性命名!!import torch.nn as nn model = nn.Sequential() model.add_module("conv1", nn.Conv2d(1, 20, 5)) model.add_module('relu1', nn.ReLU()) model.add_module('conv2', nn.Conv2d(20, 64, 5)) model.add_module('relu2', nn.ReLU()) # 输出 Sequential( (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1)) (relu1): ReLU() (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1)) (relu2): ReLU() )
注意!!!nn.module也有add_module()对象
# 被添加的module可以通过 name 属性来获取。 import torch.nn as nn class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.add_module("conv", nn.Conv2d(10, 20, 4)) # self.conv = nn.Conv2d(10, 20, 4) 和上面这个增加module的方式等价 model = Model() print(model.conv) # 通过name属性访问添加的子模块 print(model) # 输出:注意子模块的命名方式 Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1)) Model( (conv): Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1)) )
二、第二种方式
- 模板——nn.Sequential(*module)。
- 输出的每一层的名字:采用默认的命名方式(按序号 0,1,2,3…)
import torch.nn as nn model = nn.Sequential( nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU() ) print(model) # 输出:注意命名方式 Sequential( (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1)) (1): ReLU() (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1)) (3): ReLU() )
三、第三种方式
- 模板——nn.Sequential(OrderedDict([*(name, module)]))
- 输出后每一层的名字:不是采用默认的命名方式(按序号 0,1,2,3…),而是按照name属性命名!!
import collections import torch.nn as nn model = nn.Sequential(collections.OrderedDict([('conv1', nn.Conv2d(1, 20, 5)), ('relu1', nn.ReLU()), ('conv2', nn.Conv2d(20, 64, 5)), ('relu2', nn.ReLU()) ])) print(model) # 输出:注意子模块命名方式 Sequential( (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1)) (relu1): ReLU() (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1)) (relu2): ReLU() )