当向量与矩阵相乘时,是什么导致numpy.dot中的形状错误?


a= [1, 2, 3, 2.5]
b= [[0.2, 0.8, -0.5, 1.0],
[0.5, -0.91, 0.26, -0.5],
[-0.26, -0.27, 0.17, 0.87]]
print(np.dot(b,a))
print(np.dot(a,b))

为什么打印第一行,但第二行导致形状对齐错误?

"值错误:形状 (4,( 和 (3,4( 未对齐:4(暗淡 0(!= 3(暗淡 0(">

numpy尝试执行什么计算会导致该错误?

注意 - 我理解矩阵乘法。

感谢您提供的任何帮助!

编辑 - 修复了一些变量名称

您的示例是 np.dot 文档中的一个案例:

dot(a, b, out=None)
Dot product of two arrays. Specifically,
- If `a` is an N-D array and `b` is a 1-D array, it is a sum product over
the last axis of `a` and `b`.

它没有列出a一维和b一维的情况。

In [106]: a= np.array([1, 2, 3, 2.5]) 
...: b= np.array([[0.2, 0.8, -0.5, 1.0], 
...:     [0.5, -0.91, 0.26, -0.5], 
...:     [-0.26, -0.27, 0.17, 0.87]])                                               
In [107]: a.shape, b.shape                                                               
Out[107]: ((4,), (3, 4))
In [108]: np.dot(b, a)                                                                   
Out[108]: array([ 2.8  , -1.79 ,  1.885])

einsum表示法中,请注意公共j索引(两者的最后一个轴(

In [109]: np.einsum('ij,j->i', b, a)                                                     
Out[109]: array([ 2.8  , -1.79 ,  1.885])

a可以是 1d,但它与b的 2 维到最后一维配对,因此我们将其转置为将 (4,( 与 (4,3( 匹配:

In [113]: np.einsum('i,ij', a, b.T)                                                      
Out[113]: array([ 2.8  , -1.79 ,  1.885])
In [114]: np.dot(a,b.T)                                                                  
Out[114]: array([ 2.8  , -1.79 ,  1.885])

@matmul以不同的方式描述一维数组的情况,但结果是相同的:

- If the first argument is 1-D, it is promoted to a matrix by
prepending a 1 to its dimensions. After matrix multiplication
the prepended 1 is removed.
- If the second argument is 1-D, it is promoted to a matrix by
appending a 1 to its dimensions. After matrix multiplication
the appended 1 is removed.
In [117]: b@a                                                                            
Out[117]: array([ 2.8  , -1.79 ,  1.885])
In [118]: a@b.T                                                                          
Out[118]: array([ 2.8  , -1.79 ,  1.885])

a 是一个向量(也许是矩阵 (1,4(,但 u 应该重塑它(

b 是矩阵 (3,4(

所以你只能做 (3,4(x(4,1( 或 (1,4(x(4,3(

尝试权重 * 输入。T 或重新输入

尝试输入 * 权重。T