Home > Mobile >  numpy.dot - Shapes error - Neural Network
numpy.dot - Shapes error - Neural Network

Time:05-31

I am trying to multiply these A1 and W2 matrices (Z2 = W2.dot(A1)):

A1 : [[0.42940542]
 [0.55013895]]
W2 : [[-0.4734037  -0.39642393 -0.05440914 -0.24011293 -0.03670913 -0.37523234]
 [-0.45501004  0.23881832  0.21831658  0.32237388  0.25674681  0.27956714]]

But I am getting this error shapes (2,6) and (2,1) not aligned: 6 (dim 1) != 2 (dim 0), why? Isn't it normal to multiply a (2,1) with a (2,6) matrix?

Because I have a hidden layer with 2 nodes and output layer with 6 nodes

CodePudding user response:

Mathematically this is impossible because your multiplying a (2, 6) matrix by (2, 1). All you need to do is to transpose W2.

P.S: Note that in linear algebra np.dot(W2.T, A1) is not the same as np.dot(A1.T, W2)

import numpy as np

A1 = np.asarray([[0.42940542], [0.55013895]])
W2 = np.asarray([[
    -0.4734037, -0.39642393, -0.05440914, -0.24011293, -0.03670913, -0.37523234
], [-0.45501004, 0.23881832, 0.21831658, 0.32237388, 0.25674681, 0.27956714]])
print(W2.shape, A1.shape)  # (2, 6), (2, 1)
Z2 = W2.T @ A1
print(Z2)

The result would be: [[-0.45360086] [-0.03884332] [ 0.09674087] [ 0.07424463] [ 0.12548332] [-0.00732603]]

  • Related