Train the Network
Define the training function and update weights based on errors.
We'll cover the following...
Training function
Now, let’s tackle the more involved training task. There are two parts to this:
-
The first part is working out the output for a given training example. That is no different from what we just did with the
query()
function. -
The second part is taking this calculated output, comparing it with the desired output, and using the difference to guide how the network weights are updated.
We’ve already done the first part, so let’s write that out:
# train the neural networkdef train(self, inputs_list, targets_list):# convert inputs list to 2d arrayinputs = numpy.array(inputs_list, ndmin=2).Ttargets = numpy.array(targets_list, ndmin=2).T# calculate signals into hidden layerhidden_inputs = numpy.dot(self.wih, inputs)# calculate the signals emerging from hidden layerhidden_outputs = self.activation_function(hidden_inputs)# calculate signals into final output layerfinal_inputs = numpy.dot(self.who, hidden_outputs)# calculate the signals emerging from final output layerfinal_outputs = self.activation_function(final_inputs)pass
This code is almost exactly the same as the code in the query()
function, because we’re feeding the signal forward from the input layer to the final output layer in exactly the same way.
The only difference is that we have an additional parameter, targets_list
, defined in the function name, because we can’t train the network without the training examples that include the desired or target answer.
def train(self, inputs_list, targets_list)
The code also turns the targets_list
into a NumPy array, just like the inputs_list
is turned into a NumPy array.
targets = numpy.array(targets_list, ndmin=2).T
Update weights based on error
Now we are getting closer to the heart of the neural network’s inner workings—improving the weights based on the error between the calculated and target output.
Let’s do this in manageable steps.
First, we need to calculate the error, which is the difference between the desired target output provided by the training example and the actual calculated output. That’s the difference between the matrices , element by element. The Python code for this is really simple, showing again the elegant power of matrices:
# error is the (target - actual)output_errors = targets - final_outputs
We can calculate the backpropagated errors for the hidden layer nodes. Remember how we split the errors according to the connected weights, and recombined them for each hidden layer node. We worked out the matrix ...