繁体   English   中英

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

[英]What causes the shape error in numpy.dot when a vector is multiplied with a matrix?

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))

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

“ValueError:形状(4,)和(3,4)未对齐:4(dim 0)!= 3(dim 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 ND 的情况。

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以不同的方式描述一维数组的情况,但结果是相同的:

- 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),但你应该重塑它也许)

b 是一个矩阵 (3,4)

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

尝试 weights * inputs.T 或 respahe 输入

尝试输入* weights.T

暂无
暂无

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

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