• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

最佳解决方法

@运算符调用数组的__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

参考资料

  • Difference between numpy dot() and Python 3.5+ matrix multiplication @


鲜花

握手

雷人

路过

鸡蛋
专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap