評價此頁

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

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

引數

value (Any) – 要追加的值

返回型別

自我

extend(values)[source]#

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

引數

values (iterable) – 要追加的值的可迭代物件

返回型別

自我

extra_repr()[source]#

返回模組的額外表示。

返回型別

str