Guest User

Untitled

a guest
Oct 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. network = {
  2. 1: 28 * 28,
  3. 2: 45,
  4. 3: 10
  5. }
  6.  
  7. def sigmoid(value):
  8. return 1.0/(1.0 + math.exp(-value))
  9.  
  10.  
  11. def train_network(_data_set, _theta1, _theta2, _should_output_deltas):
  12.  
  13. # Vectorizing the sigmoid method
  14. sigmoid_vec = np.vectorize(sigmoid)
  15.  
  16. # Initializing the deltas (accumulators)
  17. delta1 = np.zeros(np.shape(_theta1))
  18. delta2 = np.zeros(np.shape(_theta2))
  19.  
  20. # initializing lists to cache the outputs at every iteration
  21. h_x = []
  22. y = []
  23.  
  24. mean_train = 0
  25. for k in range(len(_data_set)):
  26. mean_train += np.mean(_data_set[k][0])
  27. mean_train /= len(_data_set)
  28.  
  29. for i in xrange(len(_data_set)):
  30.  
  31. if i in batches and _should_output_deltas:
  32. print('%d iterations complete...' % (i + 1))
  33.  
  34. # Subtract the mean from each element in the matrix and divide by 255
  35. a1 = np.divide((_data_set[i][0] - mean_train), 255)
  36.  
  37. # Insert the bias unit
  38. a1 = np.insert(a1, 0, 1).reshape(network[1] + 1, 1)
  39.  
  40. # Compute the sigmoid
  41. z2 = sigmoid_vec(np.matmul(_theta1, a1))
  42.  
  43. # Add the bias unit
  44. a2 = np.insert(z2, 0, 1).reshape(network[2] + 1, 1)
  45.  
  46. # Compute the sigmoid of the output
  47. z3 = np.matmul(_theta2, a2)
  48. a3 = sigmoid_vec(z3)
  49.  
  50. # Softmax -> Commented out due to confusion in usage
  51. # a3 = softmax(a3)
  52. # print('After softmax, a3 = %s' % str(a3))
  53.  
  54. # Reshaping to a column vector
  55. label = np.reshape(_data_set[i][1], (10, 1))
  56.  
  57. # Caching
  58. y.append(label)
  59. h_x.append(a3)
  60.  
  61. # Calculate d3
  62. d3 = np.subtract(a3, label)
  63.  
  64. # Calculate d2
  65. _d2 = np.multiply(np.matmul(np.transpose(_theta2), d3), np.multiply(a2, 1 - a2))
  66.  
  67. # Remove d2[0]
  68. _d2 = np.delete(_d2, 0, axis=0)
  69.  
  70. # Accumulate in delta matrices
  71. delta2 += (np.matmul(d3, np.transpose(a2)))
  72. delta1 += (np.matmul(_d2, np.transpose(a1)))
  73.  
  74. if _should_output_deltas:
  75. delta1 *= 1/(len(_data_set))
  76. delta2 *= 1/(len(_data_set))
  77.  
  78. return delta1, delta2
  79. else:
  80. return compute_cost(np.asarray(h_x), np.asarray(y), len(_data_set))
  81.  
  82. def softmax(matrix):
  83. """
  84. A very redundant & innefecient method to calculate
  85. the softmax outputs of the column vector
  86. """
  87. shape = np.shape(matrix)
  88. t = matrix.flatten().tolist()
  89. e_t = [math.exp(_z) for _z in t]
  90. sum_e_t = sum(e_t)
  91. soft_e_t = [i/sum_e_t for i in e_t]
  92. return np.asarray(soft_e_t).reshape(shape)
  93.  
  94. def compute_cost(h_x, y, n):
  95. cost = 0
  96. for i in range(n):
  97. cost += (-(y[i][0] * math.log(h_x[i][0])) - ((1 - y[i][0]) * math.log(1 - h_x[i][0])))
  98. cost *= 1/n
  99. return cost
  100.  
  101. cost_iter = {}
  102.  
  103. # Print debugging
  104. print('Performing gradient descent for %d iterations with %f learning rate...' % (_max_iter, _alpha))
  105.  
  106. for i in range(max_iter):
  107. _theta1 -= np.multiply(alpha, _d1)
  108. _theta2 -= np.multiply(alpha, _d2)
  109. # Current cost
  110. cost = train_network(training_data, _theta1, _theta2, False)
  111. # Cache the cost.
  112. cost_iter[i] = cost
  113.  
  114. # If reduction in cost is less than 10^-4, stop.
  115. if i >= 1 and (cost_iter[i-1] - cost_iter[i] <= 0.0001):
  116. print("It seems that the cost isn't converging quickly. Breaking loop at iteration %d..." % (i+1))
  117. del cost_iter[i]
  118. break
  119.  
  120. print('Iteration:%dtCost:%f' % (i, cost))
  121.  
  122. return _theta1, _theta2, cost_iter
  123.  
  124. def test_network(optimal_theta1, optimal_theta2, _test_set):
  125. """
  126. Very similar to the 'train' method, but
  127. back-propagation is not done here.
  128.  
  129. Instead, a counter will maintain how many times
  130. the neural network predicted the right answer, and return the
  131. accuracy.
  132. """
  133.  
  134. print('Now in the testing phase...')
  135.  
  136. if _test_set is None:
  137. raise ValueError('Test data or test labels are null')
  138.  
  139. sigmoid_vec = np.vectorize(sigmoid)
  140.  
  141. hit_counter = 0
  142.  
  143. mean_test = 0
  144. for __k in range(len(_test_set)):
  145. mean_test += np.mean(_test_set[__k][0])
  146.  
  147. mean_test /= len(_test_set)
  148.  
  149. # Only iterating over 150 examples for now
  150. for k in range(150):
  151. a1 = np.divide((_test_set[k][0] - mean_test), 255)
  152. a1 = np.insert(a1, 0, 1).reshape(network[1] + 1, 1)
  153. z2 = sigmoid_vec(np.matmul(optimal_theta1, a1))
  154.  
  155. a2 = np.insert(z2, 0, 1).reshape(network[2] + 1, 1)
  156. z3 = np.matmul(optimal_theta2, a2)
  157.  
  158. a3 = sigmoid_vec(z3)
  159. # print(a3)
  160.  
  161. # Take the softmax?!
  162. # a3 = softmax(a3)
  163.  
  164. label = np.reshape(_test_set[k][1], (10, 1))
  165.  
  166. print('(Computed) %s = (Actual) %s' % (str(np.argmax(a3)), str(np.argmax(label))))
  167. if np.argmax(a3) == np.argmax(label):
  168. hit_counter += 1
  169.  
  170. # cache = np.reshape(cache, (28, 28))
  171. # plt.imshow(cache, interpolation='nearest')
  172. # plt.show()
  173.  
  174. print('Hits: %d' % hit_counter)
  175.  
  176. (Computed) 0 = (Actual) 7
  177. (Computed) 1 = (Actual) 7
  178. (Computed) 0 = (Actual) 8
  179. (Computed) 1 = (Actual) 1
  180. (Computed) 1 = (Actual) 3
  181. (Computed) 1 = (Actual) 4
  182. (Computed) 1 = (Actual) 9
  183. (Computed) 0 = (Actual) 3
  184. (Computed) 1 = (Actual) 1
  185. (Computed) 0 = (Actual) 7
  186. (Computed) 0 = (Actual) 3
  187. (Computed) 1 = (Actual) 7
  188. (Computed) 0 = (Actual) 3
  189. (Computed) 1 = (Actual) 3
  190. (Computed) 1 = (Actual) 9
  191. (Computed) 1 = (Actual) 3
  192. (Computed) 0 = (Actual) 5
  193. (Computed) 1 = (Actual) 1
  194. (Computed) 0 = (Actual) 9
  195. (Computed) 0 = (Actual) 4
  196. (Computed) 0 = (Actual) 9
  197. (Computed) 0 = (Actual) 1
  198. (Computed) 1 = (Actual) 1
  199. (Computed) 0 = (Actual) 7
  200. (Computed) 0 = (Actual) 3
  201. (Computed) 0 = (Actual) 2
  202. (Computed) 1 = (Actual) 8
  203. (Computed) 0 = (Actual) 5
  204. (Computed) 0 = (Actual) 3
  205. (Computed) 0 = (Actual) 1
  206. (Computed) 1 = (Actual) 2
  207. (Computed) 0 = (Actual) 7
  208. (Computed) 0 = (Actual) 5
  209. (Computed) 1 = (Actual) 7
  210. (Computed) 1 = (Actual) 7
  211. (Computed) 0 = (Actual) 5
  212. (Computed) 1 = (Actual) 1
  213. (Computed) 0 = (Actual) 5
  214. (Computed) 0 = (Actual) 9
  215. (Computed) 1 = (Actual) 1
  216. (Computed) 1 = (Actual) 6
Add Comment
Please, Sign In to add comment