快捷方式

ModuleList

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

在列表中保存子模块。

ModuleList 可以像常规 Python 列表一样进行索引,但它包含的模块已正确注册,并且对于所有 Module 方法都是可见的。

参数

modules (iterable, optional) – 要添加的模块的可迭代对象

示例

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]

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

参数

module (nn.Module) – 要追加的模块

返回类型

ModuleList

extend(modules)[source][source]

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

参数

modules (iterable) – 要追加的模块的可迭代对象

返回类型

Self

insert(index, module)[source][source]

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

参数
  • index (int) – 要插入的索引。

  • module (nn.Module) – 要插入的模块

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

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

查看资源