评价此页

torch.functional.chain_matmul#

torch.functional.chain_matmul(*matrices, out=None)[源码]#

返回 NN 个 2 维张量的矩阵乘积。此乘积使用矩阵链顺序算法高效计算,该算法选择算术运算成本最低的顺序([CLRS])。请注意,由于这是一个计算乘积的函数,因此 NN 必须大于或等于 2;如果等于 2,则返回平凡的矩阵-矩阵乘积。如果 NN 为 1,则这是一个无操作 - 返回原始矩阵。

警告

torch.chain_matmul() 已弃用,并将在未来的 PyTorch 版本中移除。请使用 torch.linalg.multi_dot() 代替,它接受一个包含两个或更多张量的列表,而不是多个参数。

参数
  • matrices (Tensors...) – 要确定其乘积的 2 个或更多 2 维张量的序列。

  • out (Tensor, optional) – 输出张量。如果 out = None,则忽略。

返回

如果第 ithi^{th} 张量的维度为 pi×pi+1p_{i} \times p_{i + 1},则乘积的维度为 p1×pN+1p_{1} \times p_{N + 1}.

返回类型

张量

示例

>>> a = torch.randn(3, 4)
>>> b = torch.randn(4, 5)
>>> c = torch.randn(5, 6)
>>> d = torch.randn(6, 7)
>>> # will raise a deprecation warning
>>> torch.chain_matmul(a, b, c, d)
tensor([[ -2.3375,  -3.9790,  -4.1119,  -6.6577,   9.5609, -11.5095,  -3.2614],
        [ 21.4038,   3.3378,  -8.4982,  -5.2457, -10.2561,  -2.4684,   2.7163],
        [ -0.9647,  -5.8917,  -2.3213,  -5.2284,  12.8615, -12.2816,  -2.5095]])