ParameterList#
- class torch.nn.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