**What is a binary neuron?**
A binary neuron is one of the very first mathematical functions to have the term AI applied to it, and although any serious AI enthusiast would consider it to be a mere child's toy, it does have several uses. The neuron is a binary version of a human neuron, containing inputs, outputs and a processing centre. It is also similar in that it can be altered to handle new situations, and is fault tolerant(Although to a very small degree).

The practical uses for a single neuron are admittedly small, but for simple computer AI they can be quite useful, especially for movement routines.

Before I start I would like to suggest drawing out on paper all the examples and the maths, so that you can re-perform the steps I go through. It will be a great aid in understanding the article.

**Building your first basic neuron**

Neurons are created with some basic maths and a matrix of inputs and outputs, and work best when handling simple Yes or No situations. So for this example I use a car with two sensors and two motors, both of which have fixed states of +1 or -1, anything more complicated than this may be stretching it a bit, as the inputs for the neuron have a size restriction. We first need to decide on some inputs, and for this I have chosen a simple car type scenario of two sensors on the front left and right of the car, and for the outputs two motors, back left and back right. I will call them Sensor1, Sensor2, Motor1 and Motor2. The sensors are either +1(Blocked) or -1(Clear), and the motor can report +1(Moving) or -1(Still). Our first set of inputs is as follows:

Sensor1 = +1

Sensor2 = +1

Motor1 = -1

Motor2 = -1

Now what has happened is we are against a wall, and the sensors are telling the neuron that they are on. Both motors are still switched off, as the neuron has only just started. We now have to build a table that will not only give the results we need, but also store all our data.

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +1 +1 -1 -1

Sensor2 +1 +1 -1 -1

Motor1 -1 -1 +1 +1

Motor2 -1 -1 +1 +1

Believe it or not, this is your binary neuron. The table is formed of the correct response to a "both sensors on" situation, because when making a very simple neuron you will have to give an example of a correct answer for every new situation. So if the left front sensor is pressed the right motor has to be off and the left be on(thus the robot turns right), and vice-versa. If none of the sensors are pressed both motors should be on.

To read the table simply pick a row or column that begins with your inputs and read the numbers in it, using the headings on the opposite row or column for data labels. You will notice that any column or row that starts with Sensor1 or Sensor2 has in it the correct response to this situation. For example Sensor1 tells us that both sensors are on, and that both motors should be switched off(Because having either motor on will not make our robot reverse, this is a problem we cannot solve).

**Warning!**

You may notice that we already have the answers in this table, so it will always produce the correct result. However normally you would not have this table as the neuron has no previous knowledge of how to operate. If you want to learn how to make the neuron truly "AI", keep reading to the advanced section.

The reason for including the opposite data(i.e. both motors off therefore both sensors are on) is so that reverse logic can be performed on the table, allowing us to feed in the state of the motors and guess what the sensors are. This is useless in this example as we always know the sensors values, but it can often be useful to know what inputs to expect, as we can fill in for missing and bad data. If you want you can use a table with only the sensors in it, but it does limit you with more advanced functionality.

Now we need to multiply the table by the inputs, to do this we turn the inputs into a vector:

+1

+1

?

?

You may notice that the motors are now question marks, this is because we want to work out the outputs, and we do not yet know what they should be. Next, we produce the table with our input/output vector included this time:

Sensor1 Sensor2 Motor1 Motor2 I/O

Sensor1 +1 +1 -1 -1 +1

Sensor2 +1 +1 -1 -1 +1

Motor1 -1 -1 +1 +1 ?

Motor2 -1 -1 +1 +1 ?

To work out the answers you multiply every element of each horizontal line by the inputs, like this:

Line1:

(+1*+1) + (+1*+1) + (-1*?) + (-1*?) =

Now obviously we cannot work out question marks, so we take those out:

Line1:

(+1*+1) + (+1*+1) + (-1*?) + (-1*?) = (+1*+1) + (+1*+1) =

Which equals...

Line1:

(+1*+1) + (+1*+1) + (-1*?) + (-1*?) = (+1*+1) + (+1*+1) = +2

Now work it out for all the lines:

Line2:

(+1*+1) + (+1*+1) + (-1*?) + (-1*?) = (+1*+1) + (+1*+1) = +2

Line3:

(-1*+1) + (-1*+1) + (+1*?) + (+1*?) = (+1*+1) + (+1*+1) = -2

Line4:

(-1*+1) + (-1*+1) + (+1*?) + (+1*?) = (+1*+1) + (+1*+1) = -2

We now have an output vector. You can check the results a little by matching the sensor inputs it produces to the original sensor inputs, they MUST match for this to be a valid answer. If the sensor inputs do not match, then we know that the neuron has never encountered this situation before now, and we must teach it how to react(Or let it teach itself). However, for this example the neuron will get the correct answer.

The final vector looks like this:

+2

+2

-2

-2

This is of course not right, as this is a binary neuron vector and only has either a positive(+1) or a negative(-1) result. Therefore, we need to normalize the result. Which gives us this:

+1

+1

-1

-1

You will see that the neuron has given us the correct inputs back, and has suggested that we turn both motors off. This is correct, and it will produce a correct answer 100% of the time. Now what if the inputs were different? Do we make another neuron to cope with that? Nope, we use a multiple data set neuron.

**Multiple data set neurons**

This section involves more matrix maths, but thankfully it is a lot simpler. We will take a different set of inputs and put them into a new matrix(Called the temporary movement matrix), but were going to keep the old matrix we were using. We now have two matrices, the current matrix and a new temporary movement matrix with the new data in it, here are both.

This is the neurons current dataset, stored in the neuron from the previous exercise.

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +1 +1 -1 -1

Sensor2 +1 +1 -1 -1

Motor1 -1 -1 +1 +1

Motor2 -1 -1 +1 +1

This is the vector for the new set of inputs.

+1

-1

?

?

Below is the correct answer for this particular set of inputs. Working out the answer you need before you tackle a problem like this can be of great use when testing, and can be used to correct the neuron later, should it need it.

+1

-1

+1

-1

This set of figures should turn us away from the obstacle blocking our sensor.

Now we build the table for the new vector using section one of this tutorial.

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +1 -1 +1 -1

Sensor2 -1 +1 -1 +1

Motor1 +1 -1 +1 -1

Motor2 -1 +1 -1 +1

Remember that we are telling it the answer to the question by filling in the temporary movement matrix, so it should not get the answer wrong. In more advanced versions of the neuron, you will give it the ability to train itself so giving it the answer's is just a shortcut. Now we work out the maths as before.

Line1:

(+1*+1) + (-1*-1) + (+1*?) + (-1*?) = (+1*+1) + (-1*-1) = +2

Line2:

(-1*+1) + (+1*-1) + (-1*?) + (+1*?) = (-1*+1) + (+1*-1) = -2

Line3:

(+1*+1) + (-1*-1) + (+1*?) + (-1*?) = (+1*+1) + (-1*-1) = +2

Line4:

(-1*+1) + (+1*-1) + (-1*?) + (+1*?) = (-1*+1) + (+1*-1) = -2

The reason we work out this temporary movement matrix separately rather than integrating it into our neuron straight away is so we can check the computer is giving us the right answer. If we are teaching the neuron we may need to tell it to dump a matrix if it is wrong, and we can save a lot of time normally spent removing it from the main matrix. But as it happens, the temporary movement matrix has the right answers so we can now add it to our main data matrix. We do this by simply adding the figures in the matrices together, as follows:

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +1 +1 -1 -1

Sensor2 +1 +1 -1 -1

Motor1 -1 -1 +1 +1

Motor2 -1 -1 +1 +1

Added to this:

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +1 -1 +1 -1

Sensor2 -1 +1 -1 +1

Motor1 +1 -1 +1 -1

Motor2 -1 +1 -1 +1

Equals...

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +2 0 0 -2

Sensor2 0 +2 -2 0

Motor1 0 -2 +2 0

Motor2 -2 0 0 +2

This is the updated matrix for this neuron. You should also notice that although this is a binary neuron in regards the inputs, the actual neuron itself could have larger values than 1/-1. This is similar to more complicated neurons that can accept data out of their normal range, but use an aggregate value to perform checks.

If you wish to test the final matrix, work out the results for both input vectors I have shown but using the updated matrix. If you want to see how it can identify a new scenario, try to work out the answer for a vector I have not used yet, such as:

-1

+1

-1

+1

This will produce a different set of inputs to what we gave it, signifying that we need to repeat the whole process again.

**Self-teaching binary neurons**

Staying with our original neuron, I will explain how to make a self-teaching neuron. It is not possible to write a testing system for all neurons, but I will work through this problem with you to help demonstrate the thought process involved. This is all a natural progression from the basic neuron, and I would advise trying to approach this step yourself. There is no greater teacher than experience. If however you want the quick route, read on.

There are three primary steps to designing a self-teaching procedure. The first step has to create guesses for unknown situations, the second must then test these guesses and the third must decide if the result is good enough to use as an answer. Creating guesses is sometimes enormously complicated but fortunately, our binary neuron(When used as a temporary movement matrix) is simple, we can always count on an accurate, if not slightly incomplete, input and have fixed values we can work with(+1/-1). Using a simple random number generator to fill the temporary movement matrix is the best method for this particular situation, although usually a certain amount of test data is used to provide a template for guessing(Which is an entire subject on its own).

For this example we will use this set of inputs, with a new blank matrix.

-1

+1

?

?

First, fill the matrix with a vector formed of random values. To save on space in this document I have used a matrix that is valid, in that both sensors values match on the input and the final answer. Normally you would keep creating these random values until the outputs and inputs match, or write a more complicated random algorithm that will produce valid but random values.

Sensor1 Sensor2 Motor1 Motor2

Sensor1 +1 -1 +1 -1

Sensor2 -1 +1 -1 +1

Motor1 +1 -1 +1 -1

Motor2 -1 +1 -1 +1

Now feed your vector through the matrix as before.

Line1:

(+1*-1) + (-1*+1) + (+1*?) + (-1*?) = (+1*-1) + (-1*+1) = -2

Line2:

(-1*-1) + (+1*+1) + (-1*?) + (+1*?) = (-1*-1) + (+1*+1) = +2

Line3:

(+1*-1) + (-1*+1) + (+1*?) + (-1*?) = (+1*-1) + (-1*+1) = -2

Line4:

(-1*-1) + (+1*+1) + (-1*?) + (+1*?) = (-1*-1) + (+1*+1) = +2

We know the inputs match because we have rigged the original matrix, so we now have our guess at the correct temporary movement matrix. All we need do now is test it.

Testing a neuron is different in every case and you will have to consider a different method of testing for each one. This, unfortunately, is were our test car lets us down a little, but gives a good insight into future problems you might encounter and the advantages of more complex systems. The car we are using can tell us if the sensors are on, but this is not enough data to accurately tell us if the guess has been successful, as it may take time for the car to correct itself or the car may well still stick even after the correct motor is switched on. We can make our checking more accurate though by getting the current state of the motors and checking if they actually are moving. If the motors are both moving as was suggested by the guess and have not reverted to their original state then the guess was correct. If the motors are not the same, then the move failed and a new one must be tried until the car succeeds.

We must also remember to check the inputs are the same when testing the motors as a new situation may have arisen, and in that case we will need to dump the temporary movement matrix because we can no longer accurately test the guess stored there. This is another limitation of a simple matrix.

The expected answer to this particular situation is that the motor on the opposite side is switched off to allow the car to turn pass the obstacle. But even if the answer is not as expected the neuron has still learned how to get past this particular problem. However, as mentioned above, the guess may not be correct in every situation as variations of the angle the car hits the object can make the solution incorrect. Here we need to use a set of stored data to predict the correct move by plotting the movements leading up to a decision, the resulting decisions tried, and whether or not they were successful. By analyzing these sets of data, "weights" can be assigned to certain solutions to a particular input set, which will affect which solution is tried in a given situation, but that again is an entire article in itself.

**Programming usage**