numpy dot() 和 Python 3.5+ 矩阵乘法的区别@ [英] Difference between numpy dot() and Python 3.5+ matrix multiplication @

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

问题描述

我最近转向 Python 3.5 并注意到 新的矩阵乘法运算符 (@) 有时与 numpy 点的行为不同 运算符.例如,对于 3d 数组:

将 numpy 导入为 npa = 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 dot 重现相同的结果?还有其他显着差异吗?

解决方案

@ 运算符调用数组的 __matmul__ 方法,而不是 dot.此方法也作为函数出现在 API 中 np.matmul.

<预><代码>>>>a = np.random.rand(8,13,13)>>>b = np.random.rand(8,13,13)>>>np.matmul(a, b).shape(8, 13, 13)

来自文档:

<块引用>

matmuldot 在两个重要方面不同.

  • 不允许乘以标量.
  • 矩阵堆栈一起广播,就好像矩阵是元素一样.

最后一点清楚地表明,dotmatmul 方法在传递 3D(或更高维)数组时表现不同.从文档中引用更多:

对于matmul:

<块引用>

如果任一参数是 N-D,则 N >2,它被视为驻留在最后两个索引中的一堆矩阵并相应地广播.

对于 np.dot:

<块引用>

对于二维数组,它相当于矩阵乘法,对于一维数组,它相当于向量的内积(没有复共轭).对于 N 维,它是 a 的最后一个轴和 b 的倒数第二个轴的和积

I recently moved to Python 3.5 and noticed the new matrix multiplication operator (@) sometimes behaves differently from the numpy dot operator. In example, for 3d arrays:

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)

The @ operator returns an array of shape:

c.shape
(8, 13, 13)

while the 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?

解决方案

The @ operator calls the array's __matmul__ method, not dot. This method is also present in the API as the function np.matmul.

>>> 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:

matmul differs from dot in 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 dot and matmul methods behave differently when passed 3D (or higher dimensional) arrays. Quoting from the documentation some more:

For 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.

For 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

这篇关于numpy dot() 和 Python 3.5+ 矩阵乘法的区别@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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