Search code examples
pythonnumpyneural-networkmatrix-synapse

Why aren't shapes aligned?


I have been experimenting with basic neural networks, and I found some python code online. However, when I try to add 2 more hidden layers to the network, I receive an error:

File "python", line 30, in ValueError: shapes (6,4) and (1,4) not aligned: 4 (dim 1) != 1

Can someone please explain what the problem is with this? I haven't managed to find a clear explanation anywhere. Thanks!

Code:

import numpy as np

def nonlin(x,deriv=False):
  if(deriv==True):
    return x*(1-x)
  return 1/(1+np.exp(-x))

X = np.array([[1,0,1],
            [0,1,0],
            [1,1,0],
            [0,0,0],
            [0,0,1],
            [1,1,1]])
y = np.array([[1,1,0,1,0,1]]).T
np.random.seed(1)
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,5)) - 1
syn2 = 2*np.random.random((5,4)) - 1
syn3 = 2*np.random.random((4,1)) - 1
for iter in range(100000):
  l0 = X
  l1 = nonlin(np.dot(l0,syn0))
  l2 = nonlin(np.dot(l1,syn1))
  l3 = nonlin(np.dot(l2,syn2))
  l4 = nonlin(np.dot(l3,syn3))

  l4_error = y - l4
  l4_delta = l4_error*nonlin(l3,deriv=True)

  l3_error = l4_delta.dot(syn3.T)
  l3_delta = l3_error*nonlin(l3,deriv=True)

  l2_error = l3_delta.dot(syn2.T)
  l2_delta = l2_error*nonlin(l2,deriv=True)

  l1_error = l2_delta.dot(syn1.T)
  l1_delta = l1_error * nonlin(l1,deriv=True)

  syn3 += l3.T.dot(l4_delta)
  syn2 += l2.T.dot(l3_delta)
  syn1 += l1.T.dot(l2_delta)
  syn0 += l0.T.dot(l1_delta)
print("Neural network trained:")
while True:
  l0 = np.array([[int(i) for i in input().split()]])
  l1 = nonlin(np.dot(l0,syn0))
  l2 = nonlin(np.dot(l1,syn1))
  l3 = nonlin(np.dot(l2,syn2))
  l4 = nonlin(np.dot(l3,syn3))
  print("Output:")
  if l4[0][0] > 0.5:
      print("Yes")
  else:
    print("No")

Solution

  • Shape of X is (3,6) so shape of l0 is the same and shape of syn0 is (3,4).

    So in line 22 np.(dot0,syn0) they already fail to be dot and raise a ValueError which says shape not align.

    You should transpose l0 so its shape become (6,3) then they can be not.

    Read numpy dot doc,when use numpy.dot(a, b, out=None)

    the last axis of a and the second-to-last of b

    need to be equal.Then you can matrix multiplication