简体   繁体   English

为什么两个 numpy (n,) 向量的矩阵@乘积是点积,而不是外积?

[英]Why is the matrix @ product of two numpy (n,) vectors the dot product, rather than the outer product?

If I have x.shape = (n,) and y.shape = (n,), then x @ y returns their dot product, rather than their outer product.如果我有 x.shape = (n,) 和 y.shape = (n,),则 x @ y 返回它们的点积,而不是它们的外积。 I was wondering if there was some underlying reason for this, or if its just for convenience.我想知道这是否有一些潜在的原因,或者是否只是为了方便。

Thanks.谢谢。

Function np.matmul was added when @ operator was introduced to Python. Function np.matmul是在将@运算符引入 Python 时添加的。 The new function was designed to behave as similar to np.dot as reasonable.新的 function 被设计为与np.dot相似,合理。

So why np.dot(vector, vector) performs inner product?那么为什么np.dot(vector, vector)执行内积呢?

Before @ , the function np.dot was used to perform matrix algebra in numpy.@之前,function np.dot用于在 numpy 中执行矩阵代数。 Actually, vectors in linear algebra are not 1D arrays but rather matrices with one of dimensions set to 1. In order to multiply 1D array by matrix, the former has to be somehow promoted to either row or column vector.实际上,线性代数中的向量不是一维 arrays 而是其中一个维度设置为 1 的矩阵。为了将一维数组乘以矩阵,必须以某种方式将前者提升为行向量或列向量。 We have 3 cases here:我们这里有3个案例:

  • vector by matrix, 1D array is promoted to row vector (1xN) to make operation valid逐个矩阵的向量,一维数组被提升为行向量(1xN)以使操作有效
  • matrix by vector, 1D array is promoted to column vector (Nx1)矩阵逐个向量,一维数组被提升为列向量 (Nx1)
  • vector by vector, left operand is promoted to row vector, right to column vector, as in previous both cases一个向量一个向量,左操作数被提升为行向量,右操作数被提升为列向量,和前面的两种情况一样

As result in the last case we have a normal inner product between two vectors.作为最后一种情况的结果,我们在两个向量之间有一个正常的内积。 This policy is both logical and practical because inner products are used more often.这个策略既合乎逻辑又实用,因为内积的使用频率更高。

The wiki article for dot product defines it as dot product的维基文章将其定义为

Algebraically, the dot product is the sum of the products of the corresponding entries of the two sequences of numbers.在代数上,点积是两个数字序列的相应条目的乘积之和。

(it mentions inner product many times, but outer none.) (它多次提到inner product ,但没有提到outer产品。)

If you think of 1d arrays as sequences of numbers, then A@B as dot product is natural.如果您将 1d arrays 视为数字序列,那么A@B作为点积是自然的。

The 2d matrix product can be described as the dot product of all rows of A with the columns of B.二维矩阵积可以描述为 A 的所有行与 B 的列的dot product

The key term, in both matmul and dot is "sum of the products". matmuldot中的关键术语是“产品的总和”。

For 1d array, np.sum(A*B) is another expression of the sum of products, where * is elementwise mutiplication.对于一维数组, np.sum(A*B)是乘积和的另一个表达式,其中*是元素乘法。

A (m,1) @ with a (1,n) does produce a (m,n) outer product, but that is actually a 2d matrix product with reduction on the shared size 1 dimensions.带有 (1,n) 的 (m,1) @ 确实会产生 (m,n) 外积,但这实际上是在共享大小 1 维度上减少的二维矩阵积。

We don't need the 'sum of products' mechanism to do a outer product of two 1d arrays:我们不需要“积和”机制来做两个一维 arrays 的outer积:

In [29]: np.array([1,2,3])[:,None] * np.array([10,100])                         
Out[29]: 
array([[ 10, 100],
       [ 20, 200],
       [ 30, 300]])
In [30]: np.array([1,2,3])[:,None] @ np.array([10,100])[None,:]                 
Out[30]: 
array([[ 10, 100],
       [ 20, 200],
       [ 30, 300]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM