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 3b773e5..6a352d6 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -1,5 +1,7 @@ #include #include "neuronal_network.h" +#include +#include Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){ Neural_Network network = malloc(sizeof(Neural_Network)); @@ -35,8 +37,52 @@ 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) { int num_correct = 0; diff --git a/neuronal_network.h b/neuronal_network.h index a21d553..09ec61c 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);