Created
August 17, 2015 05:42
-
-
Save miloharper/ef3f1f2fdcd6a44a3b15 to your computer and use it in GitHub Desktop.
Training the neural network using matrices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We train the neural network through a process of trial and error. | |
# Adjusting the synaptic weights each time. | |
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations): | |
for iteration in xrange(number_of_training_iterations): | |
# Pass the training set through our neural network | |
output_from_layer_1, output_from_layer_2 = self.think(training_set_inputs) | |
# Calculate the error for layer 2 (The difference between the desired output | |
# and the predicted output). | |
layer2_error = training_set_outputs - output_from_layer_2 | |
layer2_delta = layer2_error * self.__sigmoid_derivative(output_from_layer_2) | |
# Calculate the error for layer 1 (By looking at the weights in layer 1, | |
# we can determine by how much layer 1 contributed to the error in layer 2). | |
layer1_error = layer2_delta.dot(self.layer2.synaptic_weights.T) | |
layer1_delta = layer1_error * self.__sigmoid_derivative(output_from_layer_1) | |
# Calculate how much to adjust the weights by | |
layer1_adjustment = training_set_inputs.T.dot(layer1_delta) | |
layer2_adjustment = output_from_layer_1.T.dot(layer2_delta) | |
# Adjust the weights. | |
self.layer1.synaptic_weights += layer1_adjustment | |
self.layer2.synaptic_weights += layer2_adjustment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment