当前位置: 首页>>技术问答>>正文


numpy dot()和Python 3.5+矩阵乘法@的区别

我最近迁移到了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点乘相同的结果?还有其他的重大差异吗?

numpy python

最佳解决方法

@运算符调用数组的__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 from dot in two important ways.

  • Multiplication by scalars is not allowed.
  • Stacks of matrices are broadcast together as if the matrices were elements.

最后一点清楚地表明dotmatmul方法在传递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

Matrix-Multiplication

参考资料

本文由《纯净天空》出品。文章地址: https://vimsky.com/article/3737.html,未经允许,请勿转载。