Save network and overworked save matrix
This commit is contained in:
parent
13540377ff
commit
af81566c45
3 changed files with 65 additions and 11 deletions
24
matrix.c
24
matrix.c
|
|
@ -248,21 +248,29 @@ Matrix* transpose(Matrix* matrix) {
|
||||||
|
|
||||||
//file operations
|
//file operations
|
||||||
void matrix_save(Matrix* matrix, char* file_string){
|
void matrix_save(Matrix* matrix, char* file_string){
|
||||||
FILE *fptr = fopen(file_string, "w+");
|
|
||||||
if(!fptr){
|
// open the file in append mode
|
||||||
printf("Unable to get handle for \"%s\"", file_string);
|
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);
|
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 i = 0; i < matrix->rows; i++){
|
||||||
for(int j = 0; j < matrix->columns; j++){
|
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){
|
Matrix* matrix_load(char* file_string){
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,58 @@
|
||||||
|
|
||||||
#include "neuronal_network.h"
|
#include "neuronal_network.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate);
|
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate);
|
||||||
void print_network(Neural_Network* network);
|
void print_network(Neural_Network* network);
|
||||||
void free_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);
|
|
||||||
|
// 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);
|
double predict_images(Neural_Network* network, Image** images, int amount);
|
||||||
Matrix* predict_image(Neural_Network* network, Image*);
|
Matrix* predict_image(Neural_Network* network, Image*);
|
||||||
|
|
|
||||||
|
|
@ -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 print_network(Neural_Network* network);
|
||||||
void free_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);
|
Neural_Network* load_network(char* file);
|
||||||
|
|
||||||
double predict_images(Neural_Network* network, Image** images, int amount);
|
double predict_images(Neural_Network* network, Image** images, int amount);
|
||||||
|
|
|
||||||
Reference in a new issue