From 23f2a942161bb3ebb400b12049e3f9ee218bab20 Mon Sep 17 00:00:00 2001 From: Lewis Van Winkle Date: Wed, 5 Sep 2018 08:06:25 -0500 Subject: [PATCH] added srand to examples --- example1.c | 6 ++++++ example2.c | 4 ++++ example4.c | 3 +++ test.c | 2 +- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/example1.c b/example1.c index 229880a..d3f3b4e 100644 --- a/example1.c +++ b/example1.c @@ -1,4 +1,6 @@ #include +#include +#include #include "genann.h" int main(int argc, char *argv[]) @@ -6,6 +8,10 @@ int main(int argc, char *argv[]) printf("GENANN example 1.\n"); printf("Train a small ANN to the XOR function using backpropagation.\n"); + /* This will make the neural network initialize differently each run. */ + /* If you don't get a good result, try again for a different result. */ + srand(time(0)); + /* Input and expected out data for the XOR function. */ const double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; const double output[4] = {0, 1, 1, 0}; diff --git a/example2.c b/example2.c index 35d7a97..fe63569 100644 --- a/example2.c +++ b/example2.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "genann.h" @@ -8,6 +9,8 @@ int main(int argc, char *argv[]) printf("GENANN example 2.\n"); printf("Train a small ANN to the XOR function using random search.\n"); + srand(time(0)); + /* Input and expected out data for the XOR function. */ const double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; const double output[4] = {0, 1, 1, 0}; @@ -27,6 +30,7 @@ int main(int argc, char *argv[]) if (count % 1000 == 0) { /* We're stuck, start over. */ genann_randomize(ann); + last_err = 1000; } genann *save = genann_copy(ann); diff --git a/example4.c b/example4.c index 8c07ac9..14de783 100644 --- a/example4.c +++ b/example4.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include "genann.h" @@ -74,6 +75,8 @@ int main(int argc, char *argv[]) printf("GENANN example 4.\n"); printf("Train an ANN on the IRIS dataset using backpropagation.\n"); + srand(time(0)); + /* Load the data from file. */ load_data(); diff --git a/test.c b/test.c index bc38f31..a7f895e 100644 --- a/test.c +++ b/test.c @@ -258,7 +258,7 @@ int main(int argc, char *argv[]) { printf("GENANN TEST SUITE\n"); - srand(100); + srand(100); //Repeatable test results. lrun("basic", basic); lrun("xor", xor);