dot
函数会有这样的行为:M = np.ones((9, 9))
V1 = np.ones((9,))
V2 = np.ones((9, 5))
V3 = np.ones((2, 9, 5))
V4 = np.ones((3, 2, 9, 5))
现在
np.dot(M, V1)
和np.dot(M, V2)
的表现符合预期。但对于V3
和V4
,结果让我感到惊讶:>>> np.dot(M, V3).shape
(9, 2, 5)
>>> np.dot(M, V4).shape
(9, 3, 2, 5)
我预期分别是
(2, 9, 5)
和 (3, 2, 9, 5)
。另一方面,np.matmul
做到了我预期的:矩阵乘法会在第二个参数的前 N - 2 维度上进行广播,并且结果具有相同的形状。>>> np.matmul(M, V3).shape
(2, 9, 5)
>>> np.matmul(M, V4).shape
(3, 2, 9, 5)
所以我的问题是:为什么np.dot
的行为是这样的?它是否有特定的目的,还是应用了一些通用规则的结果?