From f13d3950b0abe34e9cbe25f6ecc9c6cd8e9de4fb Mon Sep 17 00:00:00 2001 From: jastornig Date: Sat, 23 Sep 2023 20:30:25 +0000 Subject: [PATCH 01/18] (fix) image loader windows --- image.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/image.c b/image.c index bc8417b..6818cf8 100644 --- a/image.c +++ b/image.c @@ -68,8 +68,8 @@ Image * load_pgm_image(char * image_file_string){ Image** import_images(char* image_file_string, char* label_file_string, int* _number_imported, int count) { printf("Loading Images\n"); // create file pointer for the image and label data - FILE* image_file = fopen(image_file_string, "r"); - FILE* label_file = fopen(label_file_string, "r"); + FILE* image_file = fopen(image_file_string, "rb"); + FILE* label_file = fopen(label_file_string, "rb"); // check if the file could be opened if(image_file == NULL || label_file == NULL) { From f836c53711e9e8bd42939d63153b21f90f5f1c50 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 24 Sep 2023 11:54:55 +0200 Subject: [PATCH 02/18] Clean up (before drastic refactoring) --- CMakeLists.txt | 2 +- image.c => image/image.c | 8 ++--- image.h => image/image.h | 4 +-- main.c | 14 ++++---- matrix.c => matrix/matrix.c | 10 ------ matrix.h => matrix/matrix.h | 13 ++----- matrix/operations.c | 1 + matrix/operations.h | 1 + neuronal_network.c | 71 +++++++++++++++++++++---------------- neuronal_network.h | 4 +-- 10 files changed, 61 insertions(+), 67 deletions(-) rename image.c => image/image.c (97%) rename image.h => image/image.h (93%) rename matrix.c => matrix/matrix.c (98%) rename matrix.h => matrix/matrix.h (67%) create mode 100644 matrix/operations.c create mode 100644 matrix/operations.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 461b2a6..5816779 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(c_net C) set(CMAKE_C_STANDARD 11) -add_executable(c_net main.c matrix.c image.c neuronal_network.c util.c util.h) +add_executable(c_net main.c matrix/matrix.c image/image.c neuronal_network.c util.c matrix/operations.c) target_link_libraries(c_net m) diff --git a/image.c b/image/image.c similarity index 97% rename from image.c rename to image/image.c index bc8417b..fb1cdfc 100644 --- a/image.c +++ b/image/image.c @@ -2,8 +2,8 @@ #include #include "image.h" -#include "matrix.h" -#include "util.h" +#include "../matrix/matrix.h" +#include "../util.h" void big_endian_to_c_uint(const char * bytes, void * target, int size) { char* helper = (char*)target; @@ -68,8 +68,8 @@ Image * load_pgm_image(char * image_file_string){ Image** import_images(char* image_file_string, char* label_file_string, int* _number_imported, int count) { printf("Loading Images\n"); // create file pointer for the image and label data - FILE* image_file = fopen(image_file_string, "r"); - FILE* label_file = fopen(label_file_string, "r"); + FILE* image_file = fopen(image_file_string, "rb"); + FILE* label_file = fopen(label_file_string, "rb"); // check if the file could be opened if(image_file == NULL || label_file == NULL) { diff --git a/image.h b/image/image.h similarity index 93% rename from image.h rename to image/image.h index 6d3caeb..5baa9dd 100644 --- a/image.h +++ b/image/image.h @@ -1,7 +1,7 @@ #pragma once -#include "matrix.h" +#include "../matrix/matrix.h" -#include "matrix.h" +#include "../matrix/matrix.h" typedef struct { Matrix* pixel_values; diff --git a/main.c b/main.c index 9b1a5d5..98195fd 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ #include -#include "image.h" +#include "image/image.h" #include "neuronal_network.h" int main() { @@ -11,18 +11,18 @@ int main() { // matrix_print(images[0]->pixel_values); // matrix_print(images[1]->pixel_values); - Neural_Network* nn = new_network(28*28, 40, 5, 10, 0.08); + Neural_Network* nn = new_network(28*28, 50, 3, 10, 0.1); randomize_network(nn, 1); // Neural_Network* nn = load_network("../networks/newest_network.txt"); -// printf("Done loading!\n"); -// batch_train(nn, images, 20000, 20); - - for (int i = 0; i < 30000; ++i) { + for (int i = 0; i < 60000; ++i) { train_network(nn, images[i], images[i]->label); } - save_network(nn); +// batch_train(nn, images, 30000, 2); + printf("Trinaing Done!\n"); + +// save_network(nn); printf("%lf\n", measure_network_accuracy(nn, images, 10000)); diff --git a/matrix.c b/matrix/matrix.c similarity index 98% rename from matrix.c rename to matrix/matrix.c index ba7c258..84e07e9 100644 --- a/matrix.c +++ b/matrix/matrix.c @@ -249,16 +249,6 @@ 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/matrix.h similarity index 67% rename from matrix.h rename to matrix/matrix.h index 2e0e5a9..022a8b8 100644 --- a/matrix.h +++ b/matrix/matrix.h @@ -8,7 +8,6 @@ typedef struct { static const int scaling_value = 10000; -// operational functions Matrix* matrix_create(int rows, int columns); void matrix_fill(Matrix* matrix, double value); void matrix_free(Matrix* matrix); @@ -18,18 +17,11 @@ void matrix_save(Matrix* matrix, char* file_string); Matrix* matrix_load(char* file_string); Matrix* load_next_matrix(FILE * save_file); -void matrix_randomize(Matrix* matrix, int n); // don't understand the usage of the n +void matrix_randomize(Matrix* matrix, int n); int matrix_argmax(Matrix* matrix); Matrix* matrix_flatten(Matrix* matrix, int axis); Matrix* matrix_add_bias(Matrix* matrix); -/* - * These methods won't change or free the input matrix. - * It creates a new matrix, which is modified and then returned. - * If we don't need the original matrix, we should consider just changing the original matrix and changing the method signature to void. - */ - -// mathematical functions Matrix* multiply(Matrix* matrix1, Matrix* matrix2); Matrix* add(Matrix* matrix1, Matrix* matrix2); Matrix* subtract(Matrix* matrix1, Matrix* matrix2); @@ -37,5 +29,4 @@ Matrix* dot(Matrix* matrix1, Matrix* matrix2); 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); \ No newline at end of file +Matrix* transpose(Matrix* matrix); \ No newline at end of file diff --git a/matrix/operations.c b/matrix/operations.c new file mode 100644 index 0000000..59147e1 --- /dev/null +++ b/matrix/operations.c @@ -0,0 +1 @@ +#include "operations.h" diff --git a/matrix/operations.h b/matrix/operations.h new file mode 100644 index 0000000..0d415b5 --- /dev/null +++ b/matrix/operations.h @@ -0,0 +1 @@ +#include "matrix.h" \ No newline at end of file diff --git a/neuronal_network.c b/neuronal_network.c index 1c58018..c1d5468 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -6,8 +6,8 @@ double sigmoid(double input); Matrix* predict(Neural_Network* network, Matrix* image_data); Matrix* sigmoid_derivative(Matrix* matrix); -Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix, double learning_rate); -void apply_weights(Neural_Network* network, Matrix* delta_weights_matrix, int index); +Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix); +void apply_weights(Neural_Network *network, Matrix *delta_weights_matrix, int index, double learning_rate); Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output); Neural_Network* new_network(int input_size, int hidden_size, int hidden_amount, int output_size, double learning_rate){ @@ -167,22 +167,26 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) { //void batch_train(Neural_Network* network, Image** images, int amount, int batch_size) { // -// for (int i = 0; i < amount; ++i) { +// if(amount % batch_size != 0) { +// printf("ERROR: Batch Size is not compatible with image amount! (batch_train)"); +// exit(1); +// } // -// if(amount % 1000 == 0) { -// printf("1k pics!\n"); -// } +// int image_index = 0; +// +// for (int i = 0; i < amount / batch_size; ++i) { // // Matrix* batch_weights[network->hidden_amount + 1]; // +// for (int j = 0; j < network->hidden_amount + 1; j++) { +// batch_weights[j] = matrix_create(network->weights[j]->rows, network->weights[j]->columns); +// matrix_fill(batch_weights[j], 0); +// } +// // for (int j = 0; j < batch_size; ++j) { -// Matrix** delta_weights = train_network(network, images[i], images[i]->label); +// Matrix** delta_weights = train_network(network, images[image_index], images[image_index]->label); // // for (int k = 0; k < network->hidden_amount + 1; k++) { -// if(j == 0) { -// batch_weights[k] = delta_weights[k]; -// continue; -// } // // Matrix* temp_result = add(batch_weights[k], delta_weights[k]); // @@ -193,14 +197,16 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) { // } // // free(delta_weights); +// +// image_index++; // } // -// for (int j = 0; j < network->hidden_amount + 1; ++j) { +// for (int j = 0; j < network->hidden_amount + 1; j++) { // Matrix* average_delta_weight = scale(batch_weights[j], (1.0 / batch_size)); -// apply_weights(network, average_delta_weight, j); +// apply_weights(network, average_delta_weight, j, network->learning_rate); // -// matrix_free(average_delta_weight); // matrix_free(batch_weights[j]); +// matrix_free(average_delta_weight); // } // } //} @@ -239,13 +245,13 @@ void train_network(Neural_Network* network, Image *image, int label) { Matrix* delta = multiply(sigmoid_prime, error); //calculate and apply the delta for all weights in out-put layer - delta_weights[network->hidden_amount] = calculate_weights_delta(output[network->hidden_amount - 1], delta, network->learning_rate); + delta_weights[network->hidden_amount] = calculate_weights_delta(output[network->hidden_amount - 1], delta); //hidden layers Matrix* previous_delta = delta; for (int i = network->hidden_amount; i > 1; i--) { delta = calculate_delta_hidden(previous_delta, network->weights[i], output[i - 1]); - delta_weights[i - 1] = calculate_weights_delta(output[i - 2], delta, network->learning_rate); + delta_weights[i - 1] = calculate_weights_delta(output[i - 2], delta); matrix_free(previous_delta); previous_delta = delta; @@ -253,10 +259,16 @@ void train_network(Neural_Network* network, Image *image, int label) { // Input Layer delta = calculate_delta_hidden(previous_delta, network->weights[1], output[0]); - delta_weights[0] = calculate_weights_delta(image_data, delta, network->learning_rate); + delta_weights[0] = calculate_weights_delta(image_data, delta); + + + // if you want to use this method as a standalone method this part needs to be uncommented + for (int i = 0; i < network->hidden_amount + 1; ++i) { + apply_weights(network, delta_weights[i], i, network->learning_rate); + } for (int i = 0; i < network->hidden_amount + 1; ++i) { - apply_weights(network, delta_weights[i], i); + matrix_free(delta_weights[i]); } // De-allocate stuff @@ -267,9 +279,7 @@ void train_network(Neural_Network* network, Image *image, int label) { matrix_free(output[i]); } - for (int i = 0; i < network->hidden_amount + 1; ++i) { - matrix_free(delta_weights[i]); - } + matrix_free(sigmoid_prime); matrix_free(wanted_output); @@ -308,7 +318,7 @@ Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix return new_deltas; } -void apply_weights(Neural_Network* network, Matrix* delta_weights_matrix, int index) { +void apply_weights(Neural_Network *network, Matrix *delta_weights_matrix, int index, double learning_rate) { if(index > network->hidden_amount || index < 0) { printf("ERROR: Index out of range! (apply_weights)"); @@ -320,27 +330,28 @@ void apply_weights(Neural_Network* network, Matrix* delta_weights_matrix, int in exit(1); } + // scale by learning rate + Matrix* scaled_delta_weights_matrix = scale(delta_weights_matrix, learning_rate); + for (int i = 0; i < delta_weights_matrix->rows; i++) { - for (int j = 0; j < delta_weights_matrix->columns; j++) { - network->weights[index]->numbers[i][j] += delta_weights_matrix->numbers[i][j]; // multiply delta_weights_matrix with learning rate AND - instead of + because soll-ist + for (int j = 0; j < scaled_delta_weights_matrix->columns; j++) { + network->weights[index]->numbers[i][j] += scaled_delta_weights_matrix->numbers[i][j]; // multiply delta_weights_matrix with learning rate AND - instead of + because soll-ist } } + + matrix_free(scaled_delta_weights_matrix); } -Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix, double learning_rate) { +Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix) { Matrix* previous_out_with_one = matrix_add_bias(previous_layer_output); Matrix* transposed_previous_out_with_bias = transpose(previous_out_with_one); Matrix* weights_delta_matrix = dot(delta_matrix, transposed_previous_out_with_bias); - // scale by learning rate - Matrix* result = scale(weights_delta_matrix, learning_rate); - matrix_free(previous_out_with_one); matrix_free(transposed_previous_out_with_bias); - matrix_free(weights_delta_matrix); - return result; + return weights_delta_matrix; } Matrix* sigmoid_derivative(Matrix* matrix) { diff --git a/neuronal_network.h b/neuronal_network.h index a2db05f..80a48a2 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -1,6 +1,6 @@ -#include "matrix.h" -#include "image.h" +#include "matrix/matrix.h" +#include "image/image.h" typedef struct { int input_size; From cf8b0a8b949edc00b929271c96170bf11a558690 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 24 Sep 2023 12:22:28 +0200 Subject: [PATCH 03/18] Clean up (1) --- image/image.c | 3 +- image/image.h | 15 +-- main.c | 61 ++++++++--- matrix/matrix.c | 255 +------------------------------------------- matrix/matrix.h | 19 +--- matrix/operations.c | 232 ++++++++++++++++++++++++++++++++++++++++ matrix/operations.h | 26 ++++- neuronal_network.c | 3 +- 8 files changed, 312 insertions(+), 302 deletions(-) diff --git a/image/image.c b/image/image.c index fb1cdfc..419620d 100644 --- a/image/image.c +++ b/image/image.c @@ -159,7 +159,6 @@ Image** import_images(char* image_file_string, char* label_file_string, int* _nu } void img_print (Image* img) { - //print the image matrix_print(img->pixel_values); //print the number of the image @@ -183,7 +182,7 @@ void img_free (Image* img) { free(img); } -void images_free (Image** images, int quantity){ +void images_free(Image** images, int quantity) { //frees every single image for(int i=0;ipixel_values); -// matrix_print(images[1]->pixel_values); + /* + * Create a new network and randomize the weights + */ - Neural_Network* nn = new_network(28*28, 50, 3, 10, 0.1); - randomize_network(nn, 1); -// Neural_Network* nn = load_network("../networks/newest_network.txt"); + Neural_Network* network = new_network(input_size, hidden_layer_size, hidden_layer_count, 10, learning_rate); + randomize_network(network, 1); - for (int i = 0; i < 60000; ++i) { - train_network(nn, images[i], images[i]->label); + /* + * Training + */ + + for (int i = 0; i < amount_of_images_used_to_train; i++) { + train_network(network, images[i], images[i]->label); } -// batch_train(nn, images, 30000, 2); + // Batch training works if you change the train_network method, but the results are not that good (needs further testing) + // batch_train(nn, images, 30000, 2); + printf("Trinaing Done!\n"); -// save_network(nn); + /* + * Saving and Loading + */ - printf("%lf\n", measure_network_accuracy(nn, images, 10000)); +// save_network(network); +// Neural_Network* network = load_network("../networks/newest_network.txt"); -} \ No newline at end of file + /* + * Measure Accuracy & predict single images + */ + + printf("Accuracy: %lf\n", measure_network_accuracy(network, images, amount_of_images_used_to_test)); + +// matrix_print(predict_image(network, images[0])); + + images_free(images, amount_of_images_to_load); + free_network(network); + + return 0; +} diff --git a/matrix/matrix.c b/matrix/matrix.c index 84e07e9..bec2425 100644 --- a/matrix/matrix.c +++ b/matrix/matrix.c @@ -1,12 +1,9 @@ #include "matrix.h" #include #include -#include -#include + #define MAX_BYTES 100 -static int RANDOMIZED = 0; -// operational functions Matrix* matrix_create(int rows, int columns) { // allocate memory for the matrix @@ -80,175 +77,6 @@ Matrix* matrix_copy(Matrix *matrix) { return copy_of_matrix; } -// mathematical functions - -/* - * These methods won't change or free the input matrix. - * It creates a new matrix, which is modified and then returned. - * If we don't need the original matrix, we should consider just changing the original matrix and changing the method signature to void. - */ - -Matrix* multiply(Matrix* matrix1, Matrix* matrix2) { - - // check if the two matrices are of the same size - if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) { - printf("ERROR: Size of matrices are not compatible! (Multiply)"); - exit(1); - } - - // create result matrix - Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns); - - // multiply the values and save them into the result matrix - for (int i = 0; i < matrix1->rows; i++) { - for (int j = 0; j < matrix1->columns; j++) { - result_matrix->numbers[i][j] = matrix1->numbers[i][j] * matrix2->numbers[i][j]; - } - } - - // return resulting matrix - return result_matrix; -} - -Matrix* add(Matrix* matrix1, Matrix* matrix2) { - - // check if the two matrices are of the same size - if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) { - printf("ERROR: Size of matrices are not compatible! (Add)"); - exit(1); - } - - // create result matrix - Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns); - - // add the value of the number in matrix 1 to the value of the number in matrix 2 - for (int i = 0; i < matrix1->rows; i++) { - for (int j = 0; j < matrix1->columns; j++) { - result_matrix->numbers[i][j] = matrix1->numbers[i][j] + matrix2->numbers[i][j]; - } - } - - // return the result matrix - return result_matrix; -} - -Matrix* subtract(Matrix* matrix1, Matrix* matrix2) { - - // check if the two matrices are of the same size - if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) { - printf("ERROR: Size of matrices are not compatible! (Subtract)"); - exit(1); - } - - // create result matrix - Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns); - - // subtract the value of the number in matrix 2 from the value of the number in matrix 1 - for (int i = 0; i < matrix1->rows; i++) { - for (int j = 0; j < matrix1->columns; j++) { - result_matrix->numbers[i][j] = matrix1->numbers[i][j] - matrix2->numbers[i][j]; - } - } - - // return the resulting matrix - return result_matrix; -} - -Matrix* dot(Matrix* matrix1, Matrix* matrix2) { - - // check if the dimensions of the matrices are compatible to calculate the dot product - if(matrix1->columns != matrix2->rows) { - printf("ERROR: Size of matrices are not compatible! (Dot-Product)"); - exit(1); - } - - // create a new matrix with the dimensions of the dot product; - Matrix* result_matrix = matrix_create(matrix1->rows, matrix2->columns); - - // iterate through all rows of matrix 1 - for (int i = 0; i < matrix1->rows; i++) { - - // iterate though all columns of matrix 2 - for (int j = 0; j < matrix2->columns; j++) { - - // sum up the products and save them into the result matrix - result_matrix->numbers[i][j] = 0; - for (int k = 0; k < matrix2->rows; k++) { - result_matrix->numbers[i][j] += matrix1->numbers[i][k] * matrix2->numbers[k][j]; - } - } - } - - // return result - return result_matrix; -} - -Matrix* apply(double (*function)(double), Matrix* matrix) { - - // create a new matrix used to calculate the result - Matrix* result_matrix = matrix_create(matrix->rows, matrix->columns); - - // 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++) { - result_matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]); - int k = 0; - } - } - - // return resulting matrix - return result_matrix; -} - -Matrix* scale(Matrix* matrix, double value) { - - // create a copy of the original matrix - Matrix* result_matrix = matrix_copy(matrix); - - // iterate over all numbers in the matrix and multiply by the scalar value - for (int i = 0; i < result_matrix->rows; i++) { - for (int j = 0; j < result_matrix->columns; j++) { - result_matrix->numbers[i][j] *= value; - } - } - - // return the copy - return result_matrix; -} - -Matrix* addScalar(Matrix* matrix, double value) { - - // create a copy of the original matrix - Matrix* result_matrix = matrix_copy(matrix); - - // iterate over all numbers in the matrix and add the scalar value - for (int i = 0; i < result_matrix->rows; i++) { - for (int j = 0; j < result_matrix->columns; j++) { - result_matrix->numbers[i][j] += value; - } - } - - // return the copy - return result_matrix; -} - -Matrix* transpose(Matrix* matrix) { - - // create a new matrix of the size n-m, based on the original matrix of size m-n - Matrix* result_matrix = matrix_create(matrix->columns, matrix->rows); - - // copy the values from the original into the correct place in the copy - for (int i = 0; i < matrix->rows; i++) { - for (int j = 0; j < matrix->columns; j++) { - result_matrix->numbers[j][i] = matrix->numbers[i][j]; - } - } - - // return the result matrix - return result_matrix; - -} - void matrix_save(Matrix* matrix, char* file_string){ // open the file in append mode @@ -308,85 +136,4 @@ Matrix* load_next_matrix(FILE *save_file){ } } return matrix; -} - -Matrix* matrix_flatten(Matrix* matrix, int axis) { - // Axis = 0 -> Column Vector, Axis = 1 -> Row Vector - Matrix* result_matrix; - // Column Vector - if (axis == 0) { - result_matrix = matrix_create(matrix -> rows * matrix -> columns, 1); - } - // Row Vector - else if (axis == 1) { - result_matrix = matrix_create(1, matrix -> rows * matrix -> columns); - } else { - printf("ERROR: Argument must be 1 or 0 (matrix_flatten)"); - exit(EXIT_FAILURE); - } - for (int i = 0; i < matrix->rows; i++) { - for (int j = 0; j < matrix->columns; j++) { - if (axis == 0) result_matrix->numbers[i * matrix->columns + j][0] = matrix->numbers[i][j]; - else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; - } - } - return result_matrix; -} - -int matrix_argmax(Matrix* matrix) { - // Expects a Mx1 matrix - if (matrix->columns != 1){ - printf("ERROR: Matrix is not Mx1 (matrix_argmax)"); - exit(EXIT_FAILURE); - } - - double max_value = 0; - int max_index = 0; - - for (int i = 0; i < matrix->rows; i++) { - if (matrix->numbers[i][0] > max_value) { - max_value = matrix->numbers[i][0]; - max_index = i; - } - } - return max_index; -} - -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); - - //calculate difference - double difference = max - min; - - //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] = min + (1.0 * (rand() % scaled_difference) / scaling_value); - } - } -} - -Matrix* matrix_add_bias(Matrix* matrix) { - if(matrix->columns != 1) { - printf("ERROR: The size of the matrix does not match an input matrix! (matrix_add_bias)"); - exit(1); - } - - Matrix* result = matrix_create(matrix->rows + 1, matrix->columns); - - result->numbers[0][0] = 1.0; - for (int i = 0; i < matrix->rows; ++i) { - result->numbers[i + 1][0] = matrix->numbers[i][0]; - } - - return result; } \ No newline at end of file diff --git a/matrix/matrix.h b/matrix/matrix.h index 022a8b8..ea37f59 100644 --- a/matrix/matrix.h +++ b/matrix/matrix.h @@ -1,4 +1,3 @@ -#pragma once #include typedef struct { @@ -6,8 +5,6 @@ typedef struct { double **numbers; } Matrix; -static const int scaling_value = 10000; - Matrix* matrix_create(int rows, int columns); void matrix_fill(Matrix* matrix, double value); void matrix_free(Matrix* matrix); @@ -15,18 +12,4 @@ 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 * save_file); - -void matrix_randomize(Matrix* matrix, int n); -int matrix_argmax(Matrix* matrix); -Matrix* matrix_flatten(Matrix* matrix, int axis); -Matrix* matrix_add_bias(Matrix* matrix); - -Matrix* multiply(Matrix* matrix1, Matrix* matrix2); -Matrix* add(Matrix* matrix1, Matrix* matrix2); -Matrix* subtract(Matrix* matrix1, Matrix* matrix2); -Matrix* dot(Matrix* matrix1, Matrix* matrix2); -Matrix* apply(double (*function)(double), Matrix* matrix); -Matrix* scale(Matrix* matrix, double value); -Matrix* addScalar(Matrix* matrix, double value); -Matrix* transpose(Matrix* matrix); \ No newline at end of file +Matrix* load_next_matrix(FILE * save_file); \ No newline at end of file diff --git a/matrix/operations.c b/matrix/operations.c index 59147e1..d969e41 100644 --- a/matrix/operations.c +++ b/matrix/operations.c @@ -1 +1,233 @@ +#include +#include +#include +#include "math.h" #include "operations.h" + +static int RANDOMIZED = 0; + +Matrix* multiply(Matrix* matrix1, Matrix* matrix2) { + + // check if the two matrices are of the same size + if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) { + printf("ERROR: Size of matrices are not compatible! (Multiply)"); + exit(1); + } + + // create result matrix + Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns); + + // multiply the values and save them into the result matrix + for (int i = 0; i < matrix1->rows; i++) { + for (int j = 0; j < matrix1->columns; j++) { + result_matrix->numbers[i][j] = matrix1->numbers[i][j] * matrix2->numbers[i][j]; + } + } + + // return resulting matrix + return result_matrix; +} + +Matrix* add(Matrix* matrix1, Matrix* matrix2) { + + // check if the two matrices are of the same size + if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) { + printf("ERROR: Size of matrices are not compatible! (Add)"); + exit(1); + } + + // create result matrix + Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns); + + // add the value of the number in matrix 1 to the value of the number in matrix 2 + for (int i = 0; i < matrix1->rows; i++) { + for (int j = 0; j < matrix1->columns; j++) { + result_matrix->numbers[i][j] = matrix1->numbers[i][j] + matrix2->numbers[i][j]; + } + } + + // return the result matrix + return result_matrix; +} + +Matrix* subtract(Matrix* matrix1, Matrix* matrix2) { + + // check if the two matrices are of the same size + if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) { + printf("ERROR: Size of matrices are not compatible! (Subtract)"); + exit(1); + } + + // create result matrix + Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns); + + // subtract the value of the number in matrix 2 from the value of the number in matrix 1 + for (int i = 0; i < matrix1->rows; i++) { + for (int j = 0; j < matrix1->columns; j++) { + result_matrix->numbers[i][j] = matrix1->numbers[i][j] - matrix2->numbers[i][j]; + } + } + + // return the resulting matrix + return result_matrix; +} + +Matrix* dot(Matrix* matrix1, Matrix* matrix2) { + + // check if the dimensions of the matrices are compatible to calculate the dot product + if(matrix1->columns != matrix2->rows) { + printf("ERROR: Size of matrices are not compatible! (Dot-Product)"); + exit(1); + } + + // create a new matrix with the dimensions of the dot product; + Matrix* result_matrix = matrix_create(matrix1->rows, matrix2->columns); + + // iterate through all rows of matrix 1 + for (int i = 0; i < matrix1->rows; i++) { + + // iterate though all columns of matrix 2 + for (int j = 0; j < matrix2->columns; j++) { + + // sum up the products and save them into the result matrix + result_matrix->numbers[i][j] = 0; + for (int k = 0; k < matrix2->rows; k++) { + result_matrix->numbers[i][j] += matrix1->numbers[i][k] * matrix2->numbers[k][j]; + } + } + } + + // return result + return result_matrix; +} + +Matrix* apply(double (*function)(double), Matrix* matrix) { + + // create a new matrix used to calculate the result + Matrix* result_matrix = matrix_create(matrix->rows, matrix->columns); + + // 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++) { + result_matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]); + int k = 0; + } + } + + // return resulting matrix + return result_matrix; +} + +Matrix* scale(Matrix* matrix, double value) { + + // create a copy of the original matrix + Matrix* result_matrix = matrix_copy(matrix); + + // iterate over all numbers in the matrix and multiply by the scalar value + for (int i = 0; i < result_matrix->rows; i++) { + for (int j = 0; j < result_matrix->columns; j++) { + result_matrix->numbers[i][j] *= value; + } + } + + // return the copy + return result_matrix; +} + +Matrix* transpose(Matrix* matrix) { + + // create a new matrix of the size n-m, based on the original matrix of size m-n + Matrix* result_matrix = matrix_create(matrix->columns, matrix->rows); + + // copy the values from the original into the correct place in the copy + for (int i = 0; i < matrix->rows; i++) { + for (int j = 0; j < matrix->columns; j++) { + result_matrix->numbers[j][i] = matrix->numbers[i][j]; + } + } + + // return the result matrix + return result_matrix; + +} + +Matrix* matrix_flatten(Matrix* matrix, int axis) { + // Axis = 0 -> Column Vector, Axis = 1 -> Row Vector + Matrix* result_matrix; + // Column Vector + if (axis == 0) { + result_matrix = matrix_create(matrix -> rows * matrix -> columns, 1); + } + // Row Vector + else if (axis == 1) { + result_matrix = matrix_create(1, matrix -> rows * matrix -> columns); + } else { + printf("ERROR: Argument must be 1 or 0 (matrix_flatten)"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < matrix->rows; i++) { + for (int j = 0; j < matrix->columns; j++) { + if (axis == 0) result_matrix->numbers[i * matrix->columns + j][0] = matrix->numbers[i][j]; + else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; + } + } + return result_matrix; +} + +int argmax(Matrix* matrix) { + // Expects a Mx1 matrix + if (matrix->columns != 1){ + printf("ERROR: Matrix is not Mx1 (argmax)"); + exit(EXIT_FAILURE); + } + + double max_value = 0; + int max_index = 0; + + for (int i = 0; i < matrix->rows; i++) { + if (matrix->numbers[i][0] > max_value) { + max_value = matrix->numbers[i][0]; + max_index = i; + } + } + return max_index; +} + +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); + + //calculate difference + double difference = max - min; + + //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] = min + (1.0 * (rand() % scaled_difference) / scaling_value); + } + } +} + +Matrix* matrix_add_bias(Matrix* matrix) { + if(matrix->columns != 1) { + printf("ERROR: The size of the matrix does not match an input matrix! (matrix_add_bias)"); + exit(1); + } + + Matrix* result = matrix_create(matrix->rows + 1, matrix->columns); + + result->numbers[0][0] = 1.0; + for (int i = 0; i < matrix->rows; ++i) { + result->numbers[i + 1][0] = matrix->numbers[i][0]; + } + + return result; +} \ No newline at end of file diff --git a/matrix/operations.h b/matrix/operations.h index 0d415b5..e4f0220 100644 --- a/matrix/operations.h +++ b/matrix/operations.h @@ -1 +1,25 @@ -#include "matrix.h" \ No newline at end of file +#include "matrix.h" + +static const int scaling_value = 10000; + +Matrix* multiply(Matrix* matrix1, Matrix* matrix2); + +Matrix* add(Matrix* matrix1, Matrix* matrix2); //only used in the batch_training method + +Matrix* subtract(Matrix* matrix1, Matrix* matrix2); + +Matrix* dot(Matrix* matrix1, Matrix* matrix2); + +Matrix* apply(double (*function)(double), Matrix* matrix); + +Matrix* scale(Matrix* matrix, double value); + +Matrix* transpose(Matrix* matrix); + +Matrix* matrix_flatten(Matrix* matrix, int axis); + +int argmax(Matrix* matrix); + +void matrix_randomize(Matrix* matrix, int n); + +Matrix* matrix_add_bias(Matrix* matrix); \ No newline at end of file diff --git a/neuronal_network.c b/neuronal_network.c index c1d5468..650633c 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -1,5 +1,6 @@ #include #include "neuronal_network.h" +#include "matrix\operations.h" #include #include @@ -120,7 +121,7 @@ double measure_network_accuracy(Neural_Network* network, Image** images, int amo for (int i = 0; i < amount; i++) { Matrix* prediction = predict_image(network, images[i]); - int guess = matrix_argmax(prediction); + int guess = argmax(prediction); int answer = (unsigned char) images[i]->label; if (guess == answer) { From 2a4fbf9bbd42d02ab491826ba27063ee5d35b469 Mon Sep 17 00:00:00 2001 From: Raphael Walcher Date: Sun, 24 Sep 2023 20:41:12 +0200 Subject: [PATCH 04/18] readme --- README.md | 100 +++++++++++------------------------------------------- 1 file changed, 19 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index 3fcc78b..72ca9a6 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,30 @@ # C-net ඞ - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://git-ainf.aau.at/jastornig/c-net.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://git-ainf.aau.at/jastornig/c-net/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - ## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. +C-net ඞ is a Python project designed to read and predict numbers from the MNIST dataset using neural networks. ## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. +![Insert GIF or Screenshot here](link_to_visual.gif) ## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. -## Contributing -State if you are open to contributions and what your requirements are for accepting them. +- [x] Implemented an Image Loader for MNIST dataset. +- [x] Created a prediction function for recognizing handwritten digits. +- [x] Developed matrix calculation methods to support neural network operations. +- [x] Added functionality to load and save neural network models. +- [x] Successfully trained the network on MNIST images. +- [x] Achieved an accuracy rate with a confidence level above 90%. +- [ ] Ongoing optimization and code refinement. -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. +## Authors and Acknowledgments +This project was brought to you by the following contributors: +- Stornig, Jakob +- Schleicher, Thomas +- Dworski, Daniel +- Walcher, Raphael -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. +We would like to express our gratitude to the following project, which served as an inspiration and reference: +- [MNIST from Scratch](https://github.com/markkraay/mnist-from-scratch) by markkraay -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +## Project Status +The project is considered finished, but ongoing optimizations and improvements may still be in progress. From b9dba1a92bd05bf1afcb483c8c895343221534de Mon Sep 17 00:00:00 2001 From: Raphael Walcher Date: Sun, 24 Sep 2023 20:51:27 +0200 Subject: [PATCH 05/18] readme --- README.md | 104 ++++++++++++------------------------------------------ 1 file changed, 23 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index 3fcc78b..f9adb41 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,34 @@ # C-net ඞ - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://git-ainf.aau.at/jastornig/c-net.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://git-ainf.aau.at/jastornig/c-net/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - ## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. +C-net ඞ is a Python project designed to read and predict numbers from the MNIST dataset using neural networks. ## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. +![Insert GIF or Screenshot here](link_to_visual.gif) ## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. -## Contributing -State if you are open to contributions and what your requirements are for accepting them. +- [x] Implemented an Image Loader for MNIST dataset. +- [x] Created a prediction function for recognizing handwritten digits. +- [x] Developed matrix calculation methods to support neural network operations. +- [x] Added functionality to load and save neural network models. +- [x] Successfully trained the network on MNIST images. +- [x] Achieved an accuracy rate with a confidence level above 90%. +- [ ] Ongoing optimization and code refinement. -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. +## Authors and Acknowledgments +This project was brought to you by the following contributors: +- Stornig, Jakob +- Schleicher, Thomas +- Dworski, Daniel +- Walcher, Raphael -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. +We would like to express our gratitude to the following project, which served as an inspiration and reference: +- [MNIST from Scratch](https://github.com/markkraay/mnist-from-scratch) by markkraay +- [Neural Network Framework in C](https://medium.com/analytics-vidhya/building-neural-network-framework-in-c-using-backpropagation-8ad589a0752d) +- [Simple Neural Network Implementation in C](https://towardsdatascience.com/simple-neural-network-implementation-in-c-663f51447547) +- [3Blue1Brown Neural Network Series](https://www.youtube.com/watch?v=aircAruvnKk&list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi) +- [Brotcrunsher's YouTube Videos](https://www.youtube.com/watch?v=oCPT87SvkPM&pp=ygUbYnJvdCBjcnVzaGVyIG5ldXJhbCBuZXp3ZXJr), [Video 2](https://www.youtube.com/watch?v=YIqYBxpv53A&pp=ygUbYnJvdCBjcnVzaGVyIG5ldXJhbCBuZXp3ZXJr), [Video 3](https://youtu.be/EAtQCut6Qno) -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +## Project Status +The project is considered finished, but ongoing optimizations and improvements may still be in progress. From e9881ed2b3b8a8d4e14ee0d63838c538c14a243d Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 20:56:44 +0200 Subject: [PATCH 06/18] !(feat) cli implemented breaking changes. save_network in neuronal_network.c now takes a second parameter file_name --- main.c | 124 +++++++++++++++++++++++++++++++++++++++------ neuronal_network.c | 10 ++-- neuronal_network.h | 2 +- 3 files changed, 116 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index 9b1a5d5..b2f7302 100644 --- a/main.c +++ b/main.c @@ -2,28 +2,122 @@ #include "image.h" #include "neuronal_network.h" +#include +#include +#include +#include "util.h" -int main() { - Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 60000); -// img_visualize(images[0]); -// img_visualize(images[1]); +void parsingErrorPrintHelp(){ + printf("Syntax: c_net [train | detect]\n"); + printf("commands:\n"); + printf("train\t train the network\n"); + printf("predict\t load a pgm image and predict_demo the number\n"); + exit(1); +} -// matrix_print(images[0]->pixel_values); -// matrix_print(images[1]->pixel_values); +void parsingErrorTrain(){ + printf("invalid syntax\n"); + printf("Syntax: c_net train [path_to_train-images.idx3-ubyte] [path_to_train-labels.idx1-ubyte] [hidden_layer_count] [neurons_per_layer] [epochs] [learning_rate] [path_to_save_network]\n"); + exit(1); +} - Neural_Network* nn = new_network(28*28, 40, 5, 10, 0.08); - randomize_network(nn, 1); -// Neural_Network* nn = load_network("../networks/newest_network.txt"); -// printf("Done loading!\n"); +void parsingErrorDetect(){ + printf("invalid syntax\n"); + printf("Syntax: c_net predict_demo [path_to_network] [image_file]"); +} -// batch_train(nn, images, 20000, 20); +void predict_demo(int argc, char** arguments){ + if(argc != 2) parsingErrorDetect(); + char * network_file = arguments[0]; + char * image_file = arguments[1]; - for (int i = 0; i < 30000; ++i) { - train_network(nn, images[i], images[i]->label); + Neural_Network * nn = load_network(network_file); + Image * image = load_pgm_image(image_file); + Matrix * result = predict_image(nn, image); + int predicted = matrix_argmax(result); + printf("prediction result %d\n", predicted); + matrix_print(result); + matrix_free(result); +} + +void train(int argc, char** arguments) { + if (argc != 7) parsingErrorTrain(); + char *image_file = arguments[0]; + char *label_file = arguments[1]; + int hidden_count = (int) strtol(arguments[2], NULL, 10); + int neurons_per_layer = (int) strtol(arguments[3], NULL, 10); + int epochs = (int) strtol(arguments[4], NULL, 10); + if (errno != 0) { + printf("hidden_count, neurons_per_layer or epochs could not be parsed!\n"); + exit(1); } + double learning_rate = strtod(arguments[5], NULL); + if (errno != 0) { + printf("learning_rate could not be parsed!\n"); + exit(1); + } + char *save_path = arguments[6]; + int imported = 0; + Image **images = import_images(image_file, label_file, &imported, 50000); - save_network(nn); +// for(int i = 0; i < imported; i++){ +// matrix_save(images[i]->pixel_values, "images.txt"); +// } +// exit(1); - printf("%lf\n", measure_network_accuracy(nn, images, 10000)); + Neural_Network *nn = new_network(28 * 28, neurons_per_layer, hidden_count, 10, learning_rate); + randomize_network(nn, 1); + printf("training_network\n"); + for(int epoch = 1; epoch <= epochs; epoch++){ + printf("epoch %d\n", epoch); + for (int i = 0; i < imported; i++) { + if (i % 1000 == 0) { + updateBar(i * 100 / imported); + } + train_network(nn, images[i], images[i]->label); + } + updateBar(100); + printf("\n"); + printf("accuracy %lf\n", measure_network_accuracy(nn, images, 10000)); + } + printf("done training!\n"); + save_network(nn, save_path); +} + +int main(int argc, char** argv) { +// Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 60000); +//// img_visualize(images[0]); +//// img_visualize(images[1]); +// +//// matrix_print(images[0]->pixel_values); +//// matrix_print(images[1]->pixel_values); +// +// Neural_Network* nn = new_network(28*28, 40, 5, 10, 0.08); +// randomize_network(nn, 1); +//// Neural_Network* nn = load_network("../networks/newest_network.txt"); +//// printf("Done loading!\n"); +// +//// batch_train(nn, images, 20000, 20); +// +// for (int i = 0; i < 30000; ++i) { +// train_network(nn, images[i], images[i]->label); +// } +// +// save_network(nn); +// +// printf("%lf\n", measure_network_accuracy(nn, images, 10000)); + if(argc < 2){ + parsingErrorPrintHelp(); + exit(1); + } + if(strcmp(argv[1], "train") == 0){ + train(argc-2, argv+2); + return 0; + } + if(strcmp(argv[1], "predict") == 0){ + predict_demo(argc - 2, argv + 2); + return 0; + } + parsingErrorPrintHelp(); } \ No newline at end of file diff --git a/neuronal_network.c b/neuronal_network.c index 1c58018..8cf8616 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -2,6 +2,7 @@ #include "neuronal_network.h" #include #include +#include "util.h" double sigmoid(double input); Matrix* predict(Neural_Network* network, Matrix* image_data); @@ -45,9 +46,7 @@ void free_network(Neural_Network* network){ free(network); } -void save_network(Neural_Network* network) { - - char* file_name = "../networks/newest_network.txt"; +void save_network(Neural_Network* network, char * file_name) { // create file FILE* save_file = fopen(file_name, "w"); @@ -117,7 +116,9 @@ void print_network(Neural_Network* network) { double measure_network_accuracy(Neural_Network* network, Image** images, int amount) { int num_correct = 0; - for (int i = 0; i < amount; i++) { + printf("evaluating network\n"); + for (int i = 50001; i < amount; i++) { + updateBar(i*100/amount); Matrix* prediction = predict_image(network, images[i]); int guess = matrix_argmax(prediction); @@ -129,6 +130,7 @@ double measure_network_accuracy(Neural_Network* network, Image** images, int amo matrix_free(prediction); } + updateBar(100); return ((double) num_correct) / amount; } diff --git a/neuronal_network.h b/neuronal_network.h index a2db05f..34ccde0 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -21,7 +21,7 @@ Neural_Network* new_network(int input_size, int hidden_size, int hidden_amount, void randomize_network(Neural_Network* network, int scope); void free_network(Neural_Network* network); -void save_network(Neural_Network* network); +void save_network(Neural_Network* network, char * file_name); Neural_Network* load_network(char* file); void print_network(Neural_Network* network); From 2019d56122f6f65ceda123dbe9a443fe2ae5590a Mon Sep 17 00:00:00 2001 From: Raphael Walcher Date: Sun, 24 Sep 2023 20:58:34 +0200 Subject: [PATCH 07/18] picture --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9adb41..c52919b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ C-net ඞ is a Python project designed to read and predict numbers from the MNIST dataset using neural networks. ## Visuals -![Insert GIF or Screenshot here](link_to_visual.gif) +![Insert GIF or Screenshot here](https://camo.githubusercontent.com/b308207b5c5ce0970b13c21609350fab21aa61a2fae56da2a6418d6fdcdbc079/68747470733a2f2f7777772e776f6c6672616d2e636f6d2f6d617468656d61746963612f6e65772d696e2d31302f656e68616e6365642d696d6167652d70726f63657373696e672f48544d4c496d616765732e656e2f68616e647772697474656e2d6469676974732d636c617373696669636174696f6e2f736d616c6c7468756d625f31302e676966) ## Roadmap From eec607f7dd2d4abcafac3af6a0117729ed5819ce Mon Sep 17 00:00:00 2001 From: Raphael Walcher Date: Sun, 24 Sep 2023 21:00:37 +0200 Subject: [PATCH 08/18] picture --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c52919b..3dac6fa 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ This project was brought to you by the following contributors: - Dworski, Daniel - Walcher, Raphael -We would like to express our gratitude to the following project, which served as an inspiration and reference: +We would like to express our gratitude to the following sources, which served as an inspiration and reference: - [MNIST from Scratch](https://github.com/markkraay/mnist-from-scratch) by markkraay - [Neural Network Framework in C](https://medium.com/analytics-vidhya/building-neural-network-framework-in-c-using-backpropagation-8ad589a0752d) - [Simple Neural Network Implementation in C](https://towardsdatascience.com/simple-neural-network-implementation-in-c-663f51447547) @@ -32,3 +32,5 @@ We would like to express our gratitude to the following project, which served as ## Project Status The project is considered finished, but ongoing optimizations and improvements may still be in progress. + +![amogus](https://media.tenor.com/7kpsm7kU330AAAAd/sussy-among-us.gif) \ No newline at end of file From 2d7fe49598fee4910248f26f1eb01a8277bd32b4 Mon Sep 17 00:00:00 2001 From: rawalcher Date: Sun, 24 Sep 2023 19:07:24 +0000 Subject: [PATCH 09/18] Update file README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3dac6fa..5db1ec0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # C-net ඞ ## Description -C-net ඞ is a Python project designed to read and predict numbers from the MNIST dataset using neural networks. +C-net ඞ is a C project designed to read and predict numbers from the MNIST dataset using neural networks. ## Visuals ![Insert GIF or Screenshot here](https://camo.githubusercontent.com/b308207b5c5ce0970b13c21609350fab21aa61a2fae56da2a6418d6fdcdbc079/68747470733a2f2f7777772e776f6c6672616d2e636f6d2f6d617468656d61746963612f6e65772d696e2d31302f656e68616e6365642d696d6167652d70726f63657373696e672f48544d4c496d616765732e656e2f68616e647772697474656e2d6469676974732d636c617373696669636174696f6e2f736d616c6c7468756d625f31302e676966) From b9c3e93564e733da6fbd2a2331906b1ea23a273f Mon Sep 17 00:00:00 2001 From: jastornig Date: Sun, 24 Sep 2023 19:20:20 +0000 Subject: [PATCH 10/18] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..e0c4685 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,37 @@ +# This file is a template, and might need editing before it works on your project. +# You can copy and paste this template into a new `.gitlab-ci.yml` file. +# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword. +# +# To contribute improvements to CI/CD templates, please follow the Development guide at: +# https://docs.gitlab.com/ee/development/cicd/templates.html +# This specific template is located at: +# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/C++.gitlab-ci.yml + +# use the official gcc image, based on debian +# can use versions as well, like gcc:5.2 +# see https://hub.docker.com/_/gcc/ + +image: gcc + +build: + stage: build + # instead of calling g++ directly you can also use some build toolkit like make + # install the necessary build tools when needed + before_script: + - apt update && apt -y install cmake + script: + - cmake . + - cmake --build . + artifacts: + paths: + - c_make + # depending on your build setup it's most likely a good idea to cache outputs to reduce the build time + # cache: + # paths: + # - "*.o" + + +deploy: + stage: deploy + script: echo "Define your deployment script!" + environment: production From 38fafa672ce41bd0b9f04ae1492a622fa3598f83 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 21:27:59 +0200 Subject: [PATCH 11/18] minor changes for pgm files --- image.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/image.c b/image.c index 6818cf8..3fcbd1e 100644 --- a/image.c +++ b/image.c @@ -14,12 +14,13 @@ void big_endian_to_c_uint(const char * bytes, void * target, int size) { void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){ int bufferOffset = 0; - char c = -1; + char c; + int counter = 0; do{ c = (char)getc(fptr); buff[bufferOffset++] = c; - }while(!feof(fptr) && c != 0 && c != ' ' && c !='\n'); + }while(!feof(fptr) && c != 0 && c != ' ' && c !='\n' && counter++ < maxCount); buff[bufferOffset-1] = 0; } @@ -29,8 +30,7 @@ Image * load_pgm_image(char * image_file_string){ image->label = -1; - char buffer[100]; - int magic_number = 0; + char buffer[2048]; fgets(buffer, 4, fptr); if(buffer[0] != 'P' || buffer[1] != '5'){ printf("Wrong file Format"); @@ -40,17 +40,16 @@ Image * load_pgm_image(char * image_file_string){ fgets(buffer, 1024, fptr); } - int image_width, image_height, image_length, image_white ; + int image_width, image_height, image_white ; read_until_space_or_newline(buffer, 10, fptr); - image_width = strtol(buffer, NULL, 10); + image_width = (int)strtol(buffer, NULL, 10); read_until_space_or_newline(buffer, 10, fptr); - image_height = strtol(buffer, NULL, 10); + image_height = (int)strtol(buffer, NULL, 10); read_until_space_or_newline(buffer, 10, fptr); - image_white = strtol(buffer, NULL, 10); + image_white = (int)strtol(buffer, NULL, 10); - image_length = image_width * image_height; image->pixel_values = matrix_create(image_height, image_width); for(int i = 0; i < image_height; i++){ From 77b67aa949fc7875839333dfe14121955c455121 Mon Sep 17 00:00:00 2001 From: jastornig Date: Sun, 24 Sep 2023 20:12:14 +0000 Subject: [PATCH 12/18] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e0c4685..e7a3182 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,6 +12,9 @@ # see https://hub.docker.com/_/gcc/ image: gcc +stages: + - build + - release build: stage: build @@ -24,14 +27,25 @@ build: - cmake --build . artifacts: paths: - - c_make - # depending on your build setup it's most likely a good idea to cache outputs to reduce the build time - # cache: - # paths: - # - "*.o" + - c_net + reports: + dotenv: CI_JOB_ID.env + -deploy: - stage: deploy +release: + stage: release + needs: + - build + release: + tag_name: $CI_COMMIT_SHORT_SHA' + description: "latest" + + assets: + links: + - name: c_net + url: '${CI_PROJECT_URL}/-/jobs/${BUILD_JOB_ID}/artifacts/file/c_net' + script: echo "Define your deployment script!" - environment: production + + From a681833712c145887f251d1c4ddb1fde4bdc8f78 Mon Sep 17 00:00:00 2001 From: jastornig Date: Sun, 24 Sep 2023 20:19:40 +0000 Subject: [PATCH 13/18] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e7a3182..7e04fb1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -23,8 +23,11 @@ build: before_script: - apt update && apt -y install cmake script: + - echo BUILD_JOB_ID=$CI_JOB_ID >> CI_JOB_ID.env + - echo "Compiling the code..." - cmake . - cmake --build . + artifacts: paths: - c_net @@ -36,7 +39,8 @@ build: release: stage: release needs: - - build + - job: build + release: tag_name: $CI_COMMIT_SHORT_SHA' description: "latest" From e821afdbd5cf9741c5d203eff557198ec9ee42b6 Mon Sep 17 00:00:00 2001 From: jastornig Date: Sun, 24 Sep 2023 20:23:22 +0000 Subject: [PATCH 14/18] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7e04fb1..0af1ae5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -37,6 +37,7 @@ build: release: + image: registry.gitlab.com/gitlab-org/release-cli:latest stage: release needs: - job: build From 1cfdf93f7caf531c2d24231a51731c5c6222d493 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 22:29:41 +0200 Subject: [PATCH 15/18] (fix) evaluation trained with 50000 images evaluated with 10000 --- main.c | 15 +++++++-------- neuronal_network.c | 3 ++- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index b2f7302..1d17aa5 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ #include "util.h" void parsingErrorPrintHelp(){ - printf("Syntax: c_net [train | detect]\n"); + printf("Syntax: c_net [train | predict]\n"); printf("commands:\n"); printf("train\t train the network\n"); printf("predict\t load a pgm image and predict_demo the number\n"); @@ -58,19 +58,18 @@ void train(int argc, char** arguments) { } char *save_path = arguments[6]; int imported = 0; - Image **images = import_images(image_file, label_file, &imported, 50000); + Image ** images = import_images(image_file, label_file, &imported, 60000); + Image ** evaluation_images = images+50000; -// for(int i = 0; i < imported; i++){ -// matrix_save(images[i]->pixel_values, "images.txt"); -// } -// exit(1); + int training_image_count = 50000; + int testing_image_count = 10000; Neural_Network *nn = new_network(28 * 28, neurons_per_layer, hidden_count, 10, learning_rate); randomize_network(nn, 1); printf("training_network\n"); for(int epoch = 1; epoch <= epochs; epoch++){ printf("epoch %d\n", epoch); - for (int i = 0; i < imported; i++) { + for (int i = 0; i < training_image_count; i++) { if (i % 1000 == 0) { updateBar(i * 100 / imported); } @@ -78,7 +77,7 @@ void train(int argc, char** arguments) { } updateBar(100); printf("\n"); - printf("accuracy %lf\n", measure_network_accuracy(nn, images, 10000)); + printf("accuracy %lf\n", measure_network_accuracy(nn, evaluation_images, testing_image_count)); } printf("done training!\n"); save_network(nn, save_path); diff --git a/neuronal_network.c b/neuronal_network.c index 8cf8616..756b71d 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -117,7 +117,8 @@ double measure_network_accuracy(Neural_Network* network, Image** images, int amo int num_correct = 0; printf("evaluating network\n"); - for (int i = 50001; i < amount; i++) { + if(amount > 10000) amount = 10000; + for (int i = 0; i < amount; i++) { updateBar(i*100/amount); Matrix* prediction = predict_image(network, images[i]); From 754d170616a8e55b1be531a41e98909d2865ccf9 Mon Sep 17 00:00:00 2001 From: jastornig Date: Sun, 24 Sep 2023 20:36:11 +0000 Subject: [PATCH 16/18] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0af1ae5..39e9622 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,7 +48,7 @@ release: assets: links: - - name: c_net + - name: c_net linux download (precompiled) url: '${CI_PROJECT_URL}/-/jobs/${BUILD_JOB_ID}/artifacts/file/c_net' script: echo "Define your deployment script!" From 746f4cf3bd716b5ded967b3a29c3d4e246f76009 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 22:36:40 +0200 Subject: [PATCH 17/18] deleted unused functions --- matrix.c | 25 ------------------------- matrix.h | 2 -- 2 files changed, 27 deletions(-) diff --git a/matrix.c b/matrix.c index ba7c258..2a9acfc 100644 --- a/matrix.c +++ b/matrix.c @@ -216,21 +216,6 @@ Matrix* scale(Matrix* matrix, double value) { return result_matrix; } -Matrix* addScalar(Matrix* matrix, double value) { - - // create a copy of the original matrix - Matrix* result_matrix = matrix_copy(matrix); - - // iterate over all numbers in the matrix and add the scalar value - for (int i = 0; i < result_matrix->rows; i++) { - for (int j = 0; j < result_matrix->columns; j++) { - result_matrix->numbers[i][j] += value; - } - } - - // return the copy - return result_matrix; -} Matrix* transpose(Matrix* matrix) { @@ -249,16 +234,6 @@ 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 2e0e5a9..4ece217 100644 --- a/matrix.h +++ b/matrix.h @@ -36,6 +36,4 @@ Matrix* subtract(Matrix* matrix1, Matrix* matrix2); Matrix* dot(Matrix* matrix1, Matrix* matrix2); 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); \ No newline at end of file From aa58f99045c7a34fd7ee0fe7c25037280f481fc6 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Mon, 25 Sep 2023 11:11:07 +0200 Subject: [PATCH 18/18] minor changes --- image.c | 4 ++++ main.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/image.c b/image.c index 3fcbd1e..f52fa2c 100644 --- a/image.c +++ b/image.c @@ -26,6 +26,10 @@ void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){ Image * load_pgm_image(char * image_file_string){ FILE * fptr = fopen(image_file_string, "r"); + if(!fptr){ + printf("could not open image file. exit\n"); + exit(1); + } Image *image = malloc(sizeof(Image)); image->label = -1; diff --git a/main.c b/main.c index 1d17aa5..f4c966e 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,7 @@ void parsingErrorTrain(){ void parsingErrorDetect(){ printf("invalid syntax\n"); - printf("Syntax: c_net predict_demo [path_to_network] [image_file]"); + printf("Syntax: c_net predict_demo [path_to_network] [image_file]\n"); } void predict_demo(int argc, char** arguments){