Shortcuts

参数列表

class torch.nn.ParameterList(values=None)[源代码]

以列表形式保存参数。

ParameterList 可以像普通的 Python 列表一样使用,但作为 Parameter 的张量会被正确注册,并且所有 Module 方法都能看到它们。

请注意,构造函数、分配列表中的元素、append() 方法和 extend() 方法会将任何 Tensor 转换为 Parameter

Parameters

参数 (可迭代对象, 可选) – 要添加到列表中的元素的可迭代对象。

示例:

class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.params = nn.ParameterList([nn.Parameter(torch.randn(10, 10)) for i in range(10)])

    def forward(self, x):
        # ParameterList 可以作为可迭代对象,或者使用整数进行索引
        for i, p in enumerate(self.params):
            x = self.params[i // 2].mm(x) + p.mm(x)
        return x
append(value)[源代码]

在列表末尾追加给定的值。

Parameters

(任意类型) – 要追加的值

Return type

ParameterList

extend(values)[源代码]

将Python可迭代对象中的值追加到列表的末尾。

Parameters

(可迭代对象) – 要追加的值的可迭代对象

Return type

自我

优云智算