Numpy 文档中提到的“sum product”是什么意思?

Jér*_*meL 5 numpy

在 NumPy v1.15 Reference Guide 中,numpy.dot 的文档使用了“sum product”的概念。

即,我们阅读以下内容:

  • 如果 a 是 ND 数组而 b 是一维数组,则它是 a 和 b 的最后一个轴上的和积。
  • 如果 a 是 ND 数组,b 是 MD 数组(其中 M>=2),则它是 a 的最后一个轴和 b 的倒数第二个轴的和积:
    dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

这个“总和产品”概念的定义是什么?
(例如,在维基百科上找不到这样的定义。)

hpa*_*ulj 5

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.
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

在早期的数学课上,你是否学会了矩阵乘积,用一根手指划过 的行A和列B,将成对的数字相乘并求和?那个动作是我对如何使用该产品的直觉的一部分。


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

  • 如果a是 ND 数组并且b是一维数组,则它是 和 的最后一个轴上的a和积b

  • 如果第二个参数是 1-D,则通过在其维度上附加 1 将其提升为矩阵。在矩阵乘法之后,附加的 1 被删除。

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

将维度附加到B,执行:

In [105]: np.matmul([[1,2],[3,4]], [[1],[2]])
Out[105]: 
array([[ 5],
       [11]])
Run Code Online (Sandbox Code Playgroud)

最后一个是 (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])
Run Code Online (Sandbox Code Playgroud)

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

  • 这实际上是在执行 `np.array(( np.dot([1,2], [1,2]), np.dot([3,4], [1,2]))`,即 2向量点积,每行一个“A”。“np.array(( 1+4, 3+8))”。 (2认同)