numpy invalid dot product shape
As part of my exercise with Numpy, i tried below code.
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 ("")
My output was as below:
((64, 32), (32,))
((64, 64), (64,))
(64,)
If I interpret, then
(64, 32 ) * (32, ) => (64, )
(64, 64 ) * (64, ) => (64, )
(64, ) + (64, ) => (64, )
So far good, Now i change extraInput Shape to #appending '1'
extraInput = np.random.random((outputAttributes, 1)
Now, i got the result which i m unable to understand.
((64, 32), (32,))
((64, 64), (64, 1))
(64, 64)
If I interpret, then
(64, 32 ) * (32, ) => (64, )
(64, 64 ) * (64,1) => (64,1)
(64, ) + (64, 1) => (64, 64 )
HOW (64,) + (64, 1) LEADS TO (64,64) ?
1 answer

https://docs.scipy.org/doc/numpy1.13.0/user/basics.broadcasting.html#generalbroadcastingrules
When operating on two arrays, NumPy compares their shapes elementwise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when
1. they are equal, or 2. one of them is 1
The last dimension of one of your arrays is 1, invoking rule 2.
If you want to keep the array shape as
(64,)
or as(64, 1)
I would suggest being explicit:Assuming
a
has shape (64,) andb
has shape (64,1):a + b[:,0] # shape (64,) a[:,np.newaxis] + b # shape (64, 1)