我最近转向 Python 3.5 并注意到 新矩阵乘法运算符 (@) 有时与 numpy 点 运算符的行为不同。例如,对于 3d 数组:
import numpy as np
a = np.random.rand(8,13,13)
b = np.random.rand(8,13,13)
c = a @ b # Python 3.5+
d = np.dot(a, b)
@
运算符返回一个形状数组:
c.shape
(8, 13, 13)
而
np.dot()
函数返回:
d.shape
(8, 13, 8, 13)
如何使用 numpy dot 重现相同的结果?还有其他显着差异吗?
@
运算符调用数组的__matmul__
方法,而不是dot
。该方法也作为函数np.matmul
出现在API中。
>>> a = np.random.rand(8,13,13)
>>> b = np.random.rand(8,13,13)
>>> np.matmul(a, b).shape
(8, 13, 13)
来自文档:
在两个重要方面与matmul
不同。dot
- 不允许乘以标量。
- 矩阵的堆栈一起广播,就好像矩阵是元素一样。
最后一点清楚地表明,
dot
和 matmul
方法在传递 3D(或更高维)数组时表现不同。从文档中引用更多:
对于
matmul
:
如果任一参数为 N-D,N > 2,则将其视为驻留在最后两个索引中的一堆矩阵并相应地进行广播。
np.dot
:
对于二维数组,它相当于矩阵乘法,对于一维数组,它相当于向量的内积(没有复杂的共轭)。 对于 N 维,它是 a 的最后一个轴和 b 的倒数第二个轴的和积
仅供参考,
@
及其 numpy 等价物 dot
和 matmul
都同样快。 (用我的一个项目 perfplot 创建的情节。)
重现剧情的代码:
import perfplot
import numpy
def setup(n):
A = numpy.random.rand(n, n)
x = numpy.random.rand(n)
return A, x
def at(A, x):
return A @ x
def numpy_dot(A, x):
return numpy.dot(A, x)
def numpy_matmul(A, x):
return numpy.matmul(A, x)
perfplot.show(
setup=setup,
kernels=[at, numpy_dot, numpy_matmul],
n_range=[2 ** k for k in range(15)],
)
@ajcr 的回答解释了
dot
和matmul
(由@
符号调用)有何不同。通过看一个简单的例子,可以清楚地看到两者在对“矩阵堆栈”或张量进行操作时的行为有何不同。
为了澄清差异,采用 4x4 数组并返回
dot
产品和 matmul
产品与 3x4x2“矩阵堆栈”或张量。
import numpy as np
fourbyfour = np.array([
[1,2,3,4],
[3,2,1,4],
[5,4,6,7],
[11,12,13,14]
])
threebyfourbytwo = np.array([
[[2,3],[11,9],[32,21],[28,17]],
[[2,3],[1,9],[3,21],[28,7]],
[[2,3],[1,9],[3,21],[28,7]],
])
print('4x4*3x4x2 dot:\n {}\n'.format(np.dot(fourbyfour,threebyfourbytwo)))
print('4x4*3x4x2 matmul:\n {}\n'.format(np.matmul(fourbyfour,threebyfourbytwo)))
每个操作的产品如下所示。注意点积是怎样的,
...a 的最后一个轴和 b 的倒数第二个轴的和乘积
以及如何将矩阵一起广播形成矩阵乘积
4x4*3x4x2 dot:
[[[232 152]
[125 112]
[125 112]]
[[172 116]
[123 76]
[123 76]]
[[442 296]
[228 226]
[228 226]]
[[962 652]
[465 512]
[465 512]]]
4x4*3x4x2 matmul:
[[[232 152]
[172 116]
[442 296]
[962 652]]
[[125 112]
[123 76]
[228 226]
[465 512]]
[[125 112]
[123 76]
[228 226]
[465 512]]]
数学方面,我觉得numpy中的点更有意义
点(a,b)_{i,j,k,a,b,c} =
因为当 a 和 b 是向量时它给出点积,或者当 a 和 b 是矩阵时它给出矩阵乘法
对于numpy中的matmul运算,它由部分dot结果组成,可以定义为
matmul(a,b)_{i,j,k,c} =
所以,你可以看到 matmul(a,b) 返回一个小形状的数组, 它具有更小的内存消耗并且在应用程序中更有意义。 特别是结合广播,可以得到
matmul(a,b)_{i,j,k,l} =
例如。
从上面两个定义,可以看出使用这两个操作的要求。假设 a.shape=(s1,s2,s3,s4) 和 b.shape=(t1,t2,t3,t4)
用下面的一段代码来说服自己。
import numpy as np
for it in range(10000):
a = np.random.rand(5,6,2,4)
b = np.random.rand(6,4,3)
c = np.matmul(a,b)
d = np.dot(a,b)
#print ('c shape: ', c.shape,'d shape:', d.shape)
for i in range(5):
for j in range(6):
for k in range(2):
for l in range(3):
if c[i,j,k,l] != d[i,j,k,j,l]:
print (it,i,j,k,l,c[i,j,k,l]==d[i,j,k,j,l]) # you will not see them
np.einsum
的比较,以显示指数是如何预测的
np.allclose(np.einsum('ijk,ijk->ijk', a,b), a*b) # True
np.allclose(np.einsum('ijk,ikl->ijl', a,b), a@b) # True
np.allclose(np.einsum('ijk,lkm->ijlm',a,b), a.dot(b)) # True
我对 MATMUL 和 DOT 的体验
我在尝试使用 MATMUL 时不断收到“ValueError:传递值的形状是 (200, 1),索引暗示 (200, 3)”。我想要一个快速的解决方法,并发现 DOT 可以提供相同的功能。使用 DOT 时我没有收到任何错误。我得到正确答案
使用 MATMUL
X.shape
>>>(200, 3)
type(X)
>>>pandas.core.frame.DataFrame
w
>>>array([0.37454012, 0.95071431, 0.73199394])
YY = np.matmul(X,w)
>>> ValueError: Shape of passed values is (200, 1), indices imply (200, 3)"
带点
YY = np.dot(X,w)
# no error message
YY
>>>array([ 2.59206877, 1.06842193, 2.18533396, 2.11366346, 0.28505879, …
YY.shape
>>> (200, )