評價此頁

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
append(value)[source]#

將給定值追加到列表末尾。

引數

value (Any) – 要追加的值

返回型別

自我

extend(values)[source]#

將 Python 可迭代物件中的值追加到列表末尾。

引數

values (iterable) – 要追加的值的迭代器

返回型別

自我

extra_repr()[source]#

返回模組的額外表示。

返回型別

str