oneflow.nn.ModuleList¶
-
class
oneflow.nn.
ModuleList
(modules: Optional[Iterable[oneflow.nn.modules.module.Module]] = None)¶ Holds submodules in a list.
The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.ModuleList.html?#torch.nn.ModuleList.
ModuleList
can be indexed like a regular Python list, but modules it contains are properly registered, and will be visible by allModule
methods.- Parameters
modules (iterable, optional) – an iterable of modules to add
>>> import oneflow.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 >>> model = MyModule() >>> model.linears ModuleList( (0): Linear(in_features=10, out_features=10, bias=True) (1): Linear(in_features=10, out_features=10, bias=True) (2): Linear(in_features=10, out_features=10, bias=True) (3): Linear(in_features=10, out_features=10, bias=True) (4): Linear(in_features=10, out_features=10, bias=True) (5): Linear(in_features=10, out_features=10, bias=True) (6): Linear(in_features=10, out_features=10, bias=True) (7): Linear(in_features=10, out_features=10, bias=True) (8): Linear(in_features=10, out_features=10, bias=True) (9): Linear(in_features=10, out_features=10, bias=True) )