ModuleList#
- classtorch.nn.modules.container.ModuleList(modules=None)[source]#
Holds submodules in a list.
ModuleListcan be indexed like a regular Python list, butmodules it contains are properly registered, and will be visible by allModulemethods.- Parameters
modules (iterable,optional) – an iterable of modules to add
Example:
classMyModule(nn.Module):def__init__(self)->None:super().__init__()self.linears=nn.ModuleList([nn.Linear(10,10)foriinrange(10)])defforward(self,x):# ModuleList can act as an iterable, or be indexed using intsfori,linenumerate(self.linears):x=self.linears[i//2](x)+l(x)returnx
- append(module)[source]#
Append a given module to the end of the list.
- Parameters
module (nn.Module) – module to append
- Return type
Self