Shortcuts

ModuleList

class torch.nn.ModuleList(modules=None)[源代码]

在列表中保存子模块。

ModuleList 可以像普通的 Python 列表一样进行索引,但它包含的模块会被正确注册,并且可以通过所有 Module 方法可见。

Parameters

模块可迭代对象可选)—— 要添加的模块的可迭代对象

示例:

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

    def forward(self, x):
        # ModuleList可以作为可迭代对象,或者使用整数进行索引
        for i, l in enumerate(self.linears):
            x = self.linears[i // 2](x) + l(x)
        return x
append(module)[源代码]

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

Parameters

模块 (nn.Module) – 要附加的模块

Return type

ModuleList

extend(modules)[源代码]

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

Parameters

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

Return type

自我

insert(index, module)[源代码]

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

Parameters
  • 索引 (int) – 要插入的索引。

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

优云智算