2

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?

Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
Kader S
  • 21
  • 4
  • You forgot the question. – Dr. Snoopy Feb 27 '17 at 09:38
  • Problem is: The 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. But how do we resolve the problem. – Kader S Feb 27 '17 at 09:53
  • Can you provide an example of input/outputs both for Matlab, correct ones, and Arduino, incorrect ones? A curiosity, why exactly do you print the value using `hexadecimal` format? – Patrick Trentin Feb 27 '17 at 12:32
  • The given input is, p = [0 0.4175 4.5925]; Matlab generates output of 4.3624. Arduino generates output of 6.7564897537231445 The parameter 16 specified in Serial.println function is the number of decimal places to use while printing floating point numbers. Refer: https://www.arduino.cc/en/Serial/Print – Kader S Feb 28 '17 at 07:57
  • @KaderS I just ported your *source code* to a `C` [snippet](https://gist.github.com/PatrickTrentin88/994cc1b5a79644b0a78f22e115c45d02) file, and it prints `6.7564897537231445`, so there's that. Perhaps you should review the *matlab* porting to *c*, you didn't post the original code so we can't be of much help. The `C` code should be easier to *debug*, at least to compare partial results with your *matlab* version. – Patrick Trentin Feb 28 '17 at 16:36
  • The Matlab code to generate neural network is fairly simple. It begins with defining Inputs and outputs. Then created network, trained it, gave desired input and simulated to identify output. Main portion of the code is, net = newff(p',t,5,{'logsig'}); net = train(net,p',t); p = [0 0.4175 4.5925]; sim(net,p') Matlab prints, ans = 4.3624. – Kader S Mar 01 '17 at 05:53
  • @KaderS Why do you think the Matlab implementation is correct? If you're not going to post your Matlab code, so we can compare, no one can help you. – Cerin Apr 03 '18 at 04:10

0 Answers0