This article continues from Neural Networks and Backpropagation: Part One and finishes working through the example of creating a neural network to compute an OR gate. If you have yet to read through the article and the corresponding Jupyter notebook you should take the time now as it is important for understanding the rest of the working.

Don’t forget to grab the jupyter notebook for this article from github here or by cloning:

`git clone git@github.com:tinhatben/or_gate.git`

## Where we left off

We have just completed the first pass of forward propagation through the network, computing the activations of each of the nodes in the network, now it is time to execute backpropagation through the network.

## What is Backpropagation?

The process of backpropagation computes the amount that each weight in the network contributes to the final error produced by the network. This process allows us to determine how much we need to adjust the weights to produce an improvement in the network performance. *What we are aiming to do is determine the ideal value for each of the weights in the network, such that we get the best performance or lowest error score for the network. *The backpropagation process involves determining the error at the output of the network and projecting back through each of the layers and nodes in the network.

So the first thing we need to do is select a function that determines the error for the network.

## Network Cost Function

For the OR gate we are going to select the least squared error cost function:

Looking at this function in more detail, the reason for squaring the difference in the output and the target is simply to remove any effect of positive or negative errors from the computation. It doesn’t matter if we are +0.5 or -0.5 away from the target, the error is the same: 0.5. The is a constant multiplier that makes differentiating the cost function easier. For future reference, the difference of the cost function with respect to the training data is:

In our example is the target output of the or gate. As and are 0 and 1, then .

Referring back to Part One the output of the network:

So,

The change in error with respect to the output of the network:

## Output Layer Backpropagation

Now that we have the error at the output, we need to project it back to determine the error component at . In order to do this we need the **chain rule of differentiation**:

With this simple rule when we calculate how much every weight and node in the network contributes to the error. So, calculating the error with respect to :

Now we already have the first half of the above equation in and what we need is the rate of change of the linearity of the output node with respect to the input. Recalling from Part One we are using the Sigmoid function as the linearity. We will not cover the proof of the derivative of the sigmoid function in this article as it is reasonably straight forward to determine; but this derivative is exactly what we need, the rate of change of linearity. So:

We can now combine the two halves of the chain rule to determine the contibution of to the error:

Recalling from Part One that for we can calculate the output layer contribution to error:

Using the same chain rule of differentiation we can compute the error attributing to each of the weights:

The only part of this equation we are missing is . Again from Part One:

Thus:

and

Repeating the same pattern, let’s repeat the calculation for the other weight and bias term in the output layer:

Again from Neural Networks and Backpropagation: Part One:

Thus:

and

Finally the error with respect to the bias:

Thus:

and

Now we have completed the back propagation process for the output layer and weights. So, moving onto the hidden layer we are essentially repeating the same process.

## Hidden Layer Backpropagation

Repeating chain rule differentiation we can determine the amount of error attributed by and .

Expanding on

From the output layer backpropagation:

From this point on we will refer to as . Try to keep in mind that is the product of the change in error with respect to the output activation and the derivative of the Sigmoid function .

Continuing with determining the components of the chain rule for :

So,

As with all differentials of activations with respect to the input

Now:

So putting it all together:

The process described above forms the pattern for deriving all of the error components for the hidden layer weights. The next obvious weight is which only differs from in that the input is different, so:

Looking at the bias weight which is connected to :

Now let’s repeat the process to compute , and :

Again, from the output layer and as per above:

So:

We can repeat this process for

and :

Calculating the corresponding values:

The output layer values as above:

## Generalised Form

Reviewing the details of the backpropagation process above it can be observed that there is a general pattern to the process. Let’s say that we are completing the process for layer and is the next layer in the network closer to the output layer. We can calculate:

Where: is the activation values of the layer before the current layer .

In coding this process through a number of layers we can now easily loop by applying the generalised form.

## Update Weights

Now that we have computed each of the derivatives of error with respect to the weights, we can update the values for the weights, hopefully to reduce the error produced by the network. To do this we need to introduce another term, the **learning rate** ; this is how much of the derivative is to be applied during the update to the weights. This is an important term in neural networks: a learning rate that is too small will lead to a network that is very slow to train, while a large value can cause instability in the network which may never find a solution. The weights are updated as follows:

where indicates a simultaneous update of all weights in the network.

## Training Process

The training process for a network is essentially a repetitious loop through the forward propagation, backpropagation and weight update processes; adjusting the weights until hopefully a minimum error is achieved. This is a very simplistic view and there are a number of technical aspects that must be correct for training to be successful, or can be executed to speed up the process. The details of training are being left for a later post as they lie outside the exact workings of backpropagation.

## In Closing

To re-iterate, this process may take a few reads to understand and get a good grasp on. The effort is well worth it, particularly if trying to debug issues with training neural networks. I also recommend that you read the jupyter notebook that partners this article as it demonstrates the practical applications of this working.

Don’t forget to grab the jupyter notebook for this article from github here or by cloning:

`git clone git@github.com:tinhatben/or_gate.git`

## One thought on “Neural Networks and Backpropagation: Part Two”