Advertisement
lancernik

ADPNeur

May 28th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def sigmoid(x):
  4. # Sigmoid activation function: f(x) = 1 / (1 + e^(-x))
  5. return 1 / (1 + np.exp(-x))
  6.  
  7. def deriv_sigmoid(x):
  8. # Derivative of sigmoid: f'(x) = f(x) * (1 - f(x))
  9. fx = sigmoid(x)
  10. return fx * (1 - fx)
  11.  
  12. def mse_loss(y_true, y_pred):
  13. # y_true and y_pred are numpy arrays of the same length.
  14. return ((y_true - y_pred) ** 2).mean()
  15.  
  16. class OurNeuralNetwork:
  17. '''
  18. A neural network with:
  19. - 2 inputs
  20. - a hidden layer with 2 neurons (h1, h2)
  21. - an output layer with 1 neuron (o1)
  22. *** DISCLAIMER ***:
  23. The code below is intended to be simple and educational, NOT optimal.
  24. Real neural net code looks nothing like this. DO NOT use this code.
  25. Instead, read/run it to understand how this specific network works.
  26. '''
  27. def __init__(self):
  28. # Weights
  29. self.w1 = np.random.normal()
  30. self.w2 = np.random.normal()
  31. self.w3 = np.random.normal()
  32. self.w4 = np.random.normal()
  33. self.w5 = np.random.normal()
  34. self.w6 = np.random.normal()
  35.  
  36. # Biases
  37. self.b1 = np.random.normal()
  38. self.b2 = np.random.normal()
  39. self.b3 = np.random.normal()
  40.  
  41. def feedforward(self, x):
  42. # x is a numpy array with 2 elements.
  43. h1 = sigmoid(self.w1 * x[0] + self.w2 * x[1] + self.b1)
  44. h2 = sigmoid(self.w3 * x[0] + self.w4 * x[1] + self.b2)
  45. o1 = sigmoid(self.w5 * h1 + self.w6 * h2 + self.b3)
  46. return o1
  47.  
  48. def train(self, data, all_y_trues):
  49. '''
  50. - data is a (n x 2) numpy array, n = # of samples in the dataset.
  51. - all_y_trues is a numpy array with n elements.
  52. Elements in all_y_trues correspond to those in data.
  53. '''
  54. learn_rate = 0.01
  55. epochs = 100000 # number of times to loop through the entire dataset
  56.  
  57. for epoch in range(epochs):
  58. for x, y_true in zip(data, all_y_trues):
  59. # --- Do a feedforward (we'll need these values later)
  60. sum_h1 = self.w1 * x[0] + self.w2 * x[1] + self.b1
  61. h1 = sigmoid(sum_h1)
  62.  
  63. sum_h2 = self.w3 * x[0] + self.w4 * x[1] + self.b2
  64. h2 = sigmoid(sum_h2)
  65.  
  66. sum_o1 = self.w5 * h1 + self.w6 * h2 + self.b3
  67. o1 = sigmoid(sum_o1)
  68. y_pred = o1
  69.  
  70. # --- Calculate partial derivatives.
  71. # --- Naming: d_L_d_w1 represents "partial L / partial w1"
  72. d_L_d_ypred = -2 * (y_true - y_pred)
  73.  
  74. # Neuron o1
  75. d_ypred_d_w5 = h1 * deriv_sigmoid(sum_o1)
  76. d_ypred_d_w6 = h2 * deriv_sigmoid(sum_o1)
  77. d_ypred_d_b3 = deriv_sigmoid(sum_o1)
  78.  
  79. d_ypred_d_h1 = self.w5 * deriv_sigmoid(sum_o1)
  80. d_ypred_d_h2 = self.w6 * deriv_sigmoid(sum_o1)
  81.  
  82. # Neuron h1
  83. d_h1_d_w1 = x[0] * deriv_sigmoid(sum_h1)
  84. d_h1_d_w2 = x[1] * deriv_sigmoid(sum_h1)
  85. d_h1_d_b1 = deriv_sigmoid(sum_h1)
  86.  
  87. # Neuron h2
  88. d_h2_d_w3 = x[0] * deriv_sigmoid(sum_h2)
  89. d_h2_d_w4 = x[1] * deriv_sigmoid(sum_h2)
  90. d_h2_d_b2 = deriv_sigmoid(sum_h2)
  91.  
  92. # --- Update weights and biases
  93. # Neuron h1
  94. self.w1 -= learn_rate * d_L_d_ypred * d_ypred_d_h1 * d_h1_d_w1
  95. self.w2 -= learn_rate * d_L_d_ypred * d_ypred_d_h1 * d_h1_d_w2
  96. self.b1 -= learn_rate * d_L_d_ypred * d_ypred_d_h1 * d_h1_d_b1
  97.  
  98. # Neuron h2
  99. self.w3 -= learn_rate * d_L_d_ypred * d_ypred_d_h2 * d_h2_d_w3
  100. self.w4 -= learn_rate * d_L_d_ypred * d_ypred_d_h2 * d_h2_d_w4
  101. self.b2 -= learn_rate * d_L_d_ypred * d_ypred_d_h2 * d_h2_d_b2
  102.  
  103. # Neuron o1
  104. self.w5 -= learn_rate * d_L_d_ypred * d_ypred_d_w5
  105. self.w6 -= learn_rate * d_L_d_ypred * d_ypred_d_w6
  106. self.b3 -= learn_rate * d_L_d_ypred * d_ypred_d_b3
  107.  
  108. # --- Calculate total loss at the end of each epoch
  109. if epoch % 10 == 0:
  110. y_preds = np.apply_along_axis(self.feedforward, 1, data)
  111. loss = mse_loss(all_y_trues, y_preds)
  112. print("Epoch %d loss: %.3f" % (epoch, loss))
  113.  
  114. # Define dataset
  115. data = np.array([
  116. [-2, -1], # Alice
  117. [25, 6], # Bob
  118. [17, 4], # Charlie
  119. [-15, -6], # Diana
  120. ])
  121. all_y_trues = np.array([
  122. 1, # Alice
  123. 0, # Bob
  124. 0, # Charlie
  125. 1, # Diana
  126. ])
  127.  
  128. # Train our neural network!
  129. network = OurNeuralNetwork()
  130. network.train(data, all_y_trues)
  131.  
  132. # Make some predictions
  133. emily = np.array([-7, -3]) # 128 pounds, 63 inches
  134. frank = np.array([20, 2]) # 155 pounds, 68 inches
  135. print("Emily: %.3f" % network.feedforward(emily)) # 0.951 - F
  136. print("Frank: %.3f" % network.feedforward(frank)) # 0.039 - M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement