Guest User

Untitled

a guest
Aug 11th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. from numpy import exp, array, random, dot
  2.  
  3.  
  4. class NeuralNetwork():
  5. def __init__(self):
  6.  
  7. random.seed(2)
  8.  
  9.  
  10. self.synaptic_weights = 2 * random.random((1, 5)) - 1
  11.  
  12. def __sigmoid(self, x):
  13. return 1 / (1 + exp(-x))
  14.  
  15. def __sigmoid_derivative(self, x):
  16. return x * (1 - x)
  17.  
  18. def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
  19. for iteration in range(number_of_training_iterations):
  20. output = self.think(training_set_inputs)
  21.  
  22. error = training_set_outputs - output
  23.  
  24. adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))
  25.  
  26. self.synaptic_weights += adjustment
  27.  
  28. def think(self, inputs):
  29. # Pass inputs through our neural network (our single neuron).
  30. return self.__sigmoid(dot(inputs, self.synaptic_weights))
  31.  
  32.  
  33. if __name__ == "__main__":
  34.  
  35. neural_network = NeuralNetwork()
  36.  
  37. print ("Random starting synaptic weights: ")
  38. print (neural_network.synaptic_weights)
  39.  
  40. training_set_inputs = array([[0], [30], [45], [60], [90]])
  41. training_set_outputs = array([[0], [0.5], [0.707], [0.866], [1]]).T
  42.  
  43. neural_network.train(training_set_inputs, training_set_outputs, 10000)
  44.  
  45. print ("New synaptic weights after training: ")
  46. print (neural_network.synaptic_weights)
  47.  
  48. print ("Considering new situation 56 -> ?: ")
  49. print (neural_network.think(array([56])))
Add Comment
Please, Sign In to add comment