矩阵矩阵乘法的函数numpy.dot(),@和方法.dot()之间有什么区别? [英] What is difference between the function numpy.dot(), @, and method .dot() for matrix-matrix multiplication?
问题描述
有什么区别吗?如果不是,按惯例首选什么? 性能似乎几乎相同.
Is there any difference? If not, what is preferred by convention? The performance seems to be almost the same.
a=np.random.rand(1000,1000)
b=np.random.rand(1000,1000)
%timeit a.dot(b) #14.3 ms ± 374 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit np.dot(a,b) #14.7 ms ± 315 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit a @ b #15.1 ms ± 779 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
推荐答案
它们基本上都在做相同的事情.在计时方面,根据Numpy的文档,此处 :
They are all basically doing the same thing. In terms of timing, based on Numpy's documentation here:
-
如果a和b都是一维数组,则它是向量的内积 (没有复杂的共轭).
If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
如果a和b均为二维数组,则为矩阵乘法,但是
最好使用matmul
或a @ b
.
If both a and b are 2-D arrays, it is matrix multiplication, but
using matmul
or a @ b
is preferred.
如果a或b为0-D(标量),则等于乘和
最好使用numpy.multiply(a, b)
或a * b
.
If either a or b is 0-D (scalar), it is equivalent to multiply and
using numpy.multiply(a, b)
or a * b
is preferred.
如果a是一个N-D数组而b是一个一维数组,则它是
a
和b
的最后一个轴.
If a is an N-D array and b is a 1-D array, it is a sum product over
the last axis of a
and b
.
这篇关于矩阵矩阵乘法的函数numpy.dot(),@和方法.dot()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!