【pytorch函数笔记】torch.nn.Sequential()

转载:https://blog.csdn.net/gdxdekx/article/details/122691668

 https://blog.csdn.net/Just_do_myself/article/details/124195393

1.介绍查看帮助文档的方法

代码查看: 

import torch
import torch.nn as nn


# help(torch.ones),我们可以调用help函数。例如,我们来[查看张量ones函数的用法。]
help(torch.ones)


# 查看torch.nn.Sequential的帮助文档
help(nn.Sequential)

官方文档:https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html?highlight=sequential#torch.nn.Sequential

2.用法

CLASS    torch.nn.Sequential(*args: Module)

A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an OrderedDict of modules can be passed in. The forward() method of Sequential accepts any input and forwards it to the first module it contains. It then “chains” outputs to inputs sequentially for each subsequent module, finally returning the output of the last module.

The value a Sequential provides over manually calling a sequence of modules is that it allows treating the whole container as a single module, such that performing a transformation on the Sequential applies to each of the modules it stores (which are each a registered submodule of the Sequential).

What’s the difference between a Sequential and a torch.nn.ModuleList? A ModuleList is exactly what it sounds like–a list for storing Module s! On the other hand, the layers in a Sequential are connected in a cascading way.
Sequential容器。 模块将按照它们在构造函数中传递的顺序添加。 另外,可以将模块的有序列表传递给。 然后,它将“链”输出输入到每个后续模块,最后返回最后一个模块的输出。

顺序通过手动调用一个模块的序列提供的值是,它允许将整个容器视为单个模块,以便在顺序上执行转换适用于其存储的每个模块(每个模块都是一个注册的子模块 顺序)。

Sequential和nn.modulelist有什么区别? modulelist 恰恰是听起来的样子 - 存储模块s的列表! 另一方面,连续的Sequential以级联的方式连接。

看个例子:

import torch
import torch.nn as nn
from collections import OrderedDict

# Using Sequential to create a small model. When `model` is run,
# input will first be passed to `Conv2d(1,20,5)`. The output of
# `Conv2d(1,20,5)` will be used as the input to the first
# `ReLU`; the output of the first `ReLU` will become the input
# for `Conv2d(20,64,5)`. Finally, the output of
# `Conv2d(20,64,5)` will be used as input to the second `ReLU`
model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )


# Using Sequential with OrderedDict. This is functionally the
# same as the above code
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

 结果:

nn.Sequential()的本质作用

按照上边的说法,与一层一层的单独调用模块组成序列相比,nn.Sequential() 可以允许将整个容器视为单个模块(即相当于把多个模块封装成一个模块),forward()方法接收输入之后,nn.Sequential()按照内部模块的顺序自动依次计算并输出结果。

这就意味着我们可以利用nn.Sequential() 自定义自己的网络层
 

from torch import nn


class net(nn.Module):
    def __init__(self, in_channel, out_channel):
        super(net, self).__init__()
        self.layer1 = nn.Sequential(nn.Conv2d(in_channel, in_channel / 4, kernel_size=1),
                                    nn.BatchNorm2d(in_channel / 4),
                                    nn.ReLU())
        self.layer2 = nn.Sequential(nn.Conv2d(in_channel / 4, in_channel / 4),
                                    nn.BatchNorm2d(in_channel / 4),
                                    nn.ReLU())
        self.layer3 = nn.Sequential(nn.Conv2d(in_channel / 4, out_channel, kernel_size=1),
                                    nn.BatchNorm2d(out_channel),
                                    nn.ReLU())
        
    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        
        return x

上边的代码,我们通过nn.Sequential()将卷积层,BN层和激活函数层封装在一个层中,输入x经过卷积、BN和ReLU后直接输出激活函数作用之后的结果。

nn.Sequential()和torch.nn.ModuleList的区别在于:torch.nn.ModuleList只是一个储存网络模块的list,其中的网络模块之间没有连接关系和顺序关系。而nn.Sequential()内的网络模块之间是按照添加的顺序级联的。
 

nn.Sequential()源码

    def __init__(self, *args):
        super().__init__()
        if len(args) == 1 and isinstance(args[0], OrderedDict):
            for key, module in args[0].items():
                self.add_module(key, module)
        else:
            for idx, module in enumerate(args):
                self.add_module(str(idx), module)

nn.Sequential()首先判断接收的参数是否为OrderedDict类型,如果是的话,分别取出OrderedDict内每个元素的key(自定义的网络模块名)和value(网络模块),然后将其通过add_module方法添加到nn.Sequrntial()中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyTorch是一个基于Python的科学计算库,主要针对深度学习任务。在PyTorch中,torch.nn是一个用于构建神经网络模型的模块。 torch.nn模块提供了一系列神经网络层和函数,方便用户构建自定义的神经网络。用户可以通过继承torch.nn.Module类来定义自己的神经网络模型。torch.nn模块中常用的类包括各种层(例如全连接层、卷积层、池化层和循环层等)、非线性激活函数和损失函数等。 在使用torch.nn模块构建神经网络时,用户需要实现模型的前向传播函数forward()。该函数定义了输入数据在神经网络中的流动方式,即通过层和函数的组合计算输出。在forward()函数中,用户可以使用已定义的层和函数进行计算,也可以实现自定义的操作。 torch.nn模块中的另一个重要概念是参数(parameter)。参数是模型中需要学习的变量,例如网络层的权重和偏置项。用户可以通过在模型中定义torch.nn.Parameter对象来创建参数,并在forward()函数中进行使用。 除了torch.nn模块外,PyTorch还提供了其他的工具和模块来辅助神经网络的训练和优化过程。例如torch.optim模块包含了各种优化算法,如随机梯度下降(SGD)、Adam等,用于更新模型中的参数。torch.utils.data模块提供了数据处理和加载的工具,方便用户使用自己的数据训练模型。 总之,torch.nn模块是PyTorch中用于构建神经网络模型的重要组成部分。通过使用torch.nn的各种类和函数,用户可以方便地创建自己想要的神经网络结构,并利用PyTorch强大的计算能力和优化算法来训练和优化模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值