【nn.Sequential】pytorch中nn.Sequential实例理解

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

import torch.nn as nn

        # Example of using Sequential
        model = nn.Sequential(
                  nn.Conv2d(1,20,5),
                  nn.ReLU(inplace=True),
                  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(inplace=True)),
                  ('conv2', nn.Conv2d(20,64,5)),
                  ('relu2', nn.ReLU())
                ]))

放到一个网络中,通过Squential将网络层和激活函数结合起来,输出激活后的网络节点。

import torch.nn as nn


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

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

    def forward(self, x):
        x = self.layer(x)
        return x


if __name__ == '__main__':
    in_dim = 1
    n_hidden_1 = 1
    n_hidden_2 = 1
    out_dim = 1
    model = Net(in_dim, n_hidden_1, n_hidden_2, out_dim)
    print(model)

输出:

Net(
  (layer): Sequential(
    (0): Linear(in_features=1, out_features=1, bias=True)
    (1): ReLU(inplace=True)
    (2): Linear(in_features=1, out_features=1, bias=True)
    (3): ReLU(inplace=True)
    (4): Linear(in_features=1, out_features=1, bias=True)
  )
)

感谢链接:

https://blog.csdn.net/dss_dssssd/article/details/82980222
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值