Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numpy import exp, array, random, dot
- class NeuralNetwork():
- def __init__(self):
- random.seed(2)
- self.synaptic_weights = 2 * random.random((1, 5)) - 1
- def __sigmoid(self, x):
- return 1 / (1 + exp(-x))
- def __sigmoid_derivative(self, x):
- return x * (1 - x)
- def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
- for iteration in range(number_of_training_iterations):
- output = self.think(training_set_inputs)
- error = training_set_outputs - output
- adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))
- self.synaptic_weights += adjustment
- def think(self, inputs):
- # Pass inputs through our neural network (our single neuron).
- return self.__sigmoid(dot(inputs, self.synaptic_weights))
- if __name__ == "__main__":
- neural_network = NeuralNetwork()
- print ("Random starting synaptic weights: ")
- print (neural_network.synaptic_weights)
- training_set_inputs = array([[0], [30], [45], [60], [90]])
- training_set_outputs = array([[0], [0.5], [0.707], [0.866], [1]]).T
- neural_network.train(training_set_inputs, training_set_outputs, 10000)
- print ("New synaptic weights after training: ")
- print (neural_network.synaptic_weights)
- print ("Considering new situation 56 -> ?: ")
- print (neural_network.think(array([56])))
Add Comment
Please, Sign In to add comment