快捷键

模块列表 ¶

class torch.nn.ModuleList(modules=None)[source][source]

在列表中保存子模块。

可以像常规 Python 列表一样索引,但其中包含的模块会被正确注册,并且所有方法都可以看到。

参数:

模块(可迭代,可选)- 要添加的模块的可迭代对象

示例:

class MyModule(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])

    def forward(self, x):
        # ModuleList can act as an iterable, or be indexed using ints
        for i, l in enumerate(self.linears):
            x = self.linears[i // 2](x) + l(x)
        return x
append(module)[source][source]

将指定的模块追加到列表的末尾。

参数:

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

返回类型:

模块列表

extend(modules)[source][source]

将 Python 可迭代对象中的模块追加到列表末尾。

参数:

模块(可迭代)- 要附加的模块的可迭代序列

返回类型:

自身

insert(index, module)[源码][源码] ¶

在列表中在指定索引之前插入给定模块。

参数:
  • index(整数)- 要插入的索引。

  • 模块(nn.Module) – 插入模块


© 版权所有 PyTorch 贡献者。

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

文档

PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源