Posts with «layer» label

Neural Network (Part 4): The Neural Network class


The Neural Network

Finally, we made it to the Neural Network class. This is the top level class that manages the communication between the layers. This class provides the necessary glue to hold all the layers together. It also controls the flow of information forwards and backwards from layer to layer. Lets have a look at a simple 2 layer neural network.



The above neural network starts with 2 inputs in Layer1 and finishes with 2 outputs in Layer 2.

This Neural Network structure is very modular in design. Layers can be added quite easily without affecting the functionality of the neural network code. The only layer that is treated a little bit differently from other layers in the neural network,  is the last layer.  However,  any/all preceding layers are treated exactly the same. And the differences in the last layer, only really come into effect in Neural Network training.

When building my network, I just need to make sure that the number of outputs (or neurons)  in the current layer match the number of connections in the next. If I wanted to create a neural network, that accepted 3 input signals from the outside world, to process them with 10 neurons, but only wanted to output one result... I could build the neural network in the following way.

addLayer(3,10)    : This first layer would have 3 layerINPUTs and 10 neurons
addLayer(10,1)    : This second (and last) layer would have 10 layerINPUTs and 1 neuron

The neural network class would manage these two layers, to ensure that information was passed from one to the other seemlessly. Here is the code for the Neural Network class that would make it possible:



processing code Neural Network Class

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* -------------------------------------------------------------------------------------

The Neural Network class is a container to hold and manage all the layers
------------------------------------------------------------------------------------- */


class NeuralNetwork{
Layer[] layers = {};
float[] arrayOfInputs={};
float[] arrayOfOutputs={};
float learningRate;
float networkError;
float trainingError;
int retrainChances=0;

NeuralNetwork(){
/* the default learning rate of a neural network is set to 0.1, which can changed by the setLearningRate(lR) function. */
learningRate=0.1;
}



/* Function to add a Layer to the Neural Network */
void addLayer(int numConnections, int numNeurons){
layers = (Layer[]) append(layers, new Layer(numConnections,numNeurons));
}



/* Function to return the number of layers in the neural network */
int getLayerCount(){
return layers.length;
}



/* Function to set the learningRate of the Neural Network */
void setLearningRate(float tempLearningRate){
learningRate=tempLearningRate;
}



/* Function to set the inputs of the neural network */
void setInputs(float[] tempInputs){
arrayOfInputs=tempInputs;
}



/* Function to set the inputs of a specified layer */
void setLayerInputs(float[] tempInputs, int layerIndex){
if(layerIndex>getLayerCount()-1){
println("NN Error: setLayerInputs: layerIndex=" + layerIndex + " exceeded limits= " + (getLayerCount()-1));
} else {
layers[layerIndex].setInputs(tempInputs);
}
}



/* Function to set the outputs of the neural network */
void setOutputs(float[] tempOutputs){
arrayOfOutputs=tempOutputs;
}



/* Function to return the outputs of the Neural Network */
float[] getOutputs(){
return arrayOfOutputs;
}



/* Function to process the Neural Network's input values and convert them to an output pattern using ALL layers in the network */
void processInputsToOutputs(float[] tempInputs){
setInputs(tempInputs);

/* Check to make sure that the number of NeuralNetwork inputs matches the Neuron Connection Count in the first layer. */
if(getLayerCount()>0){
if(arrayOfInputs.length!=layers[0].neurons[0].getConnectionCount()){
println("NN Error: processInputsToOutputs: The number of inputs do NOT match the NN");
exit();
} else {
/* The number of inputs are fine : continue */
for(int i=0; i<getLayerCount(); i++){

/*Set the INPUTs for each layer: The first layer gets it's input data from the NN whereas the 2nd and subsequent layers get their input data from the previous layer's actual output. */
if(i==0){
setLayerInputs(arrayOfInputs,i);
} else {
setLayerInputs(layers[i-1].actualOUTPUTs, i);
}

/* Now that the layer has had it's input values set, it can now process this data, and convert them into an output using the layer's neurons. The outputs will be used as inputs in the next layer (if available). */
layers[i].processInputsToOutputs();
}
/* Once all the data has filtered through to the end of network, we can grab the actualOUTPUTs of the LAST layer
These values become or will be set to the NN output values (arrayOfOutputs), through the setOutputs function call. */

setOutputs(layers[getLayerCount()-1].actualOUTPUTs);
}
}else{
println("Error: There are no layers in this Neural Network");
exit();
}
}




/* Function to train the entire network using an array. */
void trainNetwork(float[] inputData, float[] expectedOutputData){
/* Populate the ENTIRE network by processing the inputData. */
processInputsToOutputs(inputData);

/* train each layer - from back to front (back propagation) */
for(int i=getLayerCount()-1; i>-1; i--){
if(i==getLayerCount()-1){
layers[i].setDeltaError(expectedOutputData);
layers[i].trainLayer(learningRate);
networkError=layers[i].getLayerError();
} else {
/* Calculate the expected value for each neuron in this layer (eg. HIDDEN LAYER) */
for(int j=0; j<layers[i].getNeuronCount(); j++){
/* Reset the delta error of this neuron to zero. */
layers[i].neurons[j].deltaError=0;
/* The delta error of a hidden layer neuron is equal to the SUM of [the PRODUCT of the connection.weight and error of the neurons in the next layer(eg OUTPUT Layer)]. Connection#1 of each neuron in the output layer connect with Neuron#1 in the hidden layer */
for(int k=0; k<layers[i+1].getNeuronCount(); k++){
layers[i].neurons[j].deltaError += (layers[i+1].neurons[k].connections[j].weight * layers[i+1].neurons[k].deltaError);
}
/* Now that we have the sum of Errors x weights attached to this neuron.
We must multiply it by the derivative of the activation function. */

layers[i].neurons[j].deltaError *= (layers[i].neurons[j].neuronOutputValue * (1-layers[i].neurons[j].neuronOutputValue));
}
/* Now that you have all the necessary fields populated, you can now
Train this hidden layer and then clear the Expected outputs, ready for the next round */

layers[i].trainLayer(learningRate);
layers[i].clearExpectedOUTPUT();
}
}
}





/* Function to train the entire network, using an array of input and expected data within an ArrayList */
void trainingCycle(ArrayList trainingInputData, ArrayList trainingExpectedData, Boolean trainRandomly){
int dataIndex;

/* re-initialise the training Error with every cycle */
trainingError=0;

/* Cycle through the training data either randomly or sequentially */
for(int i=0; i<trainingInputData.size(); i++){
if(trainRandomly){
dataIndex=(int) (random(trainingInputData.size()));
} else {
dataIndex=i;
}

trainNetwork((float[]) trainingInputData.get(dataIndex),(float[]) trainingExpectedData.get(dataIndex));

/* Use the networkError variable which is calculated at the end of each individual training session to calculate the entire trainingError. */
trainingError+=abs(networkError);
}
}





/* Function to train the network until the Error is below a specific threshold */
void autoTrainNetwork(ArrayList trainingInputData, ArrayList trainingExpectedData, float trainingErrorTarget, int cycleLimit){
trainingError=9999;
int trainingCounter=0;


/* cycle through the training data until the trainingError gets below trainingErrorTarget (eg. 0.0005) or the training cycles have exceeded the cycleLimit variable (eg. 10000). */
while(trainingError>trainingErrorTarget && trainingCounter<cycleLimit){

/* re-initialise the training Error with every cycle */
trainingError=0;

/* Cycle through the training data randomly */
trainingCycle(trainingInputData, trainingExpectedData, true);

/* increment the training counter to prevent endless loop */
trainingCounter++;
}

/* Due to the random nature in which this neural network is trained. There may be occasions when the training error may drop below the threshold. To check if this is the case, we will go through one more cycle (but sequentially this time), and check the trainingError for that cycle. If the training error is still below the trainingErrorTarget, then we will end the training session. If the training error is above the trainingErrorTarget, we will continue to train. It will do this check a Maximum of 9 times. */
if(trainingCounter<cycleLimit){
trainingCycle(trainingInputData, trainingExpectedData, false);
trainingCounter++;

if(trainingError>trainingErrorTarget){
if (retrainChances<10){
retrainChances++;
autoTrainNetwork(trainingInputData, trainingExpectedData,trainingErrorTarget, cycleLimit);
}
}

} else {
println("CycleLimit has been reached. Has been retrained " + retrainChances + " times. Error is = " + trainingError);
}
}
}


The neural network constructor: NeuralNetwork() - automatically sets the learning rate to 0.1. Other than that, the Neural Network object is an empty shell waiting to be filled. The main setup function of the Neural Network class is the addLayer() function, which adds a layer with a specified number of connections and neurons.

Here are the functions of the Neural Network (NN) class:                                                                 
  • addLayer() : adds a layer to the NN (from left to right)
  • getLayerCount() : returns the number of layers in the NN
  • setLearningRate(): sets the learning Rate to a specified value.
  • setInputs(): sets the inputs of the NN, and become the layerINPUTs of the 1st layer
  • setLayerInputs(): set the inputs of a specified layer
  • setOutputs(): set the outputs of the NN= same as the actualOUTPUTs of the last layer.
  • getOutputs(): returns the outputs of the NN
  • processInputsToOutputs(): Converts the NN inputs into outputs by feeding through layers.
  • trainNetwork():  uses a training set to train the neural network (using an Array).
  • trainingCycle():  uses a training set to train the neural network (using an ArrayList).
  • autoTrainNetwork(): cycles through the training data until a specific condition is met
                                                                                                                                                        


Ok - so we now have all of the structural components required to build a feed forward neural network. And here is how you would create it. Let us build a neural network that accepts data from 4 sensors and has 3 layers . The layers will have 6, 8 and 2 neurons respectively... 
  • NeuralNetwork NN = new NeuralNetwork();
  • NN.addLayer(4,6);
  • NN.addLayer(6,8);
  • NN.addLayer(8,2);
Perfect, we have just created an entire neural network, with randomised weights and biases etc etc.
Unfortunately, the neural network is not that useful at the moment. Before we can even start to use it, we need to put it through school. We need to teach it.

Try to imagine a colour that you have never seen before. Hard isn't it ? That is how the Neural Network feels. So before you can get it to make any sort of classifications, you have to show it some examples of what you are looking for. Once the neural network can make sense of your examples, you use a "validation set" to put it through it's paces. If it performs well, then you are good to go, otherwise it is back to school until it can pass the test. My neural network doesn't have a validation set at the moment, but it seems to work quite well without it. This statement is not entirely true. I tend to validate it using the training data, but maybe in future I will fix it up to use a proper validation set.

So how do you train the neural network ?  Back-propagation !



Up Next: Neural Network (Part 5):  Back Propagation




To go back to the table of contents click here

Neural Network (Part 3): The Layer


The Layer

The purpose of a layer is mainly to group and manage neurons that are functionally similar, and provide a means to effectively control the flow of information through the network (forwards and backwards). The forward pass is when you convert input values into output values, the backwards pass is only done during network training.

In my neural network, the Layer is an essential building block of the neural network. They can work individually or as a team. Let us just focus on one layer for now

This is what a layer with two neurons looks like in my neural network:



As you can see from the picture above, I tend to treat each neuron as an individual.
Both neurons have the same connEntry values (cEn11 = cEn21) and (cEn12 = cEn22), which stem from the layerINPUTs. Other than that, the rest is different.  Which means that Neuron1 could produce an output value close to 1, and at the same time Neuron2 could produce an output close to 0, even though they have the same connEntry values at the start.


The following section will describe how the layerINPUTs get converted to actualOUTPUTs.

Step1: Populate the layerINPUTs:
layerINPUTs are just a placeholder for values that have been fed into the neural network, or come from a previous layer within the same neural network. If this is the first layer in the network, then the layerINPUTs would be the Neural Network's input values (eg Sensor data). If this is the 2nd layer in the network, then the layerINPUTs would equal the actualOUTPUTs of Layer One



Step2: Send each layerINPUT to the neuron's connection in the layer.
You will notice that every neuron in the same layer will have the same number of connections. This is because each neuron will connect only once to each of the layerINPUTs. The relationship between layerINPUTs and the connEntry values of a neuron are as follows.

          layerINPUTs1:                                                                                 
          cEn11 = layerINPUTs1                  Neuron1.Connection1.connEntry
          cEn21 = layerINPUTs1                  Neuron2.Connection1.connEntry


          layerINPUTs2:                                                                                  
          cEn12 = layerINPUTs2                  Neuron1.Connection2.connEntry
          cEn22 = layerINPUTs2                  Neuron2.Connection2.connEntry

Therefore, the connEntry of the first connection of every neuron in this layer will equal the first layerINPUT value in this layer.




Step3: Calculate the connExit values of each connection (including bias)

           Neuron 1:                       
           cEx11 = cEn11  x  cW11
           cEx12 = cEn12  x  cW12
             bEx1 =    1      x    bW1

           Neuron 2:                       
           cEx21 = cEn21  x  cW21
           cEx22 = cEn22  x  cW22
             bEx2 =    1      x    bW2




Step4: Calculate the NeuronInputValue for each neuron

          Neuron1:                                                                       
          Neuron1.NeuronInputValue = cEx11  +  cEx12  +  bEx1


          Neuron2:                                                                       
          Neuron2.NeuronInputValue = cEx21  +  cEx22  +  bEx2




Step5: Send the NeuronInputValues through an Activation function to produce a NeuronOutputValue

          Neuron1:                                                                                                        
          Neuron1.NeuronOutputValue= 1/(1+EXP(-1 x Neuron1.NeuronInputValue))


          Neuron2:                                                                                                 
          Neuron2.NeuronOutputValue= 1/(1+EXP(-1 x Neuron2.NeuronInputValue))


          Please note that the NeuronInputValues for each neuron are different !





Step6: Send the NeuronOutputValues to the layer's actualOUTPUTs
  • actualOUTPUT1 = Neuron1.NeuronOutputValue
  • actualOUTPUT2 = Neuron2.NeuronOutputValue



The NeuronOutputValues become the actualOUTPUTs of this layer, which then become the layerINPUTs of the next layer. And the process is repeated over and over until you reach the final layer, where the actualOUTPUTs become the outputs of the entire neural network.


Here is the code for the Layer class:


processing code Layer Class

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Layer{

Neuron[] neurons = {};
float[] layerINPUTs={};
float[] actualOUTPUTs={};
float[] expectedOUTPUTs={};
float layerError;
float learningRate;


/* This is the default constructor for the Layer */
Layer(int numberConnections, int numberNeurons){
/* Add all the neurons and actualOUTPUTs to the layer */
for(int i=0; i<numberNeurons; i++){
Neuron tempNeuron = new Neuron(numberConnections);
addNeuron(tempNeuron);
addActualOUTPUT();
}
}


/* Function to add an input or output Neuron to this Layer */
void addNeuron(Neuron xNeuron){
neurons = (Neuron[]) append(neurons, xNeuron);
}


/* Function to get the number of neurons in this layer */
int getNeuronCount(){
return neurons.length;
}


/* Function to increment the size of the actualOUTPUTs array by one. */
void addActualOUTPUT(){
actualOUTPUTs = (float[]) expand(actualOUTPUTs,(actualOUTPUTs.length+1));
}


/* Function to set the ENTIRE expectedOUTPUTs array in one go. */
void setExpectedOUTPUTs(float[] tempExpectedOUTPUTs){
expectedOUTPUTs=tempExpectedOUTPUTs;
}


/* Function to clear ALL values from the expectedOUTPUTs array */
void clearExpectedOUTPUT(){
expectedOUTPUTs = (float[]) expand(expectedOUTPUTs, 0);
}


/* Function to set the learning rate of the layer */
void setLearningRate(float tempLearningRate){
learningRate=tempLearningRate;
}


/* Function to set the inputs of this layer */
void setInputs(float[] tempInputs){
layerINPUTs=tempInputs;
}



/* Function to convert ALL the Neuron input values into Neuron output values in this layer,
through a special activation function. */

void processInputsToOutputs(){

/* neuronCount is used a couple of times in this function. */
int neuronCount = getNeuronCount();

/* Check to make sure that there are neurons in this layer to process the inputs */
if(neuronCount>0) {
/* Check to make sure that the number of inputs matches the number of Neuron Connections. */
if(layerINPUTs.length!=neurons[0].getConnectionCount()){
println("Error in Layer: processInputsToOutputs: The number of inputs do NOT match the number of Neuron connections in this layer");
exit();
} else {
/* The number of inputs are fine : continue
Calculate the actualOUTPUT of each neuron in this layer,
based on their layerINPUTs (which were previously calculated).
Add the value to the layer's actualOUTPUTs array. */

for(int i=0; i<neuronCount;i++){
actualOUTPUTs[i]=neurons[i].getNeuronOutput(layerINPUTs);
}
}
}else{
println("Error in Layer: processInputsToOutputs: There are no Neurons in this layer");
exit();
}
}


/* Function to get the error of this layer */
float getLayerError(){
return layerError;
}


/* Function to set the error of this layer */
void setLayerError(float tempLayerError){
layerError=tempLayerError;
}


/* Function to increase the layerError by a certain amount */
void increaseLayerErrorBy(float tempLayerError){
layerError+=tempLayerError;
}


/* Function to calculate and set the deltaError of each neuron in the layer */
void setDeltaError(float[] expectedOutputData){
setExpectedOUTPUTs(expectedOutputData);
int neuronCount = getNeuronCount();
/* Reset the layer error to 0 before cycling through each neuron */
setLayerError(0);
for(int i=0; i<neuronCount;i++){
neurons[i].deltaError = actualOUTPUTs[i]*(1-actualOUTPUTs[i])*(expectedOUTPUTs[i]-actualOUTPUTs[i]);

/* Increase the layer Error by the absolute difference between the calculated value (actualOUTPUT) and the expected value (expectedOUTPUT). */
increaseLayerErrorBy(abs(expectedOUTPUTs[i]-actualOUTPUTs[i]));
}
}


/* Function to train the layer : which uses a training set to adjust the connection weights and biases of the neurons in this layer */
void trainLayer(float tempLearningRate){
setLearningRate(tempLearningRate);

int neuronCount = getNeuronCount();

for(int i=0; i<neuronCount;i++){
/* update the bias for neuron[i] */
neurons[i].bias += (learningRate * 1 * neurons[i].deltaError);

/* update the weight of each connection for this neuron[i] */
for(int j=0; j<neurons[i].getConnectionCount(); j++){
neurons[i].connections[j].weight += (learningRate * neurons[i].connections[j].connEntry * neurons[i].deltaError);
}
}
}
}

Within each layer, you have neuron(s) and their associated connection(s). Therefore it makes sense to have a constructor that automatically sets these up.


If you create a new Layer(2,3), this would automatically
  • add 3 neurons to the layer
  • create 2 connections for each neuron in this layer (to connect to the previous layer neurons).
  • randomise each neuron bias and connection weights.
  • add 3 actualOUTPUT slots to hold the neuron output values.


                                                                                                                                         
Here are the functions of the Layer class:
  • addNeuron() : adds a neuron to the layer
  • getNeuronCount() : returns the number of neurons in this layer
  • addActualOUTPUT() : adds an actualOUTPUT slot to the layer.
  • setExpectedOUTPUTs() : sets the entire expectedOUTPUTs array in one go.
  • clearExpectedOUTPUT() : clear all values within the expectedOUTPUTs array.
  • setLearningRate() : sets the learning rate of the layer.
  • setInputs() : sets the inputs of the layer.
  • processInputsToOutputs() : convert all layer input values into output values
  • getLayerError() : returns the error of this layer
  • setLayerError() : sets the error of this layer
  • increaseLayerErrorBy() : increases the layer error by a specified amount.
  • setDeltaError() : calculate and set the deltaError for each neuron in this layer
  • trainLayer() : uses a training set to adjust the connection weights and biases of the neurons in this layer
There are a few functions mentioned above, which I have not yet discussed, and are used for neural network training (back-propagation). Don't worry, we'll go through them in the "back-propagation" section of the tutorial.


Next up:  Neural Network (Part 4) : The Neural Network class



To go back to the table of contents click here