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

问题描述 投票:0回答:2
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))

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

“” ValueError:形状(4,)和(3,4)未对齐:4(dim 0)!= 3(dim 0)“

numpy尝试执行哪种计算会导致该错误?

注意-我了解矩阵乘法。

感谢您能提供的任何帮助!

编辑-修复了一些变量名

python numpy matrix vector
2个回答
0
投票

a是一个向量(也许是矩阵(1,4),但是您应该改变它的形状)]

b是矩阵(3,4)

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

尝试权重*输入。T或补足输入

尝试输入*权重.T


0
投票

您的示例是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 1-D和b N-D的情况。>

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的第二维到最后一个维度配对,因此我们已将其转置以将(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以不同的方式描述了1d数组的情况,但结果是相同的:

- 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]: [email protected]                                                                          
Out[118]: array([ 2.8  , -1.79 ,  1.885])
© www.soinside.com 2019 - 2024. All rights reserved.