From 2654733c2754b8fe7e2d3efe19d729eeb6419f39 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Thu, 21 Sep 2023 09:36:40 +0200 Subject: [PATCH 1/4] (feat) load_next_matrix --- matrix.c | 10 ++++++++++ matrix.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/matrix.c b/matrix.c index 4cb9ca7..be1a530 100644 --- a/matrix.c +++ b/matrix.c @@ -279,6 +279,14 @@ Matrix* matrix_load(char* file_string){ printf("Could not open \"%s\"", file_string); exit(1); } + Matrix * m = load_next_matrix(fptr); + fclose(fptr); + return m; + +} + +Matrix * load_next_matrix(FILE *fptr){ + char buffer[MAX_BYTES]; fgets(buffer, MAX_BYTES, fptr); @@ -321,6 +329,8 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { return result_matrix; } + + int matrix_argmax(Matrix* matrix) { // Expects a Mx1 matrix if (matrix->columns != 1){ diff --git a/matrix.h b/matrix.h index 1e88e47..cc80043 100644 --- a/matrix.h +++ b/matrix.h @@ -1,4 +1,5 @@ #pragma once +#include typedef struct { int rows, columns; @@ -15,6 +16,7 @@ void matrix_print(Matrix *matrix); Matrix* matrix_copy(Matrix *matrix); void matrix_save(Matrix* matrix, char* file_string); Matrix* matrix_load(char* file_string); +Matrix* load_next_matrix(FILE * fptr); void matrix_randomize(Matrix* matrix, int n); // don't understand the usage of the n int matrix_argmax(Matrix* matrix); From d9b6e342c007781f0161aa2e4b23df463764581f Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Thu, 21 Sep 2023 09:56:10 +0200 Subject: [PATCH 2/4] load network --- .gitignore | 2 ++ main.c | 20 ++++---------------- matrix.c | 17 ++++++++--------- matrix.h | 2 +- neuronal_network.c | 10 +++++++++- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 01adc20..a412b2a 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,5 @@ Mkfile.old dkms.conf /.idea/.name /.idea/misc.xml +/.idea/shelf/Uncommitted_changes_before_Update_at_21_09_23,_09_38_[Changes]/shelved.patch +/.idea/shelf/Uncommitted_changes_before_Update_at_21_09_23,_09_38_[Changes]/shelved.patch diff --git a/main.c b/main.c index 0407567..8ddb221 100644 --- a/main.c +++ b/main.c @@ -5,25 +5,13 @@ #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, 60000); +// img_visualize(images[4]); // 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); -// +// randomize_network(nn, 20); // save_network(nn); - Neural_Network* nn = load_network("../networks/test1.txt"); +// Neural_Network* nn = load_network("../networks/test1.txt"); } \ No newline at end of file diff --git a/matrix.c b/matrix.c index be1a530..9cbd386 100644 --- a/matrix.c +++ b/matrix.c @@ -246,7 +246,6 @@ Matrix* transpose(Matrix* matrix) { } -//file operations void matrix_save(Matrix* matrix, char* file_string){ // open the file in append mode @@ -274,38 +273,40 @@ void matrix_save(Matrix* matrix, char* file_string){ } Matrix* matrix_load(char* file_string){ + FILE *fptr = fopen(file_string, "r"); + if(!fptr){ printf("Could not open \"%s\"", file_string); exit(1); } + Matrix * m = load_next_matrix(fptr); + fclose(fptr); return m; - } -Matrix * load_next_matrix(FILE *fptr){ +Matrix* load_next_matrix(FILE *save_file){ char buffer[MAX_BYTES]; - fgets(buffer, MAX_BYTES, fptr); + fgets(buffer, MAX_BYTES, save_file); int rows = (int)strtol(buffer, NULL, 10); - fgets(buffer, MAX_BYTES, fptr); + fgets(buffer, MAX_BYTES, save_file); int cols = (int)strtol(buffer, NULL, 10); Matrix *matrix = matrix_create(rows, cols); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ - fgets(buffer, MAX_BYTES, fptr); + fgets(buffer, MAX_BYTES, save_file); matrix->numbers[i][j] = strtod(buffer, NULL); } } return matrix; } - Matrix* matrix_flatten(Matrix* matrix, int axis) { // Axis = 0 -> Column Vector, Axis = 1 -> Row Vector Matrix* result_matrix; @@ -329,8 +330,6 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { return result_matrix; } - - int matrix_argmax(Matrix* matrix) { // Expects a Mx1 matrix if (matrix->columns != 1){ diff --git a/matrix.h b/matrix.h index cc80043..429d5bf 100644 --- a/matrix.h +++ b/matrix.h @@ -16,7 +16,7 @@ void matrix_print(Matrix *matrix); Matrix* matrix_copy(Matrix *matrix); void matrix_save(Matrix* matrix, char* file_string); Matrix* matrix_load(char* file_string); -Matrix* load_next_matrix(FILE * fptr); +Matrix* load_next_matrix(FILE * save_file); void matrix_randomize(Matrix* matrix, int n); // don't understand the usage of the n int matrix_argmax(Matrix* matrix); diff --git a/neuronal_network.c b/neuronal_network.c index 4682233..f64f66b 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -112,8 +112,16 @@ Neural_Network* load_network(char* file) { // create a new network to fill with the saved data Neural_Network* saved_network = new_network(input_size, hidden_size, output_size, 0); + // load matrices from file into struct + saved_network->bias_1 = load_next_matrix(save_file); + saved_network->weights_1 = load_next_matrix(save_file); + saved_network->bias_2 = load_next_matrix(save_file); + saved_network->weights_2 = load_next_matrix(save_file); + saved_network->bias_3 = load_next_matrix(save_file); + saved_network->weights_3 = load_next_matrix(save_file); + saved_network->weights_output = load_next_matrix(save_file); - + // return saved network return saved_network; } From 614cfef7f260f34be0db8a4e110b16ee96da9e7d Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Thu, 21 Sep 2023 10:19:22 +0200 Subject: [PATCH 3/4] random numbers are now somehow random --- matrix.c | 11 +++++++++-- neuronal_network.c | 3 +-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index be1a530..570eef0 100644 --- a/matrix.c +++ b/matrix.c @@ -2,8 +2,10 @@ #include #include #include +#include #define MAX_BYTES 100 +static int RANDOMIZED = 0; // operational functions Matrix* matrix_create(int rows, int columns) { @@ -17,7 +19,7 @@ Matrix* matrix_create(int rows, int columns) { // allocate memory for the numbers (2D-Array) matrix->numbers = malloc(sizeof(double*) * rows); for (int i = 0; i < rows; i++) { - matrix->numbers[i] = malloc(sizeof(double) * columns); + matrix->numbers[i] = calloc(sizeof(double), columns); } // return the pointer to the allocated memory @@ -189,7 +191,8 @@ Matrix* apply(double (*function)(double), Matrix* matrix) { // apply the function to all values in the matrix for (int i = 0; i < matrix->rows; i++) { for (int j = 0; j < matrix->columns; j++) { - matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]); + result_matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]); + int k = 0; } } @@ -350,6 +353,10 @@ int matrix_argmax(Matrix* matrix) { void matrix_randomize(Matrix* matrix, int n) { + if(!RANDOMIZED){ + srand(time(NULL)); + RANDOMIZED = 1; + } //make a min and max double min = -1.0f / sqrt(n); double max = 1.0f / sqrt(n); diff --git a/neuronal_network.c b/neuronal_network.c index 4682233..46fc953 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -159,11 +159,10 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) { //void batch_train_network(Neural_Network* network, Image** images, int size); double relu(double input) { - if (input < 0){ + if (input <= 0){ return 0.0; } return input; - //TODO: relu formel } Matrix* softmax(Matrix* matrix) { From f10814c56c9cb1085c36296d9b140199a008c6c3 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Thu, 21 Sep 2023 10:22:43 +0200 Subject: [PATCH 4/4] closing file --- neuronal_network.c | 1 + 1 file changed, 1 insertion(+) diff --git a/neuronal_network.c b/neuronal_network.c index f8b03f8..e3a939e 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -122,6 +122,7 @@ Neural_Network* load_network(char* file) { saved_network->weights_output = load_next_matrix(save_file); // return saved network + fclose(save_file); return saved_network; }