From 614df3c4a1bdb185cea048ed9a9e072a5b7bf4a3 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 19 Sep 2023 21:53:07 +0200 Subject: [PATCH] load first commit --- main.c | 25 +++++++++++++++++++++---- matrix.c | 16 ++++++++-------- matrix.h | 2 ++ networks/test1.txt | 45 +++++++++++++++++++++++++++++++++++++++++++++ neuronal_network.c | 36 ++++++++++++++++++++++++++++-------- neuronal_network.h | 2 ++ 6 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 networks/test1.txt diff --git a/main.c b/main.c index 0330abf..0407567 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,29 @@ #include -#include -#include #include "matrix.h" #include "image.h" +#include "neuronal_network.h" int main() { - Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 2); - img_visualize(images[1]); +// Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 2); +// img_visualize(images[1]); + +// Neural_Network* nn = new_network(4, 2, 3, 0.5); +// +// int n = 20; +// +// matrix_randomize(nn->bias_1, n); +// matrix_randomize(nn->bias_2, n); +// matrix_randomize(nn->bias_3, n); +// +// matrix_randomize(nn->weights_1, n); +// matrix_randomize(nn->weights_2, n); +// matrix_randomize(nn->weights_3, n); +// +// matrix_randomize(nn->weights_output, n); +// +// save_network(nn); + + Neural_Network* nn = load_network("../networks/test1.txt"); } \ No newline at end of file diff --git a/matrix.c b/matrix.c index 62a82d7..4cb9ca7 100644 --- a/matrix.c +++ b/matrix.c @@ -339,20 +339,20 @@ int matrix_argmax(Matrix* matrix) { } void matrix_randomize(Matrix* matrix, int n) { + //make a min and max - int min = -1.0 / sqrt(n); - int max = 1.0 / sqrt(n); + double min = -1.0f / sqrt(n); + double max = 1.0f / sqrt(n); + //calculate difference double difference = max - min; - //move decimal - int scale = 10000; - int scaled_difference = (int)(difference * scale); - //calculate final random int and move decimal back - double random_result = min + (1.0 * (rand() % scaled_difference) / scale ); + //move decimal + int scaled_difference = (int)(difference * scaling_value); + for (int i = 0; i < matrix->rows; i++) { for (int j = 0; j < matrix->columns; j++) { - matrix->numbers[i][j] = random_result; + matrix->numbers[i][j] = min + (1.0 * (rand() % scaled_difference) / scaling_value); } } } \ No newline at end of file diff --git a/matrix.h b/matrix.h index f3ba4be..1e88e47 100644 --- a/matrix.h +++ b/matrix.h @@ -5,6 +5,8 @@ typedef struct { double **numbers; } Matrix; +static const int scaling_value = 10000; + // operational functions Matrix* matrix_create(int rows, int columns); void matrix_fill(Matrix* matrix, double value); diff --git a/networks/test1.txt b/networks/test1.txt new file mode 100644 index 0000000..b7dcddf --- /dev/null +++ b/networks/test1.txt @@ -0,0 +1,45 @@ +4 +2 +3 +2 +1 +-0.2195067977 +-0.1657067977 +2 +4 +0.0297932023 +0.0289932023 +-0.2106067977 +-0.0132067977 +-0.1003067977 +-0.0923067977 +-0.1315067977 +0.1174932023 +2 +1 +-0.0374067977 +0.1903932023 +2 +2 +-0.1219067977 +-0.1745067977 +0.0758932023 +0.0761932023 +2 +1 +-0.0955067977 +0.0071932023 +2 +2 +-0.1881067977 +-0.1272067977 +-0.1149067977 +-0.1048067977 +3 +2 +0.1665932023 +-0.2083067977 +-0.1944067977 +0.1201932023 +0.1768932023 +-0.1408067977 diff --git a/neuronal_network.c b/neuronal_network.c index 72acda4..7490d1e 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -39,11 +39,7 @@ void free_network(Neural_Network* network){ void save_network(Neural_Network* network) { - // create file name and file string - time_t seconds; - time(&seconds); - char* file_name = "../networks/"; - sprintf(file_name, "%ld", seconds); + char* file_name = "../networks/newest_network.txt"; // create file FILE* save_file = fopen(file_name, "w"); @@ -81,7 +77,31 @@ void save_network(Neural_Network* network) { } Neural_Network* load_network(char* file) { - return NULL; + + // create file pointer and open file + FILE* save_file = fopen(file, "r"); + + // check if file could be opened + if(save_file == NULL) { + printf("ERROR: File could not be opened/found! (load_network)"); + exit(1); + } + + // read & store the information on the size of the network from the save file + char buffer[MAX_BYTES]; + fgets(buffer, MAX_BYTES, save_file); + int input_size = (int) strtol(buffer, NULL, 10); + fgets(buffer, MAX_BYTES, save_file); + int hidden_size = (int) strtol(buffer, NULL, 10); + fgets(buffer, MAX_BYTES, save_file); + int output_size = (int) strtol(buffer, NULL, 10); + + // create a new network to fill with the saved data + Neural_Network* saved_network = new_network(input_size, hidden_size, output_size, 0); + + + + return saved_network; } //double predict_images(Neural_Network* network, Image** images, int amount) { @@ -117,5 +137,5 @@ Neural_Network* load_network(char* file) { // return result; //} -void train_network(Neural_Network* network, Matrix* input, Matrix* output); -void batch_train_network(Neural_Network* network, Image** images, int size); \ No newline at end of file +//void train_network(Neural_Network* network, Matrix* input, Matrix* output); +//void batch_train_network(Neural_Network* network, Image** images, int size); \ No newline at end of file diff --git a/neuronal_network.h b/neuronal_network.h index 09ec61c..f8c3188 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -25,6 +25,8 @@ typedef struct { } Neural_Network; +static const int MAX_BYTES = 100; + Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate); //void print_network(Neural_Network* network); void free_network(Neural_Network* network);