From e824d7fdea4b4e349830cd91a0d35a69d3ec36c6 Mon Sep 17 00:00:00 2001 From: Ghost_Element Date: Tue, 19 Sep 2023 10:52:55 +0200 Subject: [PATCH 1/6] img print --- image.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/image.c b/image.c index ccb485b..028eabb 100644 --- a/image.c +++ b/image.c @@ -1,6 +1,8 @@ #pragma once +#include #include + #include "image.h" #include "matrix.h" @@ -21,11 +23,16 @@ void img_print (Img* img) { //print the image matrix_print(img->pixel_values); - - //print the label of the image - printf("%d", img->image_label); + //print the number of the image + printf("Number it is supposed to be: %d\n", img->image_label); } - void img_free (Img* img) { - + matrix_free(img->pixel_values); + free(img); +} +void images_free (Img** images, int quantity){ + for(int i=0;i Date: Tue, 19 Sep 2023 10:56:41 +0200 Subject: [PATCH 2/6] images_free + comments --- image.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/image.c b/image.c index 028eabb..e7d8862 100644 --- a/image.c +++ b/image.c @@ -20,19 +20,24 @@ Img** import_images(char* image_file_string, char* label_file_string, int number } void img_print (Img* img) { - //print the image matrix_print(img->pixel_values); //print the number of the image printf("Number it is supposed to be: %d\n", img->image_label); } + void img_free (Img* img) { + //frees the matrix of image (deep free) matrix_free(img->pixel_values); + //frees the rest of img free(img); } + void images_free (Img** images, int quantity){ + //frees every single image for(int i=0;i Date: Tue, 19 Sep 2023 11:13:10 +0200 Subject: [PATCH 3/6] fixed typo, imported flatten --- matrix.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/matrix.c b/matrix.c index 7954e59..667be8e 100644 --- a/matrix.c +++ b/matrix.c @@ -1,6 +1,7 @@ #include "matrix.h" #include #include +#include // operational functions Matrix* matrix_create(int rows, int columns) { @@ -244,7 +245,6 @@ Matrix* transpose(Matrix* matrix) { } - Matrix* matrix_flatten(Matrix* matrix, int axis) { // Axis = 0 -> Column Vector, Axis = 1 -> Row Vector Matrix* result_matrix; @@ -256,7 +256,7 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { else if (axis == 1) { result_matrix = matrix_create(1, matrix -> rows * matrix -> columns); } else { - printf("ERROR: Argument must be 1 or 0 (matrix_flatten"); + printf("ERROR: Argument must be 1 or 0 (matrix_flatten)"); exit(EXIT_FAILURE); } for (int i = 0; i < matrix->rows; i++) { @@ -265,5 +265,41 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; } } - return mat; + 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) { + //make a min and max + int min = -1.0 / sqrt(n); + int max = 1.0 / sqrt(n); + //calculate difference + double difference = max - min; + //move decimal + int scale = 10000 + int scaled_difference = (int)(difference * scale); + //calculate final random int and move decimal back + double random_result = min + (1.0 * (rand() % scaled_difference) / scale ); + + for (int i = 0; i < matrix->rows; i++) { + for (int j = 0; j < matrix->columns; j++) { + matrix->numbers[i][j] = random_result; + } + } +} \ No newline at end of file From 4d4e9a38e1a865c6ec3db67d77899eaf831b33aa Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Tue, 19 Sep 2023 11:28:51 +0200 Subject: [PATCH 4/6] matrix save and merge conflicts --- matrix.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/matrix.c b/matrix.c index 7954e59..916f4f4 100644 --- a/matrix.c +++ b/matrix.c @@ -244,6 +244,21 @@ 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); + exit(1); + } + for(int i = 0; i < matrix->rows; i++){ + for(int j = 0; j < matrix->columns; j++){ + fprintf(fptr, "%f.12 ", matrix->numbers[i][j]); + } + fputc('\n', fptr); + } +} + Matrix* matrix_flatten(Matrix* matrix, int axis) { // Axis = 0 -> Column Vector, Axis = 1 -> Row Vector @@ -265,5 +280,5 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; } } - return mat; + return result_matrix; } From 3473999ffc4e8379cce8cf4b90182eff0162de11 Mon Sep 17 00:00:00 2001 From: Ghost_Element Date: Tue, 19 Sep 2023 11:36:25 +0200 Subject: [PATCH 5/6] images_free + comments --- neuronal_network.cpp | 3 +++ neuronal_network.h | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100644 neuronal_network.cpp create mode 100644 neuronal_network.h diff --git a/neuronal_network.cpp b/neuronal_network.cpp new file mode 100644 index 0000000..dcd02e5 --- /dev/null +++ b/neuronal_network.cpp @@ -0,0 +1,3 @@ +// +// Created by danie on 19.09.2023. +// diff --git a/neuronal_network.h b/neuronal_network.h new file mode 100644 index 0000000..c0d14c8 --- /dev/null +++ b/neuronal_network.h @@ -0,0 +1,7 @@ +#pragma once + +typedef struct { + Matrix* input; + Matrix* output; + +} Neuronal_Network; \ No newline at end of file From 53553842ed836c6ae9b2aba747f5d11c88afc608 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Tue, 19 Sep 2023 12:51:19 +0200 Subject: [PATCH 6/6] Fixed sqrt and implemented matrix load --- CMakeLists.txt | 1 + matrix.c | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5038efd..1688010 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,3 +4,4 @@ project(c_net C) set(CMAKE_C_STANDARD 11) add_executable(c_net main.c matrix.c) +target_link_libraries(c_net m) diff --git a/matrix.c b/matrix.c index 0d0cccd..e2f4416 100644 --- a/matrix.c +++ b/matrix.c @@ -2,6 +2,7 @@ #include #include #include +#define MAX_BYTES 100 // operational functions Matrix* matrix_create(int rows, int columns) { @@ -252,12 +253,40 @@ void matrix_save(Matrix* matrix, char* file_string){ printf("Unable to get handle for \"%s\"", file_string); exit(1); } + fprintf(fptr, "%d\n", matrix->rows); + fprintf(fptr, "%d\n", matrix->columns); + for(int i = 0; i < matrix->rows; i++){ for(int j = 0; j < matrix->columns; j++){ - fprintf(fptr, "%f.12 ", matrix->numbers[i][j]); + fprintf(fptr, "%.10f\n", matrix->numbers[i][j]); } - fputc('\n', fptr); } + printf("saved matrix to %s", file_string); + fclose(fptr); +} + +Matrix* matrix_load(char* file_string){ + FILE *fptr = fopen(file_string, "r"); + if(!fptr){ + printf("Could not open \"%s\"", file_string); + exit(1); + } + char buffer[MAX_BYTES]; + + fgets(buffer, MAX_BYTES, fptr); + int rows = (int)strtol(buffer, NULL, 10); + fgets(buffer, MAX_BYTES, fptr); + 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); + matrix->numbers[i][j] = strtod(buffer, NULL); + } + } + return matrix; }