From f18a7714a2b14343d9f20d2528984702d86758e4 Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Thu, 21 Sep 2023 12:23:59 +0200 Subject: [PATCH 1/2] loss function and support functions --- matrix.c | 10 ++++++++++ matrix.h | 1 + neuronal_network.c | 25 +++++++++++++++++++++---- neuronal_network.h | 3 --- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/matrix.c b/matrix.c index da25e3e..c958c0f 100644 --- a/matrix.c +++ b/matrix.c @@ -249,6 +249,16 @@ Matrix* transpose(Matrix* matrix) { } +double matrix_sum(Matrix* matrix) { + double sum = 0; + for (int i = 0; i < matrix->rows; i++) { + for (int j = 0; j < matrix->columns; j++) { + sum += matrix->numbers[i][j]; + } + } + return sum; +} + void matrix_save(Matrix* matrix, char* file_string){ // open the file in append mode diff --git a/matrix.h b/matrix.h index 429d5bf..d14bfd8 100644 --- a/matrix.h +++ b/matrix.h @@ -37,6 +37,7 @@ Matrix* apply(double (*function)(double), Matrix* matrix); Matrix* scale(Matrix* matrix, double value); Matrix* addScalar(Matrix* matrix, double value); Matrix* transpose(Matrix* matrix); +double matrix_sum(Matrix* matrix); diff --git a/neuronal_network.c b/neuronal_network.c index 8b4d845..39dc591 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -4,6 +4,11 @@ #include #include +double relu(double input); +Matrix* softmax(Matrix* matrix); +double square(double input); +double loss_function(Matrix* output_matrix, int image_label); + 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 @@ -19,9 +24,7 @@ Neural_Network* new_network(int input_size, int hidden_size, int output_size, do network->bias_1 = matrix_create(hidden_size, 1); network->bias_2 = matrix_create(hidden_size, 1); network->bias_3 = matrix_create(hidden_size, 1); - network.bias_output = matrix_create(output_size, 1); - - + network->bias_output = matrix_create(output_size, 1); return network; } @@ -51,7 +54,6 @@ void free_network(Neural_Network* network){ free(network); } - void save_network(Neural_Network* network) { char* file_name = "../networks/newest_network.txt"; @@ -193,4 +195,19 @@ Matrix* softmax(Matrix* matrix) { } } return result_matrix; +} + +double square(double input) { + return input * input; +} + +double loss_function(Matrix* output_matrix, int image_label) { + Matrix* temp = matrix_copy(output_matrix); + + temp->numbers[1, image_label] -= 1; + apply(square, temp); + + matrix_free(temp); + + return matrix_sum(temp);; } \ No newline at end of file diff --git a/neuronal_network.h b/neuronal_network.h index 339ef66..19946c4 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -41,6 +41,3 @@ 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); - -double relu(double input); -Matrix* softmax(Matrix* matrix); From a6823bbeeb761e309294f57c979d4e7b0fe5ceb4 Mon Sep 17 00:00:00 2001 From: Tocuro Date: Thu, 21 Sep 2023 12:48:44 +0200 Subject: [PATCH 2/2] warum hab i ka highlighting mehr --- neuronal_network.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/neuronal_network.c b/neuronal_network.c index 39dc591..9af1d4e 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -170,6 +170,12 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) { return result; } +double cost_function(Matrix* calculated, int expected){ + calculated->numbers[expected] -= 1; + apply(square, calculated); + +} + //void train_network(Neural_Network* network, Matrix* input, Matrix* output); //void batch_train_network(Neural_Network* network, Image** images, int size); @@ -180,6 +186,10 @@ double relu(double input) { return input; } +double relu_derivative(double x) { + return (x > 0) ? 1 : 0; +} + Matrix* softmax(Matrix* matrix) { double total = 0;