# Introduction

This is the second of a short series of posts to help the reader to understand what we mean by a neural network and how it works.

In our first post we explained what we mean by a neuron and then we introduced the mathematics of how we calculate the numbers associated with it.

In this post we will run some practical examples and secondly consider how a network can be trained. The practical examples were written in R and, should you wish to experiment, the code is available on our GitHub site.

The next post in the series explains the famous Back Propagation Algorithm.

Let us start by returning to the simple diagram of a neuron with the four concepts that we explained in the first post of the series: Weights, bias, activation and cost.

You will recall that the neuron calculates a value, called the input sum, using this equation:

(1)

Then the activation function modified this value to give:

Finally, the cost function compared with the training observation to measure how close we are to getting the correct output.

## 1 Training a Neural Network

### 1.1 Gradient Descent

The goal in training our neural network is to find the weights and bias which minimize the cost of our training input observation. For any given set of weights and biases we first make small changes to them and then calculus tells us approximately how these changes effect the cost.

We can write this expression in terms of vectors and then use the nabla notation for a vector of derivatives.

Firstly we form a single vector to represent the parameters of our neuron .

Then the partial derivatives in nabla notation are as follows:

Suppose now that we chose to move the parameters by an amount where is a small positive value which is called the learning rate.

Because this is clearly negative we can see that choosing to alter the parameters by subtracting the gradient vector multiplied by the learning rate will reduce the cost.

The fact that this is approximately equal is in fact a warning to us. We know from calculus that:

This tells us that in order for the mathematics to work the changes to the parameters need to be small and so we should select a small value for . However, we can see that a small value for will also make the convergence slower. Choosing an appropriate value for is therefore just one example of managing the hyper parameters of our network.

## 2. Worked Solution

### 2.1 One Input Neural Network

Here is a diagram of the neural network that we will be studying:

In order to illustrate basic training of a neuron we will take a neuron with just one input and no activation function. let us also assume that we want our neuron to output the value zero when the input is one. We will use the Euclidean distance for cost.

By inspection, we can see that the solution will work. We can also see that is also a solution but we want to get there by machine learning.

Finding a minimum is just a simple problem of partial differential calculus.

The gradient descent technique then allows us to use a learning rate to iterate the parameters:

As an illustration we have included some example code in R that runs the first 30 iterations of this function. You can download the code from here if you wish to try it out.

The example code is called “Example Learning 1.R” and it gave the following output:

> source('Example Learning 1.R') Cost: +0.000000 Weight: -0.499952 Bias: +0.500048

So to emphasize we have told our program what input and output to expect and the process has iterated to a solution that we can see will work.

### 2.2 Vanishing Gradient Problem

In our example above the neural network did not have an activation function. Let us repeat the exercise but with a logistic activation. Accordingly, our iteration will be slightly different:

In the script “Example Learning 2.R” we alter the program to include the logistic activation function. The script then runs three times with three different starting points for and . Finally, here is the output:

The solid line on the chart is what happened when we started with . The cost falls quickly and converges well. The dashed line is what happened when we started with . The system speeds up over the first few iterations and, despite starting with a much higher cost than the first run, it ends up with a similar result. The dotted line is what happened when we started with . The learning gradient is flat for the first 100 or so iterations and the neuron only starts to learn at the end. The red line is the system starting at . This happens because the gradient of the logistic curve is very flat when we start to learn with initial values that were far from the solution.

It might look as though the red line is completely flat but it does in fact learn. However it takes nearly 2000 iterations to do it:

### 2.3 How Cross Entropy Helps a Neural Network

Instead of using the Euclidean distance we can use the cross entropy cost function.

The gradient gets larger the further away the result is from the target and so incorrect starting points should no longer be an issue. This is available to run as “Example Learning 3.R” on the GitHub repository. Finally, this is the output:

We can see that despite having very different starting points the different systems all come into agreement after only 50 iterations.

### 2.4 Two Input Neural Network

We have used a single input neuron to illustrate many points. However, a neuron can have many inputs. In order for us to study this we will take the scalar equations and introduce vector and matrix notations. To make the distinction, when we use vectors we will place a caret symbol over the variable name () and when we use matrices we will show the variable in bold letters ().

We can simply express the mathematics by using vectors and replacing multiplication by a dot product:

The result of the dot product is a scalar so it can be added to scalar to give a scalar .

In part one of this series we showed that a one neuron network with ReLU activation can model a nor gate. We now return to this example to see if we can train the neuron to learn how to do this with an R script.

So the first issue is that our two inputs and can have four possible combinations. In other words our training data has four values. A really easy way to express this could be to extend our input vector to now be a matrix of 4 rows and 2 columns:

Effectively each column is a vector of training data. To make the matrix multiplication work we will need to consider the weight vector as a one row matrix and repeat the biases once for each input observation so that the scalar bias becomes a vector.

Now equation 1 becomes:

Obviously the dot now refers to matrix multiplication and not a vector dot product. Note, however, that the result of is a vector of 4 members. We can also introduce the notion of a vectored function to express in terms of the activation function (which is ReLU in this example):

When we have many items of training data we can consider the overall cost as the sum or the mean of the costs for each observation. The cost function can also be expressed using vectors.

So now we need to consider how we train the network. In the same way that we did for the first example we need to adjust the parameters using the partial derivatives of cost with respect to each parameter.

In order to simplify the mathematics we will not use matrix or vector calculus but we will now return to simple scalar functions. This is so that the reader only needs to have high school mathematics to follow our logic.

The fourth example “Example Learning 4.R” models the single neuron with two inputs. It starts with a bias of 1 and weights of 0 and some training data to show the behavior of a nor gate. The code trains the neuron to be a nor gate in 125 iterations.

> source('Example Learning 4.R') Cost: +0.0000 Weight: {-1.00 , -1.00} Bias: +1.00 >

That brings us to the end of this post. We hope it was useful. The next post in the series explains the back propagation algorithm.