title: Single Layer Neural Network for AND Logic Gate (Python)
body: <p>I have been trying to get the following neural network working to act as a simple AND gate but it does not seem to be working. The following is my code:</p>
  
  <pre><code>import numpy as np
  
  def sigmoid(x,derivative=False):
      if(derivative==True):
          return x*(1-x)
      return 1/(1+np.exp(-x))
  
  np.random.seed(1)
  
  weights = np.array([0,0,0])
  
  training = np.array([[[1,1,1],1],
                      [[1,1,0],0],
                      [[1,0,1],0],
                      [[1,0,0],0]])
  
  for iter in xrange(training.shape[0]):
  #forwardPropagation:
          a_layer1 = training[iter][0]
          z_layer2 = np.dot(weights,a_layer1)
          a_layer2 = sigmoid(z_layer2)
          hypothesis_theta = a_layer2
  
  #backPropagation:
          delta_neuron1_layer2 = a_layer2 - training[iter][1]
          Delta_neuron1_layer2 = np.dot(a_layer2,delta_neuron1_layer2)
          update = Delta_neuron1_layer2/training.shape[0]
          weights = weights-update
  
  x = np.array([1,0,1])
  
  print weights
  print sigmoid(np.dot(weights,x))
  </code></pre>
  
  <p>The program above keeps returning strange values as output, with the input X returning a higher value than the array [1,1,1]. The first element of each of the training/testing 'inputs' represents the bias unit. The code was based off of Andrew Ng's videos on his Coursera course on Machine Learning: <a href="https://www.coursera.org/learn/machine-learning" rel="nofollow noreferrer">https://www.coursera.org/learn/machine-learning</a></p>
  
  <p>Thanks in advance for your assistance.</p>