why are you using an Arduino?
wouldn't a laptop be more suitable and convenient?
why are you using an Arduino?
wouldn't a laptop be more suitable and convenient?
When printing floating point numbers, it might be helpful to use more than two decimal places.
https://docs.arduino.cc/language-reference/en/functions/communication/serial/print/
Note the fine print about improving that.
Also, here on the forum, when asking follow-on questions, it's best to re-provide your most current code version, to ensure we're all studying the same functionality.
Thanks
Does it means you still not understand where your array indexing error was?
May be you need to print more decimals after point? See the serial.print() manual to know how to do it.
The learning process might go quite a bit faster if you study a complete, correctly programmed and working example, such as the one linked in post #6 above.
I'm only using an Arduino because I had it available from another project in a previous year. When you say using a laptop, what do you mean by that? Write the programmes without the Arduino?
I have fixed the out of bounds problems in my loops if that's what you mean?
And I have the values printing out at 4 decimal places for this reason and I can still confirm they are not quite right but I will go back to it and somehow set it up to alert me about all warnings and try to fix anything that shows up.
the arduino is for embedded applications that interface with sensors, motors, servos, ... Your neural network code is purely software that can be more quickly developed on a laptop that has a C++ compiler.
Maybe this is true, we wouldn't know because you haven't shown us anything since the original post. Seriously, if you desire help, you have to participate.
I decided to use the Arduino because it is the only way I've ever done it. It didn't seem like that much of a bother to carry a board and cable with me when I need it. I didn't think you could run code without a board of some sort?
The code from the initial post can be run on any system equipped with C++ compiler. Moreover, on any PC this code will work tens of times faster and can operate with matrices of several times larger dimensions.
Well like I said this is the first time I have used the forum, or in fact any forum, sorry I'm no expert. I think I have the new program included in this post.
bool finish = true;
void setup() {
Serial.begin(19200);
#include <math.h>
}
void loop() {
// global variables
const int R = 3;
const int C = 2;
const int N = 4;
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 };
while (finish) {
// 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++) {
for (int c = 0; c < 2; c++) {
outputs[n] = outputs[n] + (node[c] * WeightO[n][c]);
}
}
// Sigmoid function
for (int c = 0; c < 2; c++) {
outputs[c] = 1 / (1 + exp(-outputs[c]));
}
// Reverse Pass //
// Errors
for (int c = 0; c < 2; c++) {
errorO[c] = outputs[c] * (1 - outputs[c]) * (Target[c] - outputs[c]);
}
// New Weights
for (int n = 0; n < 2; n++) {
for (int c = 0; c < 3; c++) {
WeightO[n][c] = WeightO[n][c] + (errorO[n] * node[c]);
}
}
// Hidden Errors
for (int r = 0; r < 3; 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++) {
for (int c = 0; c < 4; c++) {
WeightH[n][c] = WeightH[n][c] + (errorH[n] * Inputs[c]);
}
}
Serial.println();
Serial.println("Output Errors:");
Serial.println(errorO[0], 4);
Serial.println(errorO[1], 4);
Serial.println("Output Weights:");
Serial.println(WeightO[0][0], 4);
Serial.println(WeightO[0][1], 4);
Serial.println(WeightO[0][2], 4);
Serial.println(WeightO[1][0], 4);
Serial.println(WeightO[1][1], 4);
Serial.println(WeightO[1][2], 4);
Serial.print("Hidden Layer Errors:");
Serial.println(errorH[0], 4);
Serial.println(errorH[1], 4);
Serial.println(errorH[2], 4);
Serial.println("Hidden Layer Weights:");
Serial.println(WeightH[0][0], 4);
Serial.println(WeightH[0][1], 4);
Serial.println(WeightH[0][2], 4);
Serial.println(WeightH[0][3], 4);
Serial.println(WeightH[1][0], 4);
Serial.println(WeightH[1][1], 4);
Serial.println(WeightH[1][2], 4);
Serial.println(WeightH[1][3], 4);
Serial.println(WeightH[2][0], 4);
Serial.println(WeightH[2][1], 4);
Serial.println(WeightH[2][2], 4);
Serial.println(WeightH[2][3], 4);
finish = false;
}
}
I have changed it slightly now from gcjr's example.
Putting includes inside a procedure is a bad idea. There's a very high probability that it won't compile or will work incorrectly.
You've already been told this before
Overall, the code feels like you're programming something for the first time.
For example, if you created variables for array dimensions:
why are you again using raw numbers instead?
The result will you'll get confused again.
Addition
Also take the note, that despite the comment, the R C N variables are not global.
Tidied up, structure simplified, declarations where they belong. No hardware required, just run it in Wokwi, for gosh sakes, if you want to retain any semblance of Arduino. FWIW:
most code running on PCs, MACs, phones and most any other enbedded systems is written in C/C++ (even Java, Python are written in C/C++)
i'm sure you can get a C++ compiler and other tools (e.g. make, bash, editors) on Linux. I use those same tools on Windows in the cygwin environment
The only other thing I have programmed is a line following robot using IR sensors and an Ultrasonic sensor. So I really don't have a lot of experience with most aspects of Arduino, using raw numbers helps me visualise what I am doing better. The global variables comment wasn't written by me and if I am honest I don't really know what it means. Like I said previously my main resource has been a basic introduction to coding with Arduino booklet from my lecturer, if there are any resources people would recommend I'd appreciate that.
what major are you studying at university? surely you'll need to learn to program in better environments than an Arduino
Google? There's no end to it.
There are many choices, here's links to a couple I've saved over the years:
and, of course, there's youtube.
I'm assuming you mean the course I'm studying? I'm on honours year of a Mechanical Engineering degree, so no electrical/electronic modules. But I'm interested in AI and had an Arduino from 2nd year so I started using that to make calculating simpler, and now trying to implement arrays to allow me to increase the number of inputs and outputs of my Neural Network. I also have a Raspberry Pi which I am hoping to learn how to programme with also, but for now Arduino is more familiar.
does't your university, which university, require all engineering students to learn to program?