14,981,841 members
Articles / Artificial Intelligence / Machine Learning
Article
Posted 30 Dec 2018

43.9K views
73 bookmarked

# Machine Learning in Excel

Rate me:
A cell by cell walkthrough of the maths of a Neural network

## Introduction

This article is written for you who is curious of the mathematics behind neural networks, NN. It might also be useful if you are trying to develop your own NN. It is a cell by cell walk through of a three layer NN with two neurons in each layer. Excel is used for the implementation.

## Background

If you are still reading this, we probably have at least one thing in common. We are both curious about Machine Learning and Neural Networks. There are several frameworks and free api:s in this area and it might be smarter to use them than inventing something that is already there. But on the other hand, it does not hurt to know how machine learning works in depth. And it is also a lot more fun to explore things in depth.

My journey into machine learning has perhaps just started. And I started by Googling, reading a lot of great stuff on the internet. I also saw a few good YouTube videos. But I it was hard to gain enough knowledge to start coding my own AI.
Finally, I found this blog post: A Step by Step Backpropagation Example by Matt Mazur. It suited me, and the rest of this text is based on it.

## Construction

A Neural Network, NN, consists of many layers of neurons. A Neuron has a value and connections with weights to all other neurons in the next layer.

The first layer is the input layer and the last layer is the output layer. Between input and output, there might be one or many hidden layers. The number of neurons in a layer is variable.

If a NN is used to, for example, classify images, the number of neurons in the input layer is of course equal to the number of pixels in the image. Then in the output, each neuron represents a classification of the image. (E.g., a type of animal, a flower or a digit.)

## Calculations

Before the calculations, all the weights in the NN have to be initialized with random numbers.

The image below is a print screen of the spread sheet that I refer to in the rest of this article. It might be a good idea to keep an open window of that sheet. That should make it easier to follow along.
A tip: Row 2 is the order of calculations.

### Step 1 - 3. Forward Pass

The value of one neuron is calculated by taking the sum of every previous neuron multiplied by its weight.
An extra bias weight which has no neuron is also added:

`F3 = A3 * B3 + A7 * B4 + B5`

The value is normalized through a activation function. There are several different activation functions used in neural networks.
I have used the logistic function:

`G3 = 1 / (1 + EXP(-F3))`

### Step 4 - 5. Forward Pass

The neurons of the output layer is calculated the same way as hidden layer.

```L3 = G3 * H3 + G7 * H4 + H5
and
M3 = 1 / (1 + EXP(-L3))```

### Step 6 - 7. The Error

The error of each output neuron is calculated using an expected or a target value. When classifying images, it is common to set one neuron close to 1 and the rest of the neurons close to zero.

For the errors in column Q:

Q3 = (M3 - O3)²

and:

Q7 = (M7 - O7)²

The total Error `R5` is the average of all errors and should get closer and closer to zero as the network is trained.

`R5 = (Q3 + Q7) / 2`

### Backward Propagation

A Neural Network is trained by passing it lots of train data repeatedly.
Then, for every iteration, errors and deltas are calculated. This is used to make small adjustments to all the weights in such a way that the network becomes better and better.
This is called backpropagation.
Since the total error can be expressed as a mathematical functions of each weight, one can derive those functions to obtain the slopes of the function curves in one point. The slopes indicate the direction towards a minimum for the total error and proportionally how much each weight should be adjusted in order for the total error to approach zero.

A delta value is calculated below for each weight. The deltas are stored in column I and D, for output and hidden layer respectively.

### Chain Rule - Friend of Backpropagation

In practice, we want to derive the total error R5 with respect to H3 so we first to express R5 as a function of H3 using substitutions.

Since

```R5 = (Q3 + Q7) / 2
R5 = (M3 - O3)² / 2 + (M7 - O7)² / 2```

The above function does not look very easy to derive. Is it even possible?
We will instead use the chain rule2.
It states that if we have a composition of two or more functions `f(g(x))` and let `F(x) = f(g(x))`, we can derive like this:

F’(x) = f’(g(x)) * g’(x) or in another notation:

In our case, we have the following dependency:

`R5(M3(L3(H3)))` and we can write:

### Step 9. Output layer Deltas

The function for the total error `R5` is derived with respect to the first weight `H3` of the output layer.

In the above formula, the chain rule is used to make it simpler to derive.

Since:

Since will be used later in the backpropagation, it is stored in the cell `P3`.

```P3 = (M3-O3) * M3 * (1 - M3)
```

The last derivative of the chain of derivatives above is simpler.
Since `L3 = G3 * H3 + G7 * H4 + H5`

We can now put everything together and store into cell I3.

```I3 = P3 * G3
```

The rest of the weights in output layer is calculated the same way and we get:

```P7 = (M7-O7) * M7 * (1 - M7)

I4 = G7 * P3
I5 = 1 * P3 (bias neuron)
I7 = G3 * P7
I8 = G7 * P7
I9 = 1 * P7 (bias neuron)
```

### Step 10. Backpropagation in Hidden Layer

In this step, we calculate:

The chain rule from previous steps helps to transform it to something we can use:

First term also must be split up on both errors `Q3 `and `Q7 `so:

First look at this:

It can be further split up like this:

First is already stored in `P3 = (M3-O3) * M3* (1 - M3)`

Since `L3 = G3 * H3 + G7 * H4 + H5`

When we put the above together, we get:

And in the same way as above:

First problem is solved.

Time for

We know that:

And we have previously learned to derive the logistic function.

And now:

Because:

We now put the above together to get one expression for the derivative of the total error with respect to first weight of the hidden layer.

This is stored in cell `C3`.

The calculations for the above is repeated for all hidden layer weights:

```C3 = (P3 * H3 + P7 *H7) * (G3 *(1 - G3)) * A3
C4 = (P3 * H3 + P7 *H7) * (G3 *(1 - G3)) * A7
C5 = (P3 * H3 + P7 *H7) * (G3 *(1 - G3)) * 1
C7 = (P3 * H4 + P7 *H8) * (G7 *(1 - G7)) * A3
C8 = (P3 * H4 + P7 *H8) * (G7 *(1 - G7)) * A7
C9 = (P3 * H4 + P7 *H8) * (G7 *(1 - G7)) * 1```

Now it is easy to calculate new weights using a selected learning rate from cell `A13`.

For example: (new `B3`)

```D3 = B3 - C3 * A13
```

There is a macro connected to the train button in the Excel document. The macro iterates many times and we can see how the output neurons in column `M` gets closer and closer to their target values and that the total Error in `R5` gets closer and closer to zero.

Update in version 1.1:
I discovered that it is possible to improve learning rate and accuracy by using the activation function Leaky Relu4:
`f(x) = x if x > 0` otherwise `f(x) = x/20`

It may be a good exercise to replace the Logistic Function with Leaky Relu.
Hints:

```G3 = IFS(F3 > 0; F3; F3 <= 0; F3/20)
and
P3= (M3-O3) * IFS(M3 > 0;1;M3<=0;1/20)```

(Also attaching new version of  the xls file, just in case...)

## Final Words

I realize this article might take some time to digest. I tried to explain it as I understood it. Please comment below if you find any errors.

After I sorted out how NNs work in Excel, I wrote a C# program that can interpret hand written digits. It has a Windows Forms user interface which works well. It seems to recognize almost any digit I draw, even ugly once. That was a proof to me that my understanding of Artificial Neural Networks is correct so far.

That article can be found here:

## History

• 1st January, 2019 - Version 1.0
• 8th January, 2019 - Version 1.1
• Replaced Logistic activation function with LeakyReLu
• 11th January, 2019 - Version 1.2
• Update of names of biases in diagrams
• 23th Januray, 2019 - Version 1.3
• Changed du calculation to the total error to the average av errors

## Share

 Software Developer (Senior) Sweden
No Biography provided

 First PrevNext
 Gradient backpropagation - Step by Step Member 1495830219-Oct-20 2:30 Member 14958302 19-Oct-20 2:30
 Just curious Member 1450337118-Dec-19 21:06 Member 14503371 18-Dec-19 21:06
 About Neural network in excel Member 1450337126-Nov-19 18:12 Member 14503371 26-Nov-19 18:12
 What this Excel is for? joreyesm8-May-19 10:23 joreyesm 8-May-19 10:23
 Re: What this Excel is for? KristianEkman30-May-19 22:14 KristianEkman 30-May-19 22:14
 Re: What this Excel is for? joreyesm2-Jul-19 10:14 joreyesm 2-Jul-19 10:14
 Re: What this Excel is for? KristianEkman2-Jul-19 19:32 KristianEkman 2-Jul-19 19:32
 Re: What this Excel is for? joreyesm4-Jul-19 16:14 joreyesm 4-Jul-19 16:14
 Concrete application Member 100571643-Mar-19 19:51 Member 10057164 3-Mar-19 19:51
 Re: Concrete application KristianEkman5-May-19 11:30 KristianEkman 5-May-19 11:30
 dQ3/ dG3 calculus Member 1139786511-Feb-19 7:06 Member 11397865 11-Feb-19 7:06
 Cell A13 undefined in text lognormal20-Jan-19 8:10 lognormal 20-Jan-19 8:10
 Re: Cell A13 undefined in text KristianEkman20-Jan-19 9:38 KristianEkman 20-Jan-19 9:38
 Cell reference errors in neural net diagram Johnwi10-Jan-19 4:33 Johnwi 10-Jan-19 4:33
 Re: Cell reference errors in neural net diagram KristianEkman10-Jan-19 7:07 KristianEkman 10-Jan-19 7:07
 The links to the zip files broken? currysing9-Jan-19 2:24 currysing 9-Jan-19 2:24
 Re: The links to the zip files broken? KristianEkman9-Jan-19 3:42 KristianEkman 9-Jan-19 3:42
 Q3 and Q7 cells Member 107624315-Jan-19 4:38 Member 10762431 5-Jan-19 4:38
 C# program to interpret digits \\Coders\Rob4-Jan-19 0:49 \\Coders\Rob 4-Jan-19 0:49
 Re: C# program to interpret digits KristianEkman4-Jan-19 1:05 KristianEkman 4-Jan-19 1:05
 Re: C# program to interpret digits \\Coders\Rob4-Jan-19 1:12 \\Coders\Rob 4-Jan-19 1:12
 Re: C# program to interpret digits KristianEkman7-Jan-19 2:56 KristianEkman 7-Jan-19 2:56
 ML from Excel Member 100571642-Jan-19 20:20 Member 10057164 2-Jan-19 20:20
 Re: ML from Excel KristianEkman2-Jan-19 22:06 KristianEkman 2-Jan-19 22:06
 Re : ML depuis Excel Member 100571642-Jan-19 23:51 Member 10057164 2-Jan-19 23:51
 Last Visit: 31-Dec-99 18:00     Last Update: 3-Aug-21 14:13 Refresh 12 Next ᐅ