【问题标题】:What is the meaning of "sum product" as mentioned in Numpy documentation?Numpy文档中提到的“sum product”是什么意思?
【发布时间】:2018-10-13 01:07:26
【问题描述】:

在 NumPy v1.15 参考指南中,documentation for numpy.dot 使用“和积”的概念。

也就是说,我们阅读了以下内容:

  • 如果 a 是 N 维数组而 b 是一维数组,则它是 a 和 b 的最后一个轴的和积。
  • 如果 a 是 N 维数组而 b 是 M 维数组(其中 M>=2),则它是 a 的最后一个轴和 b 的倒数第二个轴的和积:
    @987654322 @

这个“和积”概念的定义是什么?
(例如,在 Wikipedia 上找不到这样的定义。)

【问题讨论】:

  • 对应元素的乘积之和。 np.dot([3,4,4], [4,3,2])(3 * 4) + (4 * 3) + (4 * 2) 相同。
  • 顺便说一句。我不认为“和积”是一个技术术语,听起来更像是他们在进行过程中编造了这个词。
  • sum of the products的简称,应用广泛。
  • @hpaulj 听起来更像是总和的乘积。 (相信我——我是德国人,我对复合名词略知一二)
  • 与@Paul Panzer 具有相同的理解,我发现“和积”一词令人困惑,宁愿将其称为“积和”,因此我感到困惑。

标签: numpy


【解决方案1】:

https://en.wikipedia.org/wiki/Matrix_multiplication

That is, the entry c[i,j] of the product is obtained by multiplying 
term-by-term the entries of the ith row of A and the jth column of B, 
and summing these m products. In other words, c[i,j] is the dot product 
of the ith row of A and the jth column of B.

https://en.wikipedia.org/wiki/Dot_product

Algebraically, the dot product is the sum of the products of the 
corresponding entries of the two sequences of numbers.

在早期的数学课中,您是否学会了计算矩阵乘积,即用一根手指在A 的行和B 的列上滑动,将成对的数字相乘并求和?该动作是我对该产品如何采用的直觉的一部分。


对于 1d 第二个参数的情况,np.dotnp.matmul 产生相同的东西,但对动作的描述不同:

  • 如果a 是 N 维数组,b 是一维数组,则它是一个和积 ab 的最后一个轴。

  • 如果第二个参数是一维的,它被提升为矩阵 将 1 添加到其尺寸。矩阵乘法后 附加的 1 被删除。

    在 [103] 中:np.dot([[1,2],[3,4]], [1,2]) 输出[103]:数组([ 5, 11]) 在 [104] 中:np.matmul([[1,2],[3,4]], [1,2]) Out[104]: 数组([ 5, 11])

将维度附加到B,会:

In [105]: np.matmul([[1,2],[3,4]], [[1],[2]])
Out[105]: 
array([[ 5],
       [11]])

最后一个是 (2,2) 和 (2,1) => (2,1)

有时用einsum 术语表达动作会更清楚:

In [107]: np.einsum('ij,j->i', [[1,2],[3,4]], [1,2])
Out[107]: array([ 5, 11])

j,两个数组的最后一个轴是被“求和”的那个。

【讨论】:

  • 我同意,Matrix产品的案例确实很熟悉。相比之下,我没有像 np.dot([[1, 2], [3, 4]], [1, 2]) 产生 array([ 5, 11]) 那样对矩阵 * 向量积背后的理论有直觉,尤其是从形状/尺寸的角度来看。
  • 这实际上是在做np.array(( np.dot([1,2], [1,2]), np.dot([3,4], [1,2])),即两个向量点积,A 的每一行一个。 np.array(( 1+4, 3+8)).
猜你喜欢
  • 2021-03-04
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多