ParameterList¶
- class torch.nn.ParameterList(values=None)[原始碼][原始碼]¶
在列表中保存參數。
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