理解一维向量上的 np.matmul

understand np.matmul on 1D vectors

a = [1, 2, 3]
b = [10, 10, 10]

np.matmul(a, b) 结果为 60。

numpy如何将(3,)和(3,)维相乘并且returns点积不是外积(3 * 3)或抛出错误"dimension not matching"?

这直接来自 numpy.matmul() 的文档:

  • 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.
  • 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.

因此,在矩阵乘法期间,输入 ab 的形状分别转换为 (1, 3)(3,1)

根据矩阵乘法的规则,我们知道:

1 x 3 3 x 1
| |
-------- ===> 求和了。

因此,我们得到的结果是 标量