image_neural_net

Animal recognition using a neural network.
Log | Files | Refs | README

train_neural_net.py (1604B)


      1 import numpy as np
      2 
      3 inputSize = 64
      4 hiddenLayer = 64
      5 hiddenLayer2 = 16
      6 outputSize = 4
      7 
      8 iterations = 30000
      9 epochs = 2
     10 
     11 def sigmoid(x, deriv = False):
     12     if (deriv == False):
     13         x = np.clip(x, -500, 500)
     14         result = 1/(1+np.exp(-x))
     15     else:
     16         result = x*(1-x)
     17     return result
     18 
     19 x = np.load('../train/training.npy')
     20 y = np.load('../train/trainingLabels.npy')
     21 
     22 syn0 = 2*np.random.random((inputSize, hiddenLayer)) - 1
     23 syn1 = 2*np.random.random((hiddenLayer, hiddenLayer2)) - 1
     24 syn2 = 2*np.random.random((hiddenLayer2, outputSize)) - 1
     25 
     26 for j in range(epochs):
     27     print ("EPOCH " + str(j + 1))
     28     for i in range(iterations):
     29         inputLayer = x
     30         layer1 = sigmoid(np.dot(inputLayer, syn0))
     31         layer2 = sigmoid(np.dot(layer1, syn1))
     32         layer3 = sigmoid(np.dot(layer2, syn2))
     33         layer3Err = layer3 - y
     34     
     35         if (i % 1000) == 0:
     36             print ("Error at iteration " + str(i) + ": " + str(np.mean(np.abs(layer3Err))))
     37     
     38         layer3Delta = layer3Err * sigmoid(layer3, True)
     39         layer2Err = layer3Delta.dot(syn2.T)
     40 
     41         layer2Delta = layer2Err * sigmoid(layer2, True)
     42         layer1Err = layer2Delta.dot(syn1.T)
     43 
     44         layer1Delta = layer1Err * sigmoid(layer1, True)
     45     
     46         syn2 -= layer2.T.dot(layer3Delta)
     47         syn1 -= layer1.T.dot(layer2Delta)
     48         syn0 -= inputLayer.T.dot(layer1Delta)
     49 
     50 print ("Training Output:")
     51 print (layer3)
     52 print ("Training Accuracy: " + str(100 - (np.mean(np.abs(layer3Err)) * 100)) + "%")
     53 
     54 np.savetxt('../neuralnet/syn0', syn0)
     55 np.savetxt('../neuralnet/syn1', syn1)
     56 np.savetxt('../neuralnet/syn2', syn2)