From ab8d60532c6e9fd6e0b79b3a79463efb76eeecf1 Mon Sep 17 00:00:00 2001 From: Ghost_Element Date: Tue, 19 Sep 2023 13:50:16 +0200 Subject: [PATCH 1/8] neuroal_network.h done --- neuronal_network.h | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/neuronal_network.h b/neuronal_network.h index c0d14c8..c2b3c8c 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -1,7 +1,37 @@ #pragma once typedef struct { - Matrix* input; - Matrix* output; + int input_size; + //Matrix* input; as local variable given to function -} Neuronal_Network; \ No newline at end of file + // hidden layers + int hidden_size; + Matrix* weights_1; + Matrix* bias_1; + Matrix* weights_2; + Matrix* bias_2; + Matrix* weights_3; + Matrix* bias_3; + + int output_size = 10; + Matrix* weights_output; + //Matrix* bias_output; // do we need it? + //Matrix* output; as local variable given to function + + double learning_rate; + +} Neural_Network; + +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); + +void save_network(Neural_Network* network, char* file); +Neural_Network* load_network(char* file); + +double predict_images(Neural_Network* network, Image** images, int amount); +Matrix* predict_image(Neural_Network* network, Image*); +Matrix* predict(Neural_Network* network, Matrix* image_data); + +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 From 9592f1851c466604736831420504f236f2ad7446 Mon Sep 17 00:00:00 2001 From: Ghost_Element Date: Tue, 19 Sep 2023 13:54:01 +0200 Subject: [PATCH 2/8] neuroal_network.h done --- neuronal_network.cpp => neuronal_network.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename neuronal_network.cpp => neuronal_network.c (100%) diff --git a/neuronal_network.cpp b/neuronal_network.c similarity index 100% rename from neuronal_network.cpp rename to neuronal_network.c From 004e8560c6a312d93ae1d46ceb8685c0ce082a9d Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Tue, 19 Sep 2023 13:58:30 +0200 Subject: [PATCH 3/8] Header und 10 --- neuronal_network.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/neuronal_network.h b/neuronal_network.h index c2b3c8c..8b6ceb9 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -1,5 +1,8 @@ #pragma once +#include "matrix.h" +#include "image.h" + typedef struct { int input_size; //Matrix* input; as local variable given to function @@ -13,7 +16,7 @@ typedef struct { Matrix* weights_3; Matrix* bias_3; - int output_size = 10; + int output_size; Matrix* weights_output; //Matrix* bias_output; // do we need it? //Matrix* output; as local variable given to function From 13540377ffd32f3dba7478b03fb78e9f484a9c08 Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Tue, 19 Sep 2023 13:59:54 +0200 Subject: [PATCH 4/8] Neural Network.c outline --- neuronal_network.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/neuronal_network.c b/neuronal_network.c index dcd02e5..9bb8b39 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -1,3 +1,16 @@ -// -// Created by danie on 19.09.2023. -// + +#include "neuronal_network.h" + +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); + +void save_network(Neural_Network* network, char* file); +Neural_Network* load_network(char* file); + +double predict_images(Neural_Network* network, Image** images, int amount); +Matrix* predict_image(Neural_Network* network, Image*); +Matrix* predict(Neural_Network* network, Matrix* image_data); + +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 From 0c8b2c904411f9531fa4b946ef36d7da61656e52 Mon Sep 17 00:00:00 2001 From: Tocuro Date: Tue, 19 Sep 2023 14:46:40 +0200 Subject: [PATCH 5/8] i will nerma --- .gitignore | 4 +++- neuronal_network.c | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 868ec50..01adc20 100644 --- a/.gitignore +++ b/.gitignore @@ -53,4 +53,6 @@ Kernel Module Compile Results modules.order Module.symvers Mkfile.old -dkms.conf \ No newline at end of file +dkms.conf +/.idea/.name +/.idea/misc.xml diff --git a/neuronal_network.c b/neuronal_network.c index 9bb8b39..8f9c470 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -8,9 +8,38 @@ void free_network(Neural_Network* network); void save_network(Neural_Network* network, char* file); Neural_Network* load_network(char* file); -double predict_images(Neural_Network* network, Image** images, int amount); +double predict_images(Neural_Network* network, Image** images, int amount) { + int num_correct = 0; + for (int i = 0; i < amount; i++) { + Matrix* prediction = predict_image(network, images[i]); + if (matrix_argmax(prediction) == images[i]->image_label) { + num_correct++; + } + matrix_free(prediction); + } + return 1.0 * num_correct / amount; +} Matrix* predict_image(Neural_Network* network, Image*); -Matrix* predict(Neural_Network* network, Matrix* image_data); + +Matrix* predict(Neural_Network* network, Matrix* image_data) { + Matrix* hidden1_inputs = dot(network->weights_1, image_data); + Matrix* hidden1_outputs = apply(relu, hidden1_inputs); + + Matrix* hidden2_inputs = dot(network->weights_2, hidden1_outputs); + Matrix* hidden2_outputs = apply(relu, hidden2_inputs); + + + Matrix* final_inputs = dot(net->output_weights, hidden_outputs); + Matrix* final_outputs = apply(sigmoid, final_inputs); + Matrix* result = softmax(final_outputs); + + matrix_free(hidden_inputs); + matrix_free(hidden_outputs); + matrix_free(final_inputs); + matrix_free(final_outputs); + + 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 From b097f30d6473f1c58d0e99565c6f4067af8d635c Mon Sep 17 00:00:00 2001 From: Ghost_Element Date: Tue, 19 Sep 2023 14:47:18 +0200 Subject: [PATCH 6/8] neuroal_network.c new_network print_network - not needed free_network --- neuronal_network.c | 38 ++++++++++++++++++++++++++++++++++---- neuronal_network.h | 2 +- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/neuronal_network.c b/neuronal_network.c index 9bb8b39..1c58d21 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -1,9 +1,39 @@ - +#include #include "neuronal_network.h" -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); +Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){ + Neural_Network network = malloc(sizeof(Neural_Network)); + // initialize networks variables + network.input_size = input_size; + network.hidden_size = hidden_size; + network.output_size = output_size; + network.learning_rate = learning_rate; + + network.weights_1 = matrix_randomize(matrix_create(hidden_size, input_size)); + network.weights_2 = matrix_randomize(matrix_create(hidden_size, hidden_size)); + network.weights_3 = matrix_randomize(matrix_create(hidden_size, hidden_size)); + network.weights_output = matrix_randomize(matrix_create(output_size, hidden_size)); + network.bias_1 = matrix_randomize(matrix_create(hidden_size, 1)); + network.bias_2 = matrix_randomize(matrix_create(hidden_size, 1)); + network.bias_3 = matrix_randomize(matrix_create(hidden_size, 1)); + //network.bias_output = matrix_create(output_size, 1); // do we need it? + + return network; +} + +//void print_network(Neural_Network* network){}; + +void free_network(Neural_Network* network){ + matrix_free(network->weights_1); + matrix_free(network->weights_2); + matrix_free(network->weights_3); + matrix_free(network->weights_output); + matrix_free(network->bias_1); + matrix_free(network->bias_2); + matrix_free(network->bias_3); + free(network); +} + void save_network(Neural_Network* network, char* file); Neural_Network* load_network(char* file); diff --git a/neuronal_network.h b/neuronal_network.h index 8b6ceb9..a21d553 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -26,7 +26,7 @@ typedef struct { } Neural_Network; Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate); -void print_network(Neural_Network* network); +//void print_network(Neural_Network* network); void free_network(Neural_Network* network); void save_network(Neural_Network* network, char* file); From af81566c45e46f95a997aa1442ac34a9c00a4681 Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Tue, 19 Sep 2023 15:06:31 +0200 Subject: [PATCH 7/8] Save network and overworked save matrix --- matrix.c | 24 ++++++++++++++-------- neuronal_network.c | 50 ++++++++++++++++++++++++++++++++++++++++++++-- neuronal_network.h | 2 +- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/matrix.c b/matrix.c index e2f4416..62a82d7 100644 --- a/matrix.c +++ b/matrix.c @@ -248,21 +248,29 @@ Matrix* transpose(Matrix* matrix) { //file operations void matrix_save(Matrix* matrix, char* file_string){ - FILE *fptr = fopen(file_string, "w+"); - if(!fptr){ - printf("Unable to get handle for \"%s\"", file_string); + + // open the file in append mode + FILE *file = fopen(file_string, "a"); + + // check if the file could be found + if(file == NULL) { + printf("ERROR: Unable to get handle for \"%s\"! (matrix_save)", file_string); exit(1); } - fprintf(fptr, "%d\n", matrix->rows); - fprintf(fptr, "%d\n", matrix->columns); + // save the size of the matrix + fprintf(file, "%d\n", matrix->rows); + fprintf(file, "%d\n", matrix->columns); + + // save all the numbers of the matrix into the file for(int i = 0; i < matrix->rows; i++){ for(int j = 0; j < matrix->columns; j++){ - fprintf(fptr, "%.10f\n", matrix->numbers[i][j]); + fprintf(file, "%.10f\n", matrix->numbers[i][j]); } } - printf("saved matrix to %s", file_string); - fclose(fptr); + + // close the file + fclose(file); } Matrix* matrix_load(char* file_string){ diff --git a/neuronal_network.c b/neuronal_network.c index 9bb8b39..2a0bd22 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -1,12 +1,58 @@ #include "neuronal_network.h" +#include +#include 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); -void save_network(Neural_Network* network, char* file); -Neural_Network* load_network(char* file); +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); + + // create file + FILE* save_file = fopen(file_name, "w"); + + // check if file is successfully opened + if(save_file == NULL) { + printf("ERROR: Something went wrong in file creation! (save_network)"); + exit(1); + } + + // save network size to first line of the file + fprintf(save_file, "%d\n", network->input_size); + fprintf(save_file, "%d\n", network->hidden_size); + fprintf(save_file, "%d\n", network->output_size); + + // close the file + fclose(file_name); + + // save first layer + matrix_save(network->bias_1, file_name); + matrix_save(network->weights_1, file_name); + + // save second layer + matrix_save(network->bias_2, file_name); + matrix_save(network->weights_2, file_name); + + // save third layer + matrix_save(network->bias_3, file_name); + matrix_save(network->weights_3, file_name); + + // save output weights + matrix_save(network->weights_output, file_name); + + printf("Network Saved!"); +} + +Neural_Network* load_network(char* file) { + +} double predict_images(Neural_Network* network, Image** images, int amount); Matrix* predict_image(Neural_Network* network, Image*); diff --git a/neuronal_network.h b/neuronal_network.h index 8b6ceb9..09894ef 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -29,7 +29,7 @@ Neural_Network* new_network(int input_size, int hidden_size, int output_size, do void print_network(Neural_Network* network); void free_network(Neural_Network* network); -void save_network(Neural_Network* network, char* file); +void save_network(Neural_Network* network); Neural_Network* load_network(char* file); double predict_images(Neural_Network* network, Image** images, int amount); From dfbe8fe1af0e48b4dd482ede90509303da916191 Mon Sep 17 00:00:00 2001 From: Tocuro Date: Tue, 19 Sep 2023 15:21:38 +0200 Subject: [PATCH 8/8] predict funzt, hoffentlich --- neuronal_network.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/neuronal_network.c b/neuronal_network.c index fb10d6a..3b773e5 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -52,20 +52,19 @@ double predict_images(Neural_Network* network, Image** images, int amount) { Matrix* predict_image(Neural_Network* network, Image*); Matrix* predict(Neural_Network* network, Matrix* image_data) { - Matrix* hidden1_inputs = dot(network->weights_1, image_data); - Matrix* hidden1_outputs = apply(relu, hidden1_inputs); + Matrix* hidden1_outputs = apply(relu, add(dot(network->weights_1, image_data), network->bias_1)); - Matrix* hidden2_inputs = dot(network->weights_2, hidden1_outputs); - Matrix* hidden2_outputs = apply(relu, hidden2_inputs); + Matrix* hidden2_outputs = apply(relu, add(dot(network->weights_2, hidden1_outputs), network->bias_2)); + Matrix* hidden3_outputs = apply(relu, add(dot(network->weights_3, hidden2_outputs), network->bias_3)); + + Matrix* final_outputs = apply(relu, dot(network->weights_output, hidden3_outputs)); - Matrix* final_inputs = dot(net->output_weights, hidden_outputs); - Matrix* final_outputs = apply(sigmoid, final_inputs); Matrix* result = softmax(final_outputs); - matrix_free(hidden_inputs); - matrix_free(hidden_outputs); - matrix_free(final_inputs); + matrix_free(hidden1_outputs); + matrix_free(hidden2_outputs); + matrix_free(hidden3_outputs); matrix_free(final_outputs); return result;