Hello, I'm trying to implement this work on the Arduino mega2560 and using the Arduino IDE 2.3.2 compiler.
So far I haven't been able to make it work, I've tried several changes and haven't gotten any results.
I tried the first approach in the most predictable way, using the serial monitor and sending my vector of 784 elements extracted from the MNIST dataset, I tried with [ ], with { }, with 'space', completing with 3 elements like 0 = 000, changed the line ending CF, CRLF, I tried also with the vector declared directly and nothing.
I then tried switching compilers to VSCode and it didn't improve either.
In the Arduino IDE, 99% of the time I got a printed output "2", regardless of which vector I sent.
Does anyone have any suggestions or know how to solve it?
#include "LogNNet.h"
float Sh[P+1];
int i = 1;
byte Y[S+1];
void Hidden_Layer_Calculating(byte Y[S+1]) {
float W1 = 0;
Sh[0] = 1;
for (int j = 1; j <= P; j++) {
Sh[j] = 0;
for (int i = 0; i <= S; i++) {
W1 = A * sin(i / float(S) * PI / B);
for (int k = 2; k <= j; k++)
W1 = 1 - (r * W1 * W1);
Sh[j] = Sh[j] + Y[i]/255.0 * W1;
}
}
}
void Hidden_Layer_Normalization() {
Sh[0] = 1;
for (int j = 1; j <= P; j++)
Sh[j] = ((Sh[j] - minH[j-1]/100.0) /
(maxH[j-1]/100.0 - minH[j-1]/100.0))
- 0.5 - meanH[j-1]/10000.0;
}
byte Output_Layer() {
float Sout[M];
for (int j = 0; j < M; j++) {
Sout[j] = 0;
for (int i = 0; i <= P; i++)
Sout[j] = Sout[j] + Sh[i] * W2[i][j] / 100.0;
Sout[j] = 1 / (1 + exp(-1 * Sout[j]));
}
byte digit = 0;
for (int i = 0; i < M; i++) {
if (Sout[i] > Sout[digit])
digit = i;
}
return digit;
}
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
Y[i] = Serial.read();
i++;
if (i == S+1) {
Y[0] = 255;
i = 1;
Hidden_Layer_Calculating(Y);
Hidden_Layer_Normalization();
byte Digit = Output_Layer();
Serial.print(String(Digit));
}
}
}
//---- LogNNet 784:20:10 ------
#pragma once
#include <Arduino.h>
#define S 784
#define P 20
#define M 10
#define A 0.672304710111946
#define B 1.57740808024101
#define r 1.86376633343066
int maxH[P] = {16458, 13985, 16194, 4846, 9755, 12284, 5592, 5826, 9223,
6653, 3504, 11273, 3993, 5204, 7919, 3889, 9294, 6141, 5063, 5334};
int minH[P] = {1043, 944, 900, -2416, -1539, -2423, -2509, -816, -761, -2896,
-2205, -125, -2282, -457, -17, -2211, -112, -276, -858, 37};
int meanH[P] = {-2188, -2301, -2094, -595, -1707, -1204, -667, -1419, -1395,
-846, -1025, -1562, -451, -1382, -1471, -1016, -1929, -1348, -1137, -1274};
int W2[P+1][M] = {
{-491, -915, -453, -353, -455, -419, -516, -584, -324, -431},
{211, -522, 120, -463, -509, -115, 96, -412, 344, 16},
{17, -1044, 775, -133, -1325, -149, -105, -806, -539, -741},
{304, 56, -933, -890, 535, -152, 283, 229, 1362, 965},
{-95, -38, -2354, -4, 965, 219, -1500, 1769, 1154, 1590},
{-620, 4, -1564, -710, 2701, -1338, 1485, -657, 331, 897},
{-1012, 1187, 1633, -1741, -1798, -1191, 126, 667, 2425, 46},
{-644, -242, 391, -1942, -334, -64, 745, -131, 914, 838},
{-317, -523, -594, -825, -988, 991, 1085, -257, 373, 99},
{105, 565, -1483, 1147, 614, 1539, -364, -508, -389, -1703},
{-819, 331, 196, 2209, -900, -340, -471, 118, -781, -684},
{1250, -2406, -340, 925, -580, 416, -98, -850, -727, -664},
{-200, -1120, -480, 1527, -516, 245, -685, 68, -1278, -331},
{137, 542, -203, 464, -111, 323, -479, -399, -237, -77},
{7, -352, 312, -839, 778, 836, -501, -154, 1087, 220},
{712, 1207, 1556, -872, 431, 501, -752, -1718, 668, -1003},
{602, -996, 745, -123, 965, -1148, -1411, 1678, -923, -888},
{1558, -856, 14, 410, -1222, -1103, 752, -14, -1345, 602},
{-305, -712, 645, 913, -46, -1199, -969, -469, 86, 727},
{-588, -72, 655, 733, -20, -930, -197, 501, -1088, -244},
{-541, -1298, -427, 564, 599, 342, 218, 997, -947, -444}
};