Containers-ModuleList
CLASS torch.nn.ModuleList(modules=None)
将所有子模块放到一个列表中。
ModuleList
可以像 Python
的列表一样进行索引,但是其所包含的模块都被正确的注册了,并且对所有的方法都可见。
- Parameters
modules (iterable, optional)
- 要添加的可迭代模块
- example
import torch.nn as nn
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__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
-
method
-
append(module)
将制定模块插入到
List
尾部;
输入:module
输出:ModuleList
- extend(modules)
将 Python 可迭代模块追加到列表尾部
输入:module
输出:ModuleList
- insert(index, module)
在列表中给定索引的前面插入指定的模块
输入:
index (int)
索引
module (nn.Module)
需要插入的模块