# 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

• answered 2018-11-08 08:09

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

When operating on two arrays, NumPy compares their shapes element-wise. 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,) and `b` has shape (64,1):

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