numpy无效的点积形状

时间:2018-11-08 08:03:02

标签: python numpy

作为与Numpy练习的一部分,我尝试了以下代码。

import numpy as np

inputAttributes  = 32
outputAttributes = 64
noOfElements = 3


inputData = np.random.random((noOfElements, inputAttributes))
weights = np.random.random((outputAttributes, inputAttributes))

extrawegiths = np.random.random((outputAttributes, outputAttributes))
extraInput = np.random.random((outputAttributes,))

eachLayerOutput =[]

for eachData in inputData:
    print ("---------------")
    print (weights.shape, eachData.shape)
    print (extrawegiths.shape, extraInput.shape)
    result = np.dot(weights,eachData)  +  np.dot(extrawegiths, extraInput)          
    print (result.shape)
    print ("---------------")

我的输出如下:

((64, 32), (32,))
((64, 64), (64,))
(64,)

如果我解释,那么

  (64, 32 ) * (32, ) => (64, )

  (64, 64 ) * (64, ) => (64, )

  (64,    ) + (64, ) => (64, )

到目前为止很好,现在我将extraInput Shape更改为 #appending'1'

extraInput = np.random.random((outputAttributes, 1)

现在,我得到了我无法理解的结果。

((64, 32), (32,))
((64, 64), (64, 1))
(64, 64)

如果我解释,那么

  (64, 32 ) * (32, ) => (64, )

  (64, 64 ) * (64,1) => (64,1)

  (64,    ) + (64, 1) => (64, 64 )

如何(64,)+(64,1)领先于(64,64)?

1 个答案:

答案 0 :(得分:1)

https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html#general-broadcasting-rules

  

在两个数组上操作时,NumPy逐元素比较它们的形状。它从跟踪维度开始,一直向前发展。何时兼容两个维度

   1. they are equal, or
   2. one of them is 1

其中一个数组的最后一个维度是1,调用规则2。

如果您想将数组形状保持为(64,)(64, 1),我建议您保持明确:

假设a的形状为(64,),而b的形状为(64,1):

a + b[:,0]          # shape (64,)
a[:,np.newaxis] + b # shape (64, 1)