Currently I'm trying to implement the “Case Study on Function Approximation” presented by Martin Howard et all in their book Neural Network Design. Book is free downloadable.
The idea is to implement network using minimum hardware, hence, I went on for developing network model offline on PC and then implementing the network on hardware.
To begin I used neural network toolbox and associated functions of Matlab. Neural network was developed and trained in Matlab. Neural network structure, its weight and bias values are identified from Matlab.
We have three inputs and one output. Hence the network is of 3:5:1.
Implemented the above developed network on Arduino Uno board using the code below,
#define numberSensorInputs 3
#define numberHiddenNeurons 5
float sensorValue[numberSensorInputs]= {0,0.4175,4.5925};
float hiddenLayerOut[numberHiddenNeurons];
float hiddenLayerWeights[numberHiddenNeurons][numberSensorInputs]= {{38.498376246633022,4.8228789906421374,1.3602896899243053},{3.8474962878030912,-0.84371644817851899,-28.923549991793433},{-27.727414898404337,-11.923090632680966,19.537676225841405},{3.8569873599987994,-0.84731975744675336,0.17305115908902985},{0.29236102795668545,0.58525756760800285,-0.38214991270897147}};
float hiddenLayerBias[numberHiddenNeurons]= {39.11544288218294,-28.225689258527012,-16.517408802943045,0.8700678975676468,0.90940774428664883};
float outputLayerWeights[numberHiddenNeurons]= {-0.76894513764837624,-10.951307034874729,0.2457977459618178,10.887280941632419,4.3216425653566999};
float outputLayerBias = -2.8262415757548638;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
// Parameter passed is the neuron number.
for(int j=0; j<numberHiddenNeurons; j++){
hiddenLayerOut[j] = ComputeTF(SumOfHiddenNeurons(j));
}
float sum = SumOfOutputNeuron();
Serial.println(sum,16);
delay(20000);
}
float SumOfOutputNeuron(){
float result = 0;
for(int j=0; j<numberHiddenNeurons; j++){
result = result+outputLayerWeights[j]*hiddenLayerOut[j];
Serial.print("Output Neuron Product for ");Serial.print(j);Serial.print(" is ");Serial.println(result,16);
}
result = result+outputLayerBias;
Serial.print("Output Neuron Sum with bias: ");Serial.println(result,16);
return result;
}
float SumOfHiddenNeurons(int neuronNumber){
float result = 0;
for(int j=0; j<numberSensorInputs; j++){
Serial.print("Hidden Neuron Weight for ");Serial.print(j);Serial.print(" is ");Serial.println(hiddenLayerWeights[neuronNumber][j],16);
result = result+hiddenLayerWeights[neuronNumber][j]*sensorValue[j];
Serial.print("Hidden Neuron Product for ");Serial.print(j);Serial.print(" is ");Serial.println(result,16);
}
result = result+hiddenLayerBias[neuronNumber];
Serial.print("Hidden Neuron Sum with bias: ");Serial.println(result,16);
return result;
}
/* Computing Log-Sigmoid Transfer Function*/
float ComputeTF(float neuronSum){
float result = 0;
result = (1.0/(1.0 + exp(-neuronSum)));
Serial.print("Hidden Neuron output: ");Serial.println(result,16);
return result;
}
Results obtained from both implementations (Matlab and Arduino) are different. There’s a huge difference in the result. It’s understood that the difference is due to number precision of both Matlab and Arduino.
How do we resolve the problem?