torch nn.Sequential

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dss_dssssd/article/details/82980222

nn.Sequential

A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of modules can also be passed in.

一个有序的容器,神经网络模块将按照在传入构造器的顺序依次被添加到计算图中执行,同时以神经网络模块为元素的有序字典也可以作为传入参数。

看一下例子:

# Example of using Sequential
        model = nn.Sequential(
                  nn.Conv2d(1,20,5),
                  nn.ReLU(),
                  nn.Conv2d(20,64,5),
                  nn.ReLU()
                )

        # Example of using Sequential with OrderedDict
        model = nn.Sequential(OrderedDict([
                  ('conv1', nn.Conv2d(1,20,5)),
                  ('relu1', nn.ReLU()),
                  ('conv2', nn.Conv2d(20,64,5)),
                  ('relu2', nn.ReLU())
                ]))

接下来看一下Sequential源码,是如何实现的,主要看一下forward函数:
https://pytorch.org/docs/stable/_modules/torch/nn/modules/container.html#Sequential

在这里插入图片描述

因为每一个module都继承于nn.Module,都会实现__call__forward函数,具体讲解点击这里,所以forward函数中通过for循环依次调用各个神经网络模块的forward函数,最后输出经过所有神经网络层的结果。

下面是简单的例子:

class Activation_Net(nn.Module):
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super().__init__()

        self.layer1 = nn.Sequential(
            nn.Linear(in_dim, n_hidden_1), nn.ReLU(True))
        self.layer2 = nn.Sequential(
            nn.Linear(n_hidden_1, n_hidden_2), nn.ReLU(True))
        # 最后一层不需要添加激活函数
        self.layer3 = nn.Sequential(
            nn.Linear(n_hidden_2, out_dim))

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        return x

上面的代码就是通过Squential将网络层和激活函数结合起来,输出激活后的网络节点。

猜你喜欢

转载自blog.csdn.net/dss_dssssd/article/details/82980222