We have discussed almost all the features and structure of an artificial neuron in the previous chapters. This means that the artificial neuron is very simple to implement in code. After discussing the concept of the bias, we will directly get into the implementation details of the artificial neuron.
The bias
A bias is a random value between -0.5 and +0.5 that is added to the sum of the weighted input signals of the neuron. This is done to stabilize the operation of the neuron. The bias is a property of a neuron which is not directly related to any of the input signals. This helps in modifying the property of the neuron without affecting any particular input signal while training the neural network.
As we have seen in the output function of the neuron in the last chapter, Weight[0] was not multiplied with any of the input values. In our program we use Weight[0] as the bias. So we just add the Weight[0] to the sum. Now the modified output function becomes:
double Neuron::Output(double * Input, int Num_Inputs)
{
double Sum=0;
Sum += Weight[0]; // Weight[0] is the bias. The bias is added to the sum.
for(int i=1;i<=Num_Inputs;i++)
Sum += Weight[i] * Input[i-1];
return (1/ (1 + exp(-Sum)));
}
We have just completed explaining the most complex method in the neuron class!!! Yes. seriously. The neuron is as simple as that. Before going further let us discuss the neuron class structure in detail.
The following class is the actual neuron used in the whole program. We use arrays of such neurons in the Neural Network. More details are given in the places where each function is explained.
class Neuron
{
double Weight[257]; //The weights of input values
public :
Neuron();
double Output(double * Input, int Num_Inputs);
double GetWeight(int Index);
void SetWeight(int Index, double Value);
void ChangeWeight(int Index, double Change);
};
Simple. Isn't it? Now let us discuss the new functions in our neuron.
Constructor of the neuron
Our neuron class needs a constructor so that the weights and bias can be initialized properly. The weights of the neuron are initialized with small random numbers. The value of the weight assigned will always be between -0.5 and +0.5. We use a random number generator for this purpose. In the following function it can be seen that we seed(Initialize) the random-number generator with the current time so that the numbers will be different every time it is run.
Neuron::Neuron()
{
srand( (unsigned)time(NULL) );
for(int i=0;i<=256;i++)
Weight[i] = (double)rand()/32767 - 0.5;
}
We also have a getter and a setter for the weights. Using these methods you can get or set the value of a Weight[index] at a given Index.
double Neuron::GetWeight(int Index)
{
return Weight[Index];
}
void Neuron::SetWeight(int Index, double Value)
{
Weight[Index] = Value;
}
Then there is a ChangeWeight() method that can change the value of a particular Weight[i] by adding the given value to the existing weight. This function is used to make the code more efficient and readable. If it were not used, we would have to call GetWeight() and SetWeight() functions respectively in order to do the same job.
void Neuron::ChangeWeight(int Index, double Change)
{
Weight[Index] += Change;
}
That is all about the structure of an individual artificial neuron. Just keep in mind that a lot of neurons of this same kind will be interlinked to form a network called the neural network.
