Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import numpy as np
  2. import time
  3. import sys
  4.  
  5. class NeuralNetwork:
  6.     def __init__ (self, layers, eps = 0.3):
  7.         self.w = []
  8.         self.eta = 0.3
  9.         self.learningStepCount = 500
  10.         for i in range(1, len(layers)):
  11.             self.w.append(np.matrix(eps * (2 * np.random.random((layers[i], layers[i - 1])) -  1)))        
  12.                                          
  13.     def sigmoid(self, x):
  14.         return 1.0 / (1 + np.exp(-x))
  15.  
  16.     def getOutput(self, x):
  17.         for layer in self.w:
  18.             x = self.sigmoid(layer * x)
  19.         ind = x.argmax()
  20.         x = np.zeros(len(x))
  21.         x[ind] = 1
  22.         return x
  23.  
  24.     def gradient(self, x):
  25.         return np.multiply(x, 1 - x)
  26.     #@profile
  27.     def train(self, testX, testY):
  28.         for step in range(self.learningStepCount):
  29.             for xInput, y in zip(testX, testY):
  30.                 x = []
  31.                 for layer in self.w:
  32.                     x.append(xInput)
  33.                     xInput = self.sigmoid(layer * xInput)
  34.  
  35.                 output = xInput
  36.                 delta = np.multiply(self.gradient(output), (y - output))
  37.                
  38.                 for i in reversed(range(len(self.w))):
  39.                     self.w[i] += self.eta * delta * x[i].T  
  40.                     delta = np.multiply(self.gradient(x[i]), self.w[i].T * delta)                
  41.             print(step)                    
  42. #
  43. @profile
  44. def main():
  45.     x, y = [], []
  46.     tests = []
  47.     f = open('train.csv', 'r')  
  48.     for line in f:
  49.         tests.append(np.array(list(map(int, line.split(',')))))
  50.     f.close()
  51.  
  52.     np.random.shuffle(tests)
  53.     for test in tests:
  54.         x.append(np.matrix(test[1:]).T)
  55.         tmp = [0] * 10
  56.         tmp[int(test[0]) - 1] = 1
  57.         y.append(np.matrix(tmp).T)
  58.                    
  59.     netw = NeuralNetwork([784, 2000, 10])
  60.     tests = tests[:len(tests) // 10]
  61.     testCount = len(tests)    
  62.     print(testCount)
  63.     tests.clear()
  64.    
  65.     netw.train(x[:testCount // 2], y[:testCount // 2])
  66.     good = 0
  67.     for i in range(testCount // 2, testCount):
  68.         if (netw.getOutput(x[i]) == y[i]).all():
  69.             good += 1    
  70.  
  71.     print('Spent', time.time() - timer, 'seconds')                                                                                    
  72.     print('Accuracy:', good * 1.0 / testCount * 2)
  73.  
  74. if __name__ == '__main__':
  75.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement