nn.Sequential()和nn.ModuleList()

对于cnn前馈神经网络如果前馈一次写一个forward函数会有些麻烦,在此就有两种简化方式,ModuleList和Sequential。

Sequential

1 模型建立方式

(1)nn.Sequential()对象.add_module(层名,层class的实例)

net1 = nn.Sequential()

net1.add_module('conv', nn.Conv2d(3, 3, 3))

net1.add_module('batchnorm', nn.BatchNorm2d(3))

net1.add_module('activation_layer', nn.ReLU())

(2)nn.Sequential(*多个层class的实例)

net2 = nn.Sequential(

        nn.Conv2d(3, 3, 3),

        nn.BatchNorm2d(3),

        nn.ReLU()

        )

(3)nn.Sequential(OrderedDict([*多个(层名,层class的实例)]))

from collections import OrderedDict

net3= nn.Sequential(OrderedDict([

          ('conv', nn.Conv2d(3, 3, 3)),

          ('batchnorm', nn.BatchNorm2d(3)),

          ('activation_layer', nn.ReLU())

        ]))

2 检查以及调用模型

查看

print('net1:', net1)

print('net2:', net2)

print('net3:', net3)
net1: Sequential(
  (conv): Conv2d (3, 3, kernel_size=(3, 3), stride=(1, 1))
  (batchnorm): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True)
  (activation_layer): ReLU()
)
net2: Sequential(
  (0): Conv2d (3, 3, kernel_size=(3, 3), stride=(1, 1))
  (1): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True)
  (2): ReLU()
)
net3: Sequential(
  (conv): Conv2d (3, 3, kernel_size=(3, 3), stride=(1, 1))
  (batchnorm): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True)
  (activation_layer): ReLU()
)

根据名字或者序号提取子Module对象

net1.conv, net2[0], net3.conv
(Conv2d (3, 3, kernel_size=(3, 3), stride=(1, 1)),
 Conv2d (3, 3, kernel_size=(3, 3), stride=(1, 1)),
 Conv2d (3, 3, kernel_size=(3, 3), stride=(1, 1)))

调用模型输出结果

可以直接网络对象(输入数据),也可以使用上面的Module子对象分别传入(input)

input = (torch.rand(1, 3, 4, 4))

output = net1(input)

output = net2(input)

output = net3(input)

output = net3.activation_layer(net1.batchnorm(net1.conv(input)))

ModuleList

ModuleList是Module的子类, 被设计用来存储任意数量的nn. module。

如果在构造函数__ init__中用到list、tuple、dict等对象时,一定要思考是否应该用ModuleList或ParameterList代替。

lat_layers  = nn.ModuleList([
            nn.Conv2d(x, 256, kernel_size=1)
            for x in [256,512,1024]
        ])
modellist = nn.ModuleList([nn.Linear(3,4), nn.ReLU(), nn.Linear(4,2)])

注:
ModuleList里面没有forward函数
Sequential里面有forward函数

extend和append方法

用法和python中一样,extend是添加另一个modulelist

append是添加另一个module

class LinearNet(nn.Module):
  def __init__(self, input_size, num_layers, layers_size, output_size):
     super(LinearNet, self).__init__()
 
     self.linears = nn.ModuleList([nn.Linear(input_size, layers_size)])
     self.linears.extend([nn.Linear(layers_size, layers_size) for i in range(1, self.num_layers-1)])
     self.linears.append(nn.Linear(layers_size, output_size)

ref
https://blog.csdn.net/e01528/article/details/84397174

你可能感兴趣的:(torch)