Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. from keras.datasets import mnist
  2. import numpy as np
  3. import operator
  4.  
  5.  
  6. class NN:
  7. def __init__(self, input_size, hidden_size, output_size, rate=0.1):
  8. self.i_dim = input_size
  9. self.h_dim = hidden_size
  10. self.o_dim = output_size
  11. self.W_ih = np.random.rand(self.i_dim, self.h_dim)*0.2-0.1
  12. self.W_ho = np.random.rand(self.h_dim, self.o_dim)*0.2-0.1
  13. self.delta_ih = np.zeros(self.h_dim)
  14. self.delta_ho = np.zeros(self.o_dim)
  15. self.learning_rate = rate
  16. self.avg_cost = 0
  17.  
  18. def forwardPass(self, sample):
  19. # update neuron activation state with one sample
  20. activation_hidden = self.tanh(np.dot(sample, self.W_ih))
  21. activation_output = self.sigmoid(np.dot(activation_hidden, self.W_ho))
  22. return activation_hidden, activation_output
  23.  
  24. def _objective(self, x, y):
  25. _output = self.forwardPass(x)[1]
  26. return np.sum((np.array(_output) - np.array(y))**2)*0.5, np.array(_output)-np.array(y)
  27.  
  28. def objective(self, x, y):
  29. # using batch
  30. val, vec = 0., np.zeros(self.o_dim)
  31. for i in range(len(x)):
  32. t = self._objective(x[i], y[i])
  33. val += t[0]
  34. vec += t[1]
  35. return val, vec
  36.  
  37. def nes(self, x, y, x_test, y_test, npop=10, sigma=0.1, alpha=0.001, seed=0):
  38. np.random.seed(seed)
  39. for i in range(10000):
  40. #if i%10==0:
  41. print('Epoch=', i, 'Error=', self.objective(x, y)[0]/len(x))
  42. if i%20==0:
  43. self.test(x_test, y_test)
  44. N_ih = np.random.randn(npop, self.i_dim, self.h_dim)
  45. N_ho = np.random.randn(npop, self.h_dim, self.o_dim)
  46. R = np.zeros(npop)
  47. for j in range(npop):
  48. new_nn = NN(self.i_dim, self.h_dim, self.o_dim)
  49. new_nn.W_ih = self.W_ih + sigma*N_ih[j]
  50. new_nn.W_ho = self.W_ho + sigma*N_ho[j]
  51. R[j] = self.solution(new_nn, x, y)
  52.  
  53. A = (R - np.mean(R))/ np.std(R)
  54. self.W_ih += alpha / (npop*sigma) * np.dot(N_ih.T, A).T
  55. self.W_ho += alpha / (npop*sigma) * np.dot(N_ho.T, A).T
  56.  
  57. def _bp(self, x, y):
  58. cost, cost_vec = self._objective(x, y)
  59. activation_hidden, activation_output = self.forwardPass(x)
  60. delta_o = cost_vec*activation_output*(1-activation_output)
  61. delta_h = np.dot(self.W_ho, delta_o)*(1-activation_hidden**2)
  62. self.W_ho -= self.learning_rate*delta_o*activation_hidden[:,np.newaxis]
  63. self.W_ih -= self.learning_rate*delta_h*np.array(x)[:,np.newaxis]
  64. return self.W_ho, self.W_ih, cost
  65.  
  66. def bp(self, x, y):
  67. self.avg_cost = 0
  68. t = np.arange(len(x))
  69. np.random.shuffle(t)
  70. for i in t[:100]:
  71. W_ho, W_ih, cost = self._bp(x[i], y[i])
  72. self.avg_cost += cost
  73. self.avg_cost /= len(x)
  74. return self.avg_cost
  75.  
  76. def train(self, x, y, x_test, y_test, epoch=300):
  77. for i in range(epoch):
  78. self.test(x_test, y_test)
  79. self.bp(x, y)
  80. print('Epoch = {} , Average loss = {}'.format(i+1, self.avg_cost))
  81.  
  82. def test(self, x, y):
  83. accuracy = 0
  84. for i in range(len(x)):
  85. output = self.forwardPass(x[i])[1]
  86. index, _ = max(enumerate(output), key=operator.itemgetter(1))
  87. if y[i][index] == 1:
  88. accuracy += 1
  89. print('Test accuracy ={}'.format(accuracy / len(x)))
  90. return float(accuracy)/len(x)
  91.  
  92. @staticmethod
  93. def sigmoid(x):
  94. # used in hidden to output connections
  95. return 1/(1+np.exp(-x))
  96.  
  97. @staticmethod
  98. def tanh(x):
  99. # used in input to hidden connections
  100. return (1-np.exp(-2*x))/(1+np.exp(-2*x))
  101.  
  102. @staticmethod
  103. def solution(nn, x, y):
  104. return -nn.objective(x, y)[0]
  105.  
  106.  
  107. class Evolution:
  108. def __init__(self,
  109. init_size,
  110. learning_rate,
  111. ratio,
  112. power,
  113. innov,
  114. input_dim,
  115. hidden_dim,
  116. output_dim):
  117. self.size = init_size
  118. self.ratio = ratio
  119. self.power = power
  120. self.innov = innov
  121. self.rate = learning_rate
  122. self.current_herd = {}
  123. self.label = init_size+1
  124. self.i_dim, self.h_dim, self.o_dim = input_dim, hidden_dim, output_dim
  125.  
  126. def initializa(self):
  127. for i in range(self.size):
  128. self.current_herd[i] = [NN(self.i_dim, self.h_dim, self.o_dim), float('inf'), i]
  129. return self.current_herd
  130.  
  131. def _evaluate(self, nn, x, y):
  132. return nn._objective(x,y)[0]
  133.  
  134. @staticmethod
  135. def _test_evaluate(nn, x, y):
  136. accuracy = 0
  137. for i in range(len(x)):
  138. output = nn.forwardPass(x[i])[1]
  139. index, _ = max(enumerate(output), key=operator.itemgetter(1))
  140. if y[i][index] == 1:
  141. accuracy += 1
  142. #print('Test accuracy ={}'.format(accuracy / len(x)))
  143. return float(accuracy) / len(x)
  144.  
  145. def evaluate(self, x, y):
  146. avg_fitness = 0
  147. best = 0
  148. for i in self.current_herd:
  149. fitness = self._test_evaluate(self.current_herd[i][0],x, y)
  150. self.current_herd[i][1]=fitness
  151. avg_fitness+=fitness
  152. best = max(best, fitness)
  153. print("average fitness=", avg_fitness/self.size, "best fitness=", best)
  154. return self.current_herd
  155.  
  156. def get_parents(self):
  157. l = sorted(self.current_herd.values(), key=lambda x: -x[1])[:int(self.ratio*self.size)]
  158. new_herd = {}
  159. for i in l:
  160. new_herd[i[2]] = i
  161. self.current_herd = new_herd
  162. return self.current_herd
  163.  
  164. def generation(self):
  165. n = np.random.choice(len(self.current_herd), size=int((1-self.innov)*self.size)-len(self.current_herd))
  166. ind = list(self.current_herd.keys())
  167. for i in n:
  168. nn = self.current_herd[ind[i]][0]
  169. new_nn = NN(self.i_dim, self.h_dim, self.o_dim)
  170. delta_ih = np.random.normal(0,1,nn.W_ih.shape)
  171. delta_ih[abs(delta_ih)>self.power] = 0
  172. delta_ho = np.random.normal(0,1,nn.W_ho.shape)
  173. delta_ho[abs(delta_ho)>self.power] = 0
  174. new_nn.W_ih = nn.W_ih+delta_ih*self.rate
  175. new_nn.W_ho = nn.W_ho+delta_ho*self.rate
  176. self.current_herd[self.label+1] = [new_nn, float('inf'), self.label]
  177. self.label += 1
  178. for i in range(int(self.innov*self.size)):
  179. self.current_herd[self.label+1] = [NN(self.i_dim, self.h_dim, self.o_dim), float('inf'), self.label+1]
  180. self.label += 1
  181. return self.current_herd
  182.  
  183. def evolution(self, n, x, y):
  184. self.initializa()
  185. for i in range(n):
  186. self.evaluate(x, y)
  187. self.get_parents()
  188. self.generation()
  189. #self.rate -= 0.0001*n
  190.  
  191. def test(self, x, y):
  192. accuracy = 0
  193. for i in self.current_herd:
  194. accuracy += self.current_herd[i][0].test(x, y)
  195. print('Test accuracy ={}'.format(accuracy / self.size))
  196.  
  197. if __name__ == '__main__':
  198. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  199.  
  200. x_train = x_train.reshape(60000, 784)
  201. x_test = x_test.reshape(10000, 784)
  202.  
  203. num_classes = 10
  204.  
  205. x_train = x_train.astype('float32')
  206. x_test = x_test.astype('float32')
  207. x_train /= 255
  208. x_test /= 255
  209.  
  210. t_train = np.zeros((y_train.shape[0], num_classes))
  211. t_train[np.arange(y_train.shape[0]), y_train] = 1
  212. y_train = t_train
  213.  
  214. t_test = np.zeros((y_test.shape[0], num_classes))
  215. t_test[np.arange(y_test.shape[0]), y_test] = 1
  216. y_test = t_test
  217.  
  218. nn = NN(784, 100, num_classes)
  219. nn.nes(x_train[:10000], y_train[:10000], x_test[:1000], y_test[:1000])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement