快捷键

顺序的 ¶

类 torch.nn.Sequential(*argsModule)[source][source] ¶
类 torch.nn.Sequential(argOrderedDict[strModule])

顺序容器。

模块将按照它们在构造函数中传入的顺序添加到其中。或者,也可以传入一个 OrderedDict 模块。 forward() 方法接受任何输入并将其转发到它包含的第一个模块。然后,“链式”地将输出传递给后续模块的输入,最后返回最后一个模块的输出。

Sequential 提供的价值在于,它允许将整个容器视为单个模块,这样对 Sequential 进行的转换就会应用到它存储的每个模块(这些模块都是 Sequential 的注册子模块)。

Sequentialtorch.nn.ModuleList 之间的区别是什么? ModuleList 正如其名—一个用于存储 Module 的列表!另一方面, Sequential 中的层以级联方式连接。

示例:

# 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())
        ]))
append(module)[source][source]

将给定的模块添加到末尾。

参数:

模块(nn.Module)- 添加模块

返回类型:

顺序的


© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源