Difference between numpy dot() and Python 3.5+ matrix multiplication @
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)
@ operator returns an array of shape:
c.shape (8, 13, 13)
np.dot() function returns:
d.shape (8, 13, 8, 13)
How can I reproduce the same result with numpy dot? Are there any other significant differences?
@ operator calls the array's
__matmul__ method, not
dot. This method is also present in the API as the function
>>> a = np.random.rand(8,13,13) >>> b = np.random.rand(8,13,13) >>> np.matmul(a, b).shape (8, 13, 13)
From the documentation:
dotin two important ways.
- Multiplication by scalars is not allowed.
- Stacks of matrices are broadcast together as if the matrices were elements.
The last point makes it clear that
matmul methods behave differently when passed 3D (or higher dimensional) arrays. Quoting from the documentation some more:
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.
★ Back to homepage or read more recommendations:
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