ParameterList#
- class torch.nn.modules.container.ParameterList(values=None)[source]#
將引數儲存在一個列表中。
ParameterList可以像普通 Python 列表一樣使用,但是其中型別為Parameter的 Tensors 會被正確註冊,並且對所有Module方法可見。請注意,建構函式、列表元素的賦值、
append()方法和extend()方法都會將任何Tensor轉換為Parameter。- 引數
parameters (iterable, optional) – 要新增到列表中的元素的可迭代物件。
示例
class MyModule(nn.Module): def __init__(self) -> None: super().__init__() self.params = nn.ParameterList( [nn.Parameter(torch.randn(10, 10)) for i in range(10)] ) def forward(self, x): # ParameterList can act as an iterable, or be indexed using ints for i, p in enumerate(self.params): x = self.params[i // 2].mm(x) + p.mm(x) return x