gpt4 book ai didi

python - 当矩阵a是二维矩阵,b是三维矩阵时,如何理解matmul函数?

转载 作者:行者123 更新时间:2023-12-01 01:28:21 28 4
gpt4 key购买 nike

a=np.arange(8).reshape(2,2,2)
b=np.arange(4).reshape(2,2)
print(np.matmul(a,b))

结果是:[[[ 2 3] [6 11]]

[[10 19] [14 27]]]如何理解结果?它是如何产生的

最佳答案

简短回答:它将第二个 2d 矩阵“广播”到 3d 矩阵,然后执行“映射”,以便将元素子矩阵映射到结果中的新子矩阵。

作为documentation on np.matmul [numpy-doc]说:

numpy.matmul(a, b, out=None)

Matrix product of two arrays.

The behavior depends on the arguments in the following way.

  1. If both arguments are 2-D they are multiplied like conventional matrices.
  2. If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
  3. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
  4. If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

所以这里第二项适用。因此,首先第二个矩阵也被“广播”到 3d 变体,这意味着我们要乘以:

array([[[0, 1],
[2, 3]],

[[4, 5],
[6, 7]]])

与:

array([[[0, 1],
[2, 3]],

[[0, 1],
[2, 3]]])

我们将它们视为堆叠矩阵。首先我们乘以:

array([[0, 1],      array([[0, 1],
[2, 3]]) x [2, 3]])

这给了我们:

array([[ 2,  3],
[ 6, 11]])

然后是元素第二个子矩阵:

array([[4, 5],      array([[0, 1],
[6, 7]]) x [2, 3]])

这给了我们:

array([[10, 19],
[14, 27]])

因此,我们将它们叠加到结果中,并获得:

>>> np.matmul(a, b)
array([[[ 2, 3],
[ 6, 11]],

[[10, 19],
[14, 27]]])

尽管该行为已被完美定义,但最好谨慎使用此功能,因为对于 3d 矩阵与 2d 矩阵的“矩阵乘积”可能是什么样子,还有其他“有意义的”定义,因此这些是此处未使用。

关于python - 当矩阵a是二维矩阵,b是三维矩阵时,如何理解matmul函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139757/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com