Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. def loss(y, output):
  2. return (y - output)**2
  3.  
  4. def d_loss(y, output):
  5. return 2*(output-y)
  6.  
  7. class NeuralNetwork:
  8. def __init__(self, lr=0.01, beta=0.9):
  9. self.weights1 = np.random.rand(784,126)
  10. self.weights2 = np.random.rand(126,32)
  11. self.weights3 = np.random.rand(32,10)
  12. self.vel1 = 0.0
  13. self.vel2 = 0.0
  14. self.vel3 = 0.0
  15.  
  16. self.lr = lr
  17. self.beta = beta
  18.  
  19. def reinitialize(self):
  20. self.weights1 = np.random.rand(784, 126)
  21. self.weights2 = np.random.rand(126, 32)
  22. self.weights3 = np.random.rand(32, 10)
  23. self.vel1 = 0.0
  24. self.vel2 = 0.0
  25. self.vel3 = 0.0
  26.  
  27. def feedforward(self, x, y):
  28. self.input = x
  29. self.y = y
  30. self.layer1 = sig(np.dot(self.input, self.weights1))
  31. self.layer2 = sig(np.dot(self.layer1, self.weights2))
  32. self.output = sig(np.dot(self.layer2, self.weights3))
  33.  
  34. def backprop(self):
  35. A = d_loss(self.y, self.output) * d_sig(self.output)
  36. d_weights3 = np.dot(self.layer2.T, A )
  37.  
  38. B = (np.dot(A, self.weights3.T) * d_sig(self.layer2))
  39. d_weights2 = np.dot(self.layer1.T, B )
  40.  
  41. C = (np.dot(B, self.weights2.T) * d_sig(self.layer1))
  42. d_weights1 = np.dot(self.input.T, C )
  43.  
  44. self.vel1 = self.beta * self.vel1 + self.lr * d_weights1
  45. self.vel2 = self.beta * self.vel2 + self.lr * d_weights2
  46. self.vel3 = self.beta * self.vel3 + self.lr * d_weights3
  47. self.weights1 -= self.vel1
  48. self.weights2 -= self.vel2
  49. self.weights3 -= self.vel3
  50.  
  51. def prediction(self, x):
  52. self.feedforward(x, y=0)
  53. return self.output[0][0]
  54.  
  55. def accuracy_test(self, X_test, Y_test):
  56. self.correct = 0
  57. self.total = 0
  58. for i in range(len(X_test)):
  59. pred = self.prediction(X_test[i]).argmax()
  60. if pred == Y_test[i]:
  61. self.correct += 1
  62. self.total += 1
  63. return self.correct/self.total * 100
  64. def ca_fonctionne(X):
  65. if X[0] < 0.1:
  66. if X[1] > 0.9:
  67. if X[2] > 0.9:
  68. if X[3] < 0.1:
  69. return True
  70. return False
  71.  
  72. if __name__ == "__main__":
  73. total = 0
  74. correct = 0
  75. X = np.array([[0,0],
  76. [0,1],
  77. [1,0],
  78. [1,1]])
  79. Y = np.array([[0],[1],[1],[0]])
  80.  
  81. taille_ensemble = len(X)
  82. n_test = 1000
  83. epoch = 10
  84. nn = NeuralNetwork()
  85. for test in range(n_test):
  86. nn.reinitialize()
  87.  
  88. for i in range(epoch):
  89. x = np.array([X[i%taille_ensemble]])
  90. y = np.array([Y[i%taille_ensemble]])
  91. nn.feedforward(x,y)
  92. nn.backprop()
  93.  
  94. t = [nn.prediction([[0,0]]), nn.prediction([[1, 0]]), nn.prediction([[0, 1]]), nn.prediction([[1, 1]])]
  95. if ca_fonctionne(t):
  96. correct += 1
  97. total += 1
  98.  
  99. print('----------------------')
  100. print(correct/total * 100)
  101. print(ca_fonctionne(t))
  102. print(nn.prediction([[0, 0]]))
  103. print(nn.prediction([[1, 0]]))
  104. print(nn.prediction([[0, 1]]))
  105. print(nn.prediction([[1, 1]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement