Sunday, August 14, 2011

Neural Network (Part 6) : Back Propagation, a worked example

A worked example of a Back-propagation training cycle.




In this example we will create a 2 layer network (as seen above), to accept 2 readings, and produce 2 outputs. The readings are (0,1) and the expectedOutputs in this example are (1,0).

Step 1: Create the network

NeuralNetwork NN = new NeuralNetwork();   
NN.addLayer(2,2);
NN.addLayer(2,2);
float[] readings = {0,1};
float[] expectedOutputs = {1,0};
NN.trainNetwork(readings,expectedOutputs);

This neural network will have randomised weights and biases when created.
Let us assume that the network generates the following random variables:

LAYER1.Neuron1
Layer1.Neuron1.Connection1.weight = cW111 = 0.3
Layer1.Neuron1.Connection2.weight = cW112 = 0.8
Layer1.Neuron1.Bias = bW11 = 0.5

LAYER1.Neuron2
Layer1.Neuron2.Connection1.weight = cW121 =  0.1
Layer1.Neuron2.Connection2.weight = cW122 =  0.1
Layer1.Neuron2.Bias = bW12 = 0.2

LAYER2.Neuron1
Layer2.Neuron1.Connection1.weight = cW211 = 0.6
Layer2.Neuron1.Connection2.weight = cW212 = 0.4
Layer2.Neuron1.Bias = bW21 = 0.4

LAYER2.Neuron2
Layer2.Neuron2.Connection1.weight = cW221 = 0.9
Layer2.Neuron2.Connection2.weight = cW222 = 0.9
Layer2.Neuron2.Bias = bW22 = 0.5




Step 2: Process the Readings through the Neural Network

a) Provide the Readings to the first layer, and calculate the neuron outputs

The readings provided to the neural network is (0,1), which go straight through to the first layer (layer1).
Starting with Layer 1:
Layer1.INPUT1 = 0
Layer1.INPUT2 =1

   Calculate Layer1.Neuron1.NeuronOutput
   ConnExit (cEx111) = ConnEntry (cEn111)  x Weight (cW111) = 0 x 0.3 = 0;
   ConnExit (cEx112) = ConnEntry (cEn112)  x Weight (cW112) = 1 x 0.8 = 0.8;
   Bias (bEx11) = ConnEntry (1) x Weight (bW11) = 1 x 0.4 = 0.4
   NeuronInputValue11 = 0 + 0.8 + 0.4 = 1.2
   NeuronOutputValue11 = 1/(1+EXP(-1 x 1.2)) = 0.768525
  
  Calculate Layer1.Neuron2.NeuronOutput
   ConnExit (cEx121) = ConnEntry (cEn121)  x Weight (cW121) = 0 x 0.1 = 0;
   ConnExit (cEx122) = ConnEntry (cEn122)  x Weight (cW122) = 1 x 0.1 = 0.1;
   Bias (bEx12) = ConnEntry (1) x Weight (bW12) = 1 x 0.2 = 0.2
   NeuronInputValue12 = 0 + 0.1 + 0.2 = 0.3
   NeuronOutputValue12 = 1/(1+EXP(-1 x 0.3)) = 0.574443


b) Provide LAYER2 with Layer 1 Outputs.

Now lets move to  Layer 2:
Layer2.INPUT1 = NeuronOutputValue11 = 0.768525
Layer2.INPUT2 = NeuronOutputValue12 = 0.574443

   Calculate Layer2.Neuron1.NeuronOutput
   ConnExit (cEx211) = (cEn211)  x Weight (cW211) = 0.768525 x 0.6 = 0.461115;
   ConnExit (cEx212) = (cEn212)  x Weight (cW212) = 0.574443 x 0.4 = 0.229777;
   Bias (bEx21) = ConnEntry (1) x Weight (bW21) = 1 x 0.4 = 0.4
   NeuronInputValue21 = 0.461115 + 0.229777 + 0.4 = 1.090892
   NeuronOutputValue21 = 1/(1+EXP(-1 x 1.090892)) = 0.74855
  
  Calculate Layer2.Neuron2.NeuronOutput
   ConnExit (cEx221) = (cEn221)  x Weight (cW221) = 0.768525  x 0.1 = 0.076853;
   ConnExit (cEx222) = (cEn222)  x Weight (cW222) = 0.574443  x 0.1 = 0.057444;
   Bias(bEx22) = ConnEntry (1) x Weight (bW22) = 1 x 0.5 = 0.5
   NeuronInputValue22 = 0.076853 + 0.057444 + 0.5 = 0.634297  
   NeuronOutputValue22 = 1/(1+EXP(-1 x 0.634297)) = 0.653463



Step 3) Calculate the delta error for neurons in layer 2
     -Because layer 2 is the last layer in this neural network -
      we will use the expected output data (1,0) to calculate the delta error.
   
LAYER2.Neuron1:
Let Layer2.ExpectedOutput1 = eO21 = 1    
      Layer2.ActualOutput1= aO21 = NeuronOutputValue21= 0.74855         
      Layer2.Neuron1.deltaError1 = dE21

dE21 =     aO21       x      (1 - aO21)     x  (eO21 - aO21)
       =  (0.74855)  x  (1 - 0.74855)  x  (1 - 0.74855)
        = (0.74855)  x     (0.25145)     x    (0.25145)
        = 0.047329



LAYER2.Neuron2:
Let Layer2.ExpectedOutput2 = eO22 = 0         
      Layer2.ActualOutput2     = aO22 = NeuronOutputValue22 = 0.653463      
      Layer2.Neuron2.deltaError = dE22

dE22  =      aO22       x      (1 - aO22)       x  (eO22 - aO22)
        = (0.653463)  x  (1 - 0.653463)  x  (0 - 0.653463)
        = (0.653463)  x     (0.346537)     x    (-0.653463)
        = -0.14797




Step 4) Calculate the delta error for neurons in layer 1

LAYER1.Neuron1 delta Error calculation

Let              Layer1.Neuron1.deltaError  = dE11 
                            Layer1.actualOutput1  = aO11 = NeuronOutputValue11 =  0.768525
      Layer2.Neuron1.Connection1.weight = cW211   =  0.6
                     Layer2.Neuron1.deltaError = dE21 =  0.047329
      Layer2.Neuron2.Connection1.weight = cW221   =  0.9
                     Layer2.Neuron2.deltaError = dE22 = -0.14797

dE11 = (aO11)          x  (1 -   aO11)         x ( [cW211   x   dE21]      +   [cW221  x    dE22] )
           = (0.768525) x   (1 - 0.768525)     x   ([0.6        x  0.047329]  +   [  0.9      x  -0.14797]  )
           = -0.01864

LAYER1.Neuron2 delta Error calculation

Let              Layer1.Neuron2.deltaError  = dE12 
                            Layer1.actualOutput2  = aO12    = NeuronOutputValue12 =  0.574443
      Layer2.Neuron1.Connection2.weight = cW212   =  0.4
                     Layer2.Neuron1.deltaError = dE21 =  0.047329
      Layer2.Neuron2.Connection2.weight = cW222   =  0.9
                     Layer2.Neuron2.deltaError = dE22 = -0.14797

dE12  = (aO12)          x  (1 -   aO12)         x ( [cW212  x     dE21]      +   [cW222  x    dE22] )
           = (0.574443) x   (1 - 0.574443)  x     ([0.4      x  0.047329]  +      [  0.9      x  -0.14797]  )
           = -0.02793





Step 5) Update Layer_2 neuron connection weights and bias (with a learning rate (LR) = 0.1)


Layer 2, Neuron 1 calculations:

Let
Layer2.Neuron1.Connection1.New_weight = New_cW211
Layer2.Neuron1.Connection1.Old_weight   =   Old_cW211   = 0.6
Layer2.Neuron1.Connection1.connEntry =                 cEn211 = 0.768525
Layer2.Neuron1.deltaError =                                       dE21 = 0.047329

New_cW211 = Old_cW211 + (LR x cEn211 x dE21)
                     =    0.6            + (0.1 x 0.768525 x 0.047329)
                     =    0.6            + ( 0.003627)
                     =    0.603627



Layer2.Neuron1.Connection2.New_weight = New_cW212
Layer2.Neuron1.Connection2.Old_weight   =   Old_cW212 = 0.4
Layer2.Neuron1.Connection2.connEntry =                cEn212 = 0.574443
Layer2.Neuron1.deltaError =                                      dE21 = 0.047329

New_cW212 = Old_cW212 + (LR x cEn212 x dE21)
                     =    0.4            + (0.1 x 0.574443 x 0.047329)
                     =    0.4            + (0.002719)
                     =    0.402719



Layer2.Neuron1.New_Bias = New_Bias21
Layer2.Neuron1.Old_Bias =    Old_Bias21 = 0.4
Layer2.Neuron1.deltaError =             dE21 = 0.047329

New_Bias21 = Old_Bias21 + (LR x  1  x  de21)
                     =  0.4              + (0.1 x 1  x 0.047329)
                     =  0.4              + (0.0047329)
                     =  0.4047329


--------------------------------------------------------------------

Layer 2, Neuron 2 calculations:

Layer2.Neuron2.Connection1.New_weight = New_cW221
Layer2.Neuron2.Connection1.Old_weight =    Old_cW221 = 0.9
Layer2.Neuron2.Connection1.connEntry =               cEn221 = 0.768525
Layer2.Neuron2.deltaError =                                     dE22 = -0.14797

New_cW221 = Old_cW221 + (LR x cEn221 x dE22)
                     =    0.9            + (0.1 x 0.768525 x -0.14797)
                     =    0.9            + ( -0.01137)
                     =    0.88863


Layer2.Neuron2.Connection2.New_weight = New_cW222
Layer2.Neuron2.Connection2.Old_weight =    Old_cW222 = 0.9
Layer2.Neuron2.Connection2.connEntry =              cEn222 = 0.574443
Layer2.Neuron2.deltaError =                                    dE22 = -0.14797

New_cW222 = Old_cW222 + (LR x cEn222 x dE22)
                     =    0.9            + (0.1 x 0.574443 x -0.14797)
                     =    0.9            + (-0.0085)
                     =    0.8915


Layer2.Neuron2.New_Bias = New_Bias22
Layer2.Neuron2.Old_Bias =    Old_Bias22 =  0.5
Layer2.Neuron2.deltaError =             dE22 = -0.14797

New_Bias22 = Old_Bias22 + (LR x  1  x  de22)
                     =  0.5              + (0.1 x  1  x  -0.14797)
                     =  0.5            +   (-0.014797)
                     =  0.485203



--------------------------------------------------------------------------


Step 6) Update Layer_1 neuron connection weights and bias.

Layer 1, Neuron 1 calculations:

Let
Layer1.Neuron1.Connection1.New_weight = New_cW111
Layer1.Neuron1.Connection1.Old_weight   =   Old_cW111   =  0.3
Layer1.Neuron1.Connection1.connEntry =                 cEn111 = 0
Layer1.Neuron1.deltaError =                                       dE11 = -0.01864

New_cW111 = Old_cW111 + (LR   x  cEn111   x   dE11)
                     =  0.3              +   (0.1   x     0      x    -0.01864)
                     =  0.3              +   ( 0 )
                     =  0.3    


Layer1.Neuron1.Connection2.New_weight = New_cW112
Layer1.Neuron1.Connection2.Old_weight   =   Old_cW112 = 0.8
Layer1.Neuron1.Connection2.connEntry =               cEn112 = 1
Layer1.Neuron1.deltaError =                                      dE11 = -0.01864

New_cW112 = Old_cW112 + (LR   x  cEn112   x   dE11)
                     =  0.8    +            (0.1     x    1     x     -0.01864)
                     =  0.8    +            (-0.001864)
                     =  0.798136   


Layer1.Neuron1.New_Bias = New_Bias11
Layer1.Neuron1.Old_Bias =    Old_Bias11 = 0.5
Layer1.Neuron1.deltaError =             dE11 = -0.01864

New_Bias11 = Old_Bias11 + (LR   x  1   x  dE11)
                     =  0.5              + (0.1   x 1   x -0.01864 )
                     =  0.5              + (-0.001864)
                     =  0.498136

--------------------------------------------------------------------

Layer 1, Neuron 2 calculations:

Layer1.Neuron2.Connection1.New_weight = New_cW121
Layer1.Neuron2.Connection1.Old_weight =    Old_cW121 = 0.1
Layer1.Neuron2.Connection1.connEntry =               cEn121 = 0
Layer1.Neuron2.deltaError =                                     dE12 =   -0.02793

New_cW121 = Old_cW121 + (LR  x  cEn121 x dE12)
                     =  0.1               + (0.1  x     0     x  -0.02793 )
                     =  0.1   +   (0)
                     =  0.1




Layer1.Neuron2.Connection2.New_weight = New_cW122
Layer1.Neuron2.Connection2.Old_weight =    Old_cW122 = 0.1
Layer1.Neuron2.Connection2.connEntry =              cEn122 = 1
Layer1.Neuron2.deltaError =                                    dE12 =  -0.02793

New_cW122 = Old_cW122 + (LR  x  cEn122  x   dE12)
                     =  0.1                + (0.1   x    1      x  -0.02793)
                     =  0.1    +  (-0.002793)
                     =  0.097207



Layer1.Neuron2.New_Bias = New_Bias12
Layer1.Neuron2.Old_Bias =    Old_Bias12 =  0.2
Layer1.Neuron2.deltaError =             dE12 =  -0.02793

New_Bias12 = Old_Bias12 + (LR    x  1  x  de12)
                     =  0.2             +   (0.1  x  1  x  -0.02793)
                     =  0.2             +  (-0.002793)
                     =  0.197207


----------------------------------------------------------------------

All done. That was just one training cycle. Thank goodness we have computers !
A computer can process these calculations really quickly, and depending on how complicated your neural network is (ie. number of layers, and number of neurons per layer), you may find that the training procedure may take some time. But believe me, if you have designed it right, it is well worth the wait.
Because once you have the desired weights and bias values set up, you are good to go, and as you receive data, the computer can do a single forward pass in a fraction of a second, and you will get your desired output, hopefully :)

Here is a complete Processing.org script that demonstrates the use of my neural network.
Neural Network (Part 7): Cut and Paste Code (click here).

If you liked my tutorial - please let me know in the comments. It is sometimes hard to know if anyone is actually reading this stuff. If you use my code in your own project, I am also happy for you to leave a link to a YouTube video etc in the comments also.

To go back to the table of contents click here

Friday, August 12, 2011

Neural Network (Part 5): The Back Propagation process

Back-propagation

Back propagation is the process by which you move backwards through the neural network to adjust the weights and biases so as to reduce the total error of the network. The total error of the network is essentially the difference between the end results (actual Outputs) and the expected results. If you expected to get a result of 1, but instead got a 0:  you would go back through the network and tweak each of the weights (and bias) values so that your end result was a little bit closer to 1 than before.

The process of back-propagation is such that larger errors and larger weights and biases that create those errors are penalised more than their smaller counterparts. Bigger weights have a bigger influence on the final outcome than smaller weights, and are therefore penalised more for incorrect answers.

After many training cycles, the neural network reaches a stage of equilibrium (not quite, but close enough), whereby the tweaking is insignificant to the final outcome.

If you under-train, then you will get the wrong result more often than desired.
If you over-train, then the neural network will not be able to "think outside the sqaure", so to speak.

So how do you propagate backwards ??



Step 1:  Feed-forward pass through:
Send some data through the network to populate all the variables. This feed-forward pass allows you calculate your actualOUTPUTs, which you will use to compare against your expectedOUTPUTs.



Step 2: Calculate delta-error for the neurons in the last layer (output layer).
The delta-error calculation for the neuron(s) in the last layer of the neural network is a liitle bit different than the other layers. You can work this out once you calculate the actualOUTPUTs from the feedforward pass.

Let  Last Layer Neuron1.deltaError = LLN1.dE
       Last Layer.actualOutput1 = aO1            <--- This is the same as the Neuron1 Output Value
       Last Layer.expectedOutput1 = exO1

  •        LLN1.dE = (aO1) x (1-aO1) x (exO1 - aO1);


Once you have calculated the deltaError for every neuron in the last layer (output layer), you can move onto the next step.



Step 3: Calculate the delta-error for the hidden layer neurons

The hidden layers for this neural network, is any layer in the neural network, that is not the last layer. However, each layer should sit like ducks in a row. And we are now going to calculate the delta error for the second last layer in the neural network. This could in theory be the first layer in the network (if this network only had 2 layers).


HLN = Hidden Layer Neuron, 
LLN = Last Layer Neuron,
aO=actualOUTPUT,
dE=deltaError


HLN.dE = (HLN.aO) x (1-HLN.aO) x (Sum of   [LLN.dE   x   LLN to HLN connection weight])


Keep moving back through the network layers until you reach the 1st layer (ie, you run out of layers).



Step 4: Update the weights of the connections and Bias of neuron.
a) Multiply the neuron's deltaError which was calculated in either step 2 or 3, by the learning rate (0.1), and by the connection's connEntry value.
b) Then add this calculated value (in Step (4a)) to the current weight of the connection.

neuron.connections[i].weight += (learningRate * neuron.connections[i].connEntry * neuron.deltaError);

The bias is like a connection with a constant connEntry of 1, therefore the calculation is

neuron.bias +=  (learningRate * 1 * neuron.deltaError);




Up Next: Neural Network (Part 6):



To go back to the table of contents click here



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