矩阵矩阵乘法的函数numpy.dot(),@和方法.dot()之间有什么区别? [英] What is difference between the function numpy.dot(), @, and method .dot() for matrix-matrix multiplication?

查看:580
本文介绍了矩阵矩阵乘法的函数numpy.dot(),@和方法.dot()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别吗?如果不是,按惯例首选什么? 性能似乎几乎相同.

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均为二维数组,则为矩阵乘法,但是 最好使用matmula @ 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是一个一维数组,则它是 ab的最后一个轴.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆