Enter your search above to see Results

Whitepapers

Neural Networks
Part 2: Training

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.

Rendered by QuickLaTeX.com

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

(1)   \begin{equation*} z=w_1i_1 + w_2i_2 + b \end{equation*}

Then the activation function modified this value to give:

    \[ a=\sigma(z) \]

Finally, the cost function compared a with the training observation y 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.

    \[\Delta c \approx \frac{\partial c}{\partial w}\Delta w+\frac{\partial c}{\partial b}\Delta b\]

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 p =[w, b].

    \[ \Delta p = \begin{bmatrix} \Delta w & \Delta b \end{bmatrix} \]

Then the partial derivatives in nabla notation are as follows:

    \[ \nabla c = \begin{bmatrix} \frac{\partial c}{\partial w} & \frac{\partial c}{\partial b} \end{bmatrix} \]

    \[\Delta c \approx \nabla c \cdot \Delta p\]

Suppose now that we chose to move the parameters by an amount \Delta p=-\lambda \nabla c where \lambda is a small positive value which is called the learning rate.

    \[\Delta c \approx - \lambda \nabla c \cdot \nabla c=- \lambda \nabla^2c\]

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:

    \[\Delta c = \lim_{\Delta w \to 0}\frac{\partial c}{\partial w}\Delta w+\lim_{\Delta b \to 0}\frac{\partial c}{\partial b}\Delta b\]

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 \lambda. However, we can see that a small value for \lambda will also make the convergence slower. Choosing an appropriate value for \lambda 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:

Rendered by QuickLaTeX.com

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.

    \[c = \frac{1}{2}(a-y)^2 \]

By inspection, we can see that the solution w=b=0 will work. We can also see that w=k, b=-k \text{ where } k\in \R 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.

    \[\frac {\partial c}{\partial w}=(a - y)i \]

    \[\frac {\partial c}{\partial b}=(a - y)\]

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

    \begin{align*} w_{n+1} = w_n-\lambda \frac{\partial c}{\partial w} &=w_n - \lambda(a_n-y)i \\ b_{n+1} = b_n-\lambda \frac{\partial c}{\partial b} &=b_n - \lambda(a_n-y) \end{align*}

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:

    \[\frac{\partial c}{\partial w}=(a-y)i\sigma'(z)=(a-y)i\sigma(z)(1-\sigma(z))\]

    \[\frac{\partial c}{\partial b}=(a-y)\sigma'(z)=(a-y)\sigma(z)(1-\sigma(z))\]

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 w and b. Finally, here is the output:

The solid line on the chart is what happened when we started with w=b=0. The cost falls quickly and converges well.  The dashed line is what happened when we started with w=b=1. 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 w=b=2. 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 w=b=3.  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.

    \[c=-\{y ln (a) + (1-y)ln(1-a)\}\]

    \begin{align*} \frac{\partial c}{\partial w} &=-\bigg\{\frac{y}{a}-\frac{1-y}{1-a}\bigg\}\frac{\partial a}{\partial w}\\ &=-\bigg\{\frac{y}{a}-\frac{1-y}{1-a}\bigg\}\sigma'(z)\frac{\partial z}{\partial w}\\ &=-\bigg\{\frac{y}{a}-\frac{1-y}{1-a}\bigg\}\sigma'(z)i \\ &=-\bigg\{\frac{y}{a}-\frac{1-y}{1-a}\bigg\}\sigma(z)(1-\sigma(z))i \\ &=-\bigg\{\frac{y}{a}-\frac{1-y}{1-a}\bigg\}a(1-a)i \\ &= \{y(a-1)-a(y-1)\}i \\ &= \{a-y)\}i \end{align*}

The gradient gets larger the further away the result a is from the target y 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

Rendered by QuickLaTeX.com

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 (\hat a) and when we use matrices we will show the variable in bold letters (\bold a).

    \[ \hat i= \begin{bmatrix} i_1 & i_2 \end{bmatrix} \]

    \[ \hat w= \begin{bmatrix} w_1 & w_2 \end{bmatrix} \]

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

    \[z=\hat i \cdot \hat w + b\]

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

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 i_1 and i_2 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:

    \[\bold i= \begin{bmatrix} i_{11} & i_{21} & i_{31} & i_{41} \\ i_{12} & i_{22} & i_{32} & i_{42} \\ \end{bmatrix}\]

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.

    \[ \bold w= \begin{bmatrix} w_1 & w_2 \end{bmatrix} \]

    \[\hat b= \begin{bmatrix} b & b & b & b \end{bmatrix}\]

Now equation 1 becomes:

    \[\hat z = \bold w \cdot \bold i + \hat b\]

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

    \[ \hat a = \begin{bmatrix} \sigma(z_1) & \sigma(z_2) & \sigma(z_3) & \sigma(z_4) \end{bmatrix} = \sigma(\hat z) \]

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.

    \begin{align*} c & =\frac {1} {2} \sum_{n=1}^N(a_n-y_n)^2 \\ & =\frac {1} {2} (\hat a - \hat y) \cdot (\hat a - \hat y) \end{align*}

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.

    \[c=\frac {1} {2} \sum_{n=1}^N(a_n-y_n)^2\]

    \[\frac{\partial c}{\partial a_n}=\sum_{n=1}^N(a_n-y_n)\]

    \begin{align*} \frac{\partial c}{\partial z_n} &=\frac{\partial c}{\partial a_n} \times \frac {\partial a_n}{\partial z_n} \\ &=\sum_{n=1}^N(a_n-y_n)\sigma'(z_n) \end{align*}

    \begin{align*} \frac{\partial c}{\partial w_j} &=\frac{\partial c}{\partial z_n} \times \frac{\partial z_n}{\partial w_j} \\ &=\sum_{n=1}^N(a_n-y_n)\sigma'(z_n)i_{nj} \end{align*}

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.

Latest Articles

Neural Networks
Part 1: Features of a Neuron
Neural Networks
Part 3: Back Propagation
High Performance Computing at JTA The Data Scientists JTA The Data Scientists is a Microsoft Gold Partner in Data Platform

Categories

Latest News Case Studies Computer Conservation Events Latest News Whitepapers / Publications
Enquiry

See how we can make your data speak

Send an enquiry to us below

reCAPTCHA