Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class NeuralNetwork:
  4. def __init__(self, x, y):
  5. self.input = x
  6. self.weights1 = np.random.rand(self.input.shape[1], 4)
  7. self.weights2 = np.random.rand(4, 1)
  8. self.y = y
  9. self.output = np.zeros(self.y.shape)
  10.  
  11. def feedforward(self):
  12. self.layer1 = sigmoid(np.dot(self.input, self.weights1))
  13. self.output = sigmoid(np.dot(self.layer1, self.weights2))
  14.  
  15. def backprop(self):
  16. d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
  17. d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) *
  18. sigmoid_derivative(self.output), self.weights2.T) *
  19. sigmoid_derivative(self.layer1)))
  20.  
  21. self.weights1 += learning_rate*d_weights1
  22. self.weights2 += learning_rate*d_weights2
  23.  
  24. def train(self):
  25. self.output = self.feedforward()
  26. self.backprop()
  27.  
  28. def sigmoid(t): return 1/(1+np.exp(-t))
  29. def sigmoid_derivative(p): return p*(1-p)
  30.  
  31. def loss(y, y_out):
  32. return np.mean(np.square(y - y_out))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement