Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import numpy as np
  2.  
  3. trg_inputs = np.array([[0,0,1], [1,1,1],[1,0,1],[0,1,1]])
  4. trg_outputs = np.array([[0,1,1,0]]).T
  5.  
  6. # single layer
  7. class NeuralNetwork:
  8. def __init__(self):
  9. np.random.seed(1)
  10. self.synaptic_weights = 2 * np.random.random((3,1)) - 1 #the randomiser gives values between 0 and 1
  11.  
  12. def __sigmoid(self, x):
  13. return 1/(1+np.exp(-x))
  14.  
  15. def __sigmoid_derivative(self, x):
  16. return x*(1-x)
  17.  
  18. def train(self, trg_inputs, trg_outputs,iter_n):
  19. for iteration in range(iter_n):
  20. raw, output = self.predict(trg_inputs)
  21. error = trg_outputs - output
  22. adj = np.dot(trg_inputs.T , error * self.__sigmoid_derivative(raw))
  23. self.synaptic_weights += adj
  24.  
  25. def predict(self,inputs):
  26. raw = np.dot(inputs, self.synaptic_weights)
  27. return raw, self.__sigmoid(raw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement