Monday 14 March 2011

Building a Neural Network in Excel 1

In this post we're going to build a simple neural network in Microsoft Excel. Excel is surprisingly good for doing this as it's familiar to many people and simple to use after minimal exposure. Moreover, it has a lot of built in functions (IF, CHOOSE, etc) that are the same as in any programming language (Matlab, C#, Java), plus many other useful functions.

The scenario we'll use is that of the Grak: the imaginary creature in the picture opposite. The Grak hangs from a branch by its single foot and waits for its dinner to walk by beneath. It has two sensors - one for temperature and one for butyric acid. The sensors are binary, meaning they only pick up values of 1 or 0 (though we can change this later). In the place where the Grak lives there are three other kinds of animals: Wampuses (cold but smelly), Wiggles (hot but not smelly), and Fraggles (both hot and smelly). These three creatures, plus the option of no creature at all (cold and not smelly) gives us the following logic table:

So, columns S1 and S2 are our network's (Grak's) sensors, and the column marked Drop? is the message from the neuron's axon to the muscle telling it to drop (1) or not (0).

You can see from the table that our Darwinian Grak only eats one kind of animal. Which one?



Darwinian Grak

1. Open a new worksheet in Microsoft Excel.
2. We are going to input the data for the following Darwinian Grak into the worksheet:

The two boxes, S1 and S2, are the two sensors that the Grak has. At the moment it is sensing a zero value in each one of them. Each of these sensors connects to the neuron body via a synapse, which we call weights; w1 and w2. In this case the weights are set to the values shown. The threshold is the limit that the activity of the neuron must surpass in order to fire - in this case 1.



Enter the information into Excel in the following way:

What you’ve said here is that there are two inputs, each with a value of zero. There are two weights, with values of -0.5 and 0.2 respectively. There is a threshold of 1. There are also activation and output values, but these are not set yet – this is because we are going to have the network (Grak) calculate these itself.

3. Now we need to add S1*w1 to S2*w2. To do this, first click on cell C4. Now go to the formula bar and enter the following:

When you press return, the value zero should appear in cell C4. This is because S1*w1 + S2*w2 = 0.

4. Finally, we need to compare the value we’ve just calculated with the threshold, and output a 1 if the value exceeds the threshold and a 0 otherwise.

Click on cell E4. Now go up to the formalas tabbed menu and select 'Insert Function':

When you have done this, a separate window will pop up that gives the option to “Select a function:”. From this list, choose IF. Another window will pop up. Input the following data and click ok:

What you have said is: if the data in cell C4 is greater than or equal to ( >= ) the value in cell D4, then make the value in cell E4 1. If not, make it 0. Simple, isn’t it!

5. Now try each of the following input pairs for the Darwinian Grak's sensors:

Bearing in mind that the Darwinian Grak only feeds on Fraggles, does the Grak you have made survive? Why, or why not?

6. The Darwinian Grak’s synapse strengths are genetically determined. The Grak above has a sister whose synapse strengths are 0.5 and 0.6. Adapt your Excel worksheet for this new Grak, then test it with each of the input pairs above. Again bearing in mind that Fraggles are the only thing this Grak can eat, does it survive?


So far we’ve seen two sibling Graks whose behaviours are pre-determined at birth (perhaps genetically). Should one of them be born with a mutation that makes it perform badly, it will die. This was the case with the first of the two, but not with the second. This is all fine, but it would be better if a baby were able to get some instruction from their mother so that any birth defects in synapse strength could be updated and they could survive... it would be better if it could learn! This is the case with the Common Grak that we'll look at in the next post.


Here's a video of me going through the steps from this post:

No comments:

Post a Comment