#include <math.h>
// global variables
const int R = 3;
const int C = 2;
const int N = 4;
const int ndigits = 4; //number of digits of precision in printed values
float node[R] = {};
float outputs[C] = {};
float errorH[R] = {};
float errorO[C] = {};
int Inputs[N] = { 1, 0, 1, 0 };
float WeightH[R][N] = {
{ 0.1, 0.9, 0.5, 0.25 },
{ -0.2, -0.1, 0.8, -0.45 },
{ 0.7, -0.6, -0.3, 0.4 }
};
double WeightO[C][R] = {
{ 0.75, -0.4, -0.5 },
{ -0.9, 0.2, 0.3 }
};
int Target[C] = { 1, 0 };
void setup() {
Serial.begin(19200);
// Hidden layer
for (int r = 0; r < R; r++) {
for (int n = 0; n < N; n++) {
node[r] = node[r] + (Inputs[n] * WeightH[r][n]);
}
}
// Sigmoid function
for (int r = 0; r < R; r++) {
node[r] = 1 / (1 + exp(-node[r]));
}
// Output layer
for (int n = 0; n < 2; n++) {//why not N
for (int c = 0; c < 2; c++) { //why not C
outputs[n] = outputs[n] + (node[c] * WeightO[n][c]);
}
}
// Sigmoid function
for (int c = 0; c < 2; c++) { //why not C
outputs[c] = 1 / (1 + exp(-outputs[c]));
}
// Reverse Pass //
// Errors
for (int c = 0; c < 2; c++) { //why not C
errorO[c] = outputs[c] * (1 - outputs[c]) * (Target[c] - outputs[c]);
}
// New Weights
for (int n = 0; n < 2; n++) { //why not N
for (int c = 0; c < 3; c++) { //why not C
WeightO[n][c] = WeightO[n][c] + (errorO[n] * node[c]);
}
}
// Hidden Errors
for (int r = 0; r < 3; r++) { //why not R
int n = 0;
errorH[r] = node[r] * (1 - node[r]) * ((WeightO[r][n] * errorO[n]) + (WeightO[r][n + 1] * errorO[n + 1]));
}
// New Hidden Layer Weights
for (int n = 0; n < 3; n++) { //why not N
for (int c = 0; c < 4; c++) {//why not C
WeightH[n][c] = WeightH[n][c] + (errorH[n] * Inputs[c]);
}
}
Serial.println();
Serial.println("Output Errors:");
Serial.println(errorO[0], ndigits);
Serial.println(errorO[1], ndigits);
Serial.println("Output Weights:");
Serial.println(WeightO[0][0], ndigits);
Serial.println(WeightO[0][1], ndigits);
Serial.println(WeightO[0][2], ndigits);
Serial.println(WeightO[1][0], ndigits);
Serial.println(WeightO[1][1], ndigits);
Serial.println(WeightO[1][2], ndigits);
Serial.println("Hidden Layer Errors:");
Serial.println(errorH[0], ndigits);
Serial.println(errorH[1], ndigits);
Serial.println(errorH[2], ndigits);
Serial.println("Hidden Layer Weights:");
Serial.println(WeightH[0][0], ndigits);
Serial.println(WeightH[0][1], ndigits);
Serial.println(WeightH[0][2], ndigits);
Serial.println(WeightH[0][3], ndigits);
Serial.println(WeightH[1][0], ndigits);
Serial.println(WeightH[1][1], ndigits);
Serial.println(WeightH[1][2], ndigits);
Serial.println(WeightH[1][3], ndigits);
Serial.println(WeightH[2][0], ndigits);
Serial.println(WeightH[2][1], ndigits);
Serial.println(WeightH[2][2], ndigits);
Serial.println(WeightH[2][3], ndigits);
}
void loop() {}