我最近迁移到了Python 3.5,注意到new matrix multiplication operator(新的矩阵乘法云算符) (@)的行为与numpy dot操作符有所不同。例如,对于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点乘相同的结果?还有其他的重大差异吗?
最佳解决方法
@
运算符调用数组的__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
differs fromdot
in two important ways.
- Multiplication by scalars is not allowed.
- Stacks of matrices are broadcast together as if the matrices were elements.
最后一点清楚地表明dot
和matmul
方法在传递3D(或更高维)数组时行为不同。更多的引用文档:
对于matmul
:
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.
对于np.dot
:
For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b