load first commit
This commit is contained in:
parent
424035b1b2
commit
614df3c4a1
6 changed files with 106 additions and 20 deletions
25
main.c
25
main.c
|
|
@ -1,12 +1,29 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <malloc.h>
|
|
||||||
|
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
|
#include "neuronal_network.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 2);
|
// Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 2);
|
||||||
img_visualize(images[1]);
|
// img_visualize(images[1]);
|
||||||
|
|
||||||
|
// Neural_Network* nn = new_network(4, 2, 3, 0.5);
|
||||||
|
//
|
||||||
|
// int n = 20;
|
||||||
|
//
|
||||||
|
// matrix_randomize(nn->bias_1, n);
|
||||||
|
// matrix_randomize(nn->bias_2, n);
|
||||||
|
// matrix_randomize(nn->bias_3, n);
|
||||||
|
//
|
||||||
|
// matrix_randomize(nn->weights_1, n);
|
||||||
|
// matrix_randomize(nn->weights_2, n);
|
||||||
|
// matrix_randomize(nn->weights_3, n);
|
||||||
|
//
|
||||||
|
// matrix_randomize(nn->weights_output, n);
|
||||||
|
//
|
||||||
|
// save_network(nn);
|
||||||
|
|
||||||
|
Neural_Network* nn = load_network("../networks/test1.txt");
|
||||||
|
|
||||||
}
|
}
|
||||||
14
matrix.c
14
matrix.c
|
|
@ -339,20 +339,20 @@ int matrix_argmax(Matrix* matrix) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void matrix_randomize(Matrix* matrix, int n) {
|
void matrix_randomize(Matrix* matrix, int n) {
|
||||||
|
|
||||||
//make a min and max
|
//make a min and max
|
||||||
int min = -1.0 / sqrt(n);
|
double min = -1.0f / sqrt(n);
|
||||||
int max = 1.0 / sqrt(n);
|
double max = 1.0f / sqrt(n);
|
||||||
|
|
||||||
//calculate difference
|
//calculate difference
|
||||||
double difference = max - min;
|
double difference = max - min;
|
||||||
|
|
||||||
//move decimal
|
//move decimal
|
||||||
int scale = 10000;
|
int scaled_difference = (int)(difference * scaling_value);
|
||||||
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 i = 0; i < matrix->rows; i++) {
|
||||||
for (int j = 0; j < matrix->columns; j++) {
|
for (int j = 0; j < matrix->columns; j++) {
|
||||||
matrix->numbers[i][j] = random_result;
|
matrix->numbers[i][j] = min + (1.0 * (rand() % scaled_difference) / scaling_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
2
matrix.h
2
matrix.h
|
|
@ -5,6 +5,8 @@ typedef struct {
|
||||||
double **numbers;
|
double **numbers;
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
|
static const int scaling_value = 10000;
|
||||||
|
|
||||||
// operational functions
|
// operational functions
|
||||||
Matrix* matrix_create(int rows, int columns);
|
Matrix* matrix_create(int rows, int columns);
|
||||||
void matrix_fill(Matrix* matrix, double value);
|
void matrix_fill(Matrix* matrix, double value);
|
||||||
|
|
|
||||||
45
networks/test1.txt
Normal file
45
networks/test1.txt
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
4
|
||||||
|
2
|
||||||
|
3
|
||||||
|
2
|
||||||
|
1
|
||||||
|
-0.2195067977
|
||||||
|
-0.1657067977
|
||||||
|
2
|
||||||
|
4
|
||||||
|
0.0297932023
|
||||||
|
0.0289932023
|
||||||
|
-0.2106067977
|
||||||
|
-0.0132067977
|
||||||
|
-0.1003067977
|
||||||
|
-0.0923067977
|
||||||
|
-0.1315067977
|
||||||
|
0.1174932023
|
||||||
|
2
|
||||||
|
1
|
||||||
|
-0.0374067977
|
||||||
|
0.1903932023
|
||||||
|
2
|
||||||
|
2
|
||||||
|
-0.1219067977
|
||||||
|
-0.1745067977
|
||||||
|
0.0758932023
|
||||||
|
0.0761932023
|
||||||
|
2
|
||||||
|
1
|
||||||
|
-0.0955067977
|
||||||
|
0.0071932023
|
||||||
|
2
|
||||||
|
2
|
||||||
|
-0.1881067977
|
||||||
|
-0.1272067977
|
||||||
|
-0.1149067977
|
||||||
|
-0.1048067977
|
||||||
|
3
|
||||||
|
2
|
||||||
|
0.1665932023
|
||||||
|
-0.2083067977
|
||||||
|
-0.1944067977
|
||||||
|
0.1201932023
|
||||||
|
0.1768932023
|
||||||
|
-0.1408067977
|
||||||
|
|
@ -39,11 +39,7 @@ void free_network(Neural_Network* network){
|
||||||
|
|
||||||
void save_network(Neural_Network* network) {
|
void save_network(Neural_Network* network) {
|
||||||
|
|
||||||
// create file name and file string
|
char* file_name = "../networks/newest_network.txt";
|
||||||
time_t seconds;
|
|
||||||
time(&seconds);
|
|
||||||
char* file_name = "../networks/";
|
|
||||||
sprintf(file_name, "%ld", seconds);
|
|
||||||
|
|
||||||
// create file
|
// create file
|
||||||
FILE* save_file = fopen(file_name, "w");
|
FILE* save_file = fopen(file_name, "w");
|
||||||
|
|
@ -81,7 +77,31 @@ void save_network(Neural_Network* network) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Neural_Network* load_network(char* file) {
|
Neural_Network* load_network(char* file) {
|
||||||
return NULL;
|
|
||||||
|
// create file pointer and open file
|
||||||
|
FILE* save_file = fopen(file, "r");
|
||||||
|
|
||||||
|
// check if file could be opened
|
||||||
|
if(save_file == NULL) {
|
||||||
|
printf("ERROR: File could not be opened/found! (load_network)");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// read & store the information on the size of the network from the save file
|
||||||
|
char buffer[MAX_BYTES];
|
||||||
|
fgets(buffer, MAX_BYTES, save_file);
|
||||||
|
int input_size = (int) strtol(buffer, NULL, 10);
|
||||||
|
fgets(buffer, MAX_BYTES, save_file);
|
||||||
|
int hidden_size = (int) strtol(buffer, NULL, 10);
|
||||||
|
fgets(buffer, MAX_BYTES, save_file);
|
||||||
|
int output_size = (int) strtol(buffer, NULL, 10);
|
||||||
|
|
||||||
|
// create a new network to fill with the saved data
|
||||||
|
Neural_Network* saved_network = new_network(input_size, hidden_size, output_size, 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return saved_network;
|
||||||
}
|
}
|
||||||
|
|
||||||
//double predict_images(Neural_Network* network, Image** images, int amount) {
|
//double predict_images(Neural_Network* network, Image** images, int amount) {
|
||||||
|
|
@ -117,5 +137,5 @@ Neural_Network* load_network(char* file) {
|
||||||
// return result;
|
// return result;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
void train_network(Neural_Network* network, Matrix* input, Matrix* output);
|
//void train_network(Neural_Network* network, Matrix* input, Matrix* output);
|
||||||
void batch_train_network(Neural_Network* network, Image** images, int size);
|
//void batch_train_network(Neural_Network* network, Image** images, int size);
|
||||||
|
|
@ -25,6 +25,8 @@ typedef struct {
|
||||||
|
|
||||||
} Neural_Network;
|
} Neural_Network;
|
||||||
|
|
||||||
|
static const int MAX_BYTES = 100;
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
||||||
Reference in a new issue