我最近遷移到了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