Train the Network

Define the training function and update weights based on errors.

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:

Press + to interact
# train the neural network
def train(self, inputs_list, targets_list):
# convert inputs list to 2d array
inputs = numpy.array(inputs_list, ndmin=2).T
targets = numpy.array(targets_list, ndmin=2).T
# calculate signals into hidden layer
hidden_inputs = numpy.dot(self.wih, inputs)
# calculate the signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer
final_inputs = numpy.dot(self.who, hidden_outputs)
# calculate the signals emerging from final output layer
final_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.

Press + to interact
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.

Press + to interact
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 (targetsfinal_outputs)(\text{targets} - \text{final\_outputs}), element by element. The Python code for this is really simple, showing again the elegant power of matrices:

Press + to interact
# 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 ...

Access this course and 1400+ top-rated courses and projects.