Merge remote-tracking branch 'origin/Delta-Error-Test' into Delta-Error-Test
This commit is contained in:
commit
3a8ab15bf2
15 changed files with 108599 additions and 333 deletions
|
|
@ -3,5 +3,5 @@ project(c_net C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
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)
|
target_link_libraries(c_net m)
|
||||||
|
|
|
||||||
32
image.h
32
image.h
|
|
@ -1,32 +0,0 @@
|
||||||
#pragma once
|
|
||||||
#include "matrix.h"
|
|
||||||
|
|
||||||
#include "matrix.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
Matrix* pixel_values;
|
|
||||||
char label;
|
|
||||||
} Image;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const Image* image;
|
|
||||||
const size_t size;
|
|
||||||
} Image_Container;
|
|
||||||
|
|
||||||
static const int MAGIC_NUMBER_LABEL = 2049;
|
|
||||||
|
|
||||||
static const int MAGIC_NUMBER_IMAGES = 2051;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* reads a specified number of images out of the training dataset
|
|
||||||
* @param image_file_string Path to the file containing the image data
|
|
||||||
* @param label_file_string Path to the file containing the image labels
|
|
||||||
* @param ptr via this pointer, the images can be accessed
|
|
||||||
* @param count maximum number of images to be loaded. If it is 0, all available images are loaded.
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
Image ** import_images(char* image_file_string, char* label_file_string, int* number_imported, int count);
|
|
||||||
Image * load_pgm_image(char * image_file_string);
|
|
||||||
void img_print (Image* image);
|
|
||||||
void img_visualize(Image*image);
|
|
||||||
void img_free (Image* image);
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "matrix.h"
|
#include "../matrix/matrix.h"
|
||||||
#include "util.h"
|
#include "../util.h"
|
||||||
|
|
||||||
void big_endian_to_c_uint(const char * bytes, void * target, int size) {
|
void big_endian_to_c_uint(const char * bytes, void * target, int size) {
|
||||||
char* helper = (char*)target;
|
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) {
|
Image** import_images(char* image_file_string, char* label_file_string, int* _number_imported, int count) {
|
||||||
printf("Loading Images\n");
|
printf("Loading Images\n");
|
||||||
// create file pointer for the image and label data
|
// create file pointer for the image and label data
|
||||||
FILE* image_file = fopen(image_file_string, "r");
|
FILE* image_file = fopen(image_file_string, "rb");
|
||||||
FILE* label_file = fopen(label_file_string, "r");
|
FILE* label_file = fopen(label_file_string, "rb");
|
||||||
|
|
||||||
// check if the file could be opened
|
// check if the file could be opened
|
||||||
if(image_file == NULL || label_file == NULL) {
|
if(image_file == NULL || label_file == NULL) {
|
||||||
|
|
@ -90,7 +90,6 @@ Image** import_images(char* image_file_string, char* label_file_string, int* _nu
|
||||||
fread(word_buffer, 4, 1, label_file);
|
fread(word_buffer, 4, 1, label_file);
|
||||||
big_endian_to_c_uint(word_buffer, &label_count, buffer_size);
|
big_endian_to_c_uint(word_buffer, &label_count, buffer_size);
|
||||||
|
|
||||||
|
|
||||||
//Read description of file
|
//Read description of file
|
||||||
fread(word_buffer, 4, 1, image_file);
|
fread(word_buffer, 4, 1, image_file);
|
||||||
big_endian_to_c_uint(word_buffer, &magic_number_images, buffer_size);
|
big_endian_to_c_uint(word_buffer, &magic_number_images, buffer_size);
|
||||||
|
|
@ -160,7 +159,6 @@ Image** import_images(char* image_file_string, char* label_file_string, int* _nu
|
||||||
}
|
}
|
||||||
|
|
||||||
void img_print (Image* img) {
|
void img_print (Image* img) {
|
||||||
|
|
||||||
//print the image
|
//print the image
|
||||||
matrix_print(img->pixel_values);
|
matrix_print(img->pixel_values);
|
||||||
//print the number of the image
|
//print the number of the image
|
||||||
21
image/image.h
Normal file
21
image/image.h
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "../matrix/matrix.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Matrix* pixel_values;
|
||||||
|
char label;
|
||||||
|
} Image;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const Image* image;
|
||||||
|
const size_t size;
|
||||||
|
} Image_Container;
|
||||||
|
|
||||||
|
static const int MAGIC_NUMBER_LABEL = 2049;
|
||||||
|
static const int MAGIC_NUMBER_IMAGES = 2051;
|
||||||
|
|
||||||
|
Image ** import_images(char* image_file_string, char* label_file_string, int* number_imported, int count);
|
||||||
|
Image * load_pgm_image(char * image_file_string);
|
||||||
|
void img_print (Image* image);
|
||||||
|
void img_visualize(Image*image);
|
||||||
|
void img_free (Image* image);
|
||||||
|
void images_free (Image** images, int quantity);
|
||||||
62
main.c
62
main.c
|
|
@ -1,24 +1,64 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "image.h"
|
#include "image/image.h"
|
||||||
#include "neuronal_network.h"
|
#include "neuronal_network.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 60000);
|
|
||||||
|
const int amount_of_images_to_load = 60000;
|
||||||
|
const int amount_of_images_used_to_train = 30000;
|
||||||
|
const int amount_of_images_used_to_test = 1000;
|
||||||
|
const int input_size = 28*28;
|
||||||
|
const int hidden_layer_size = 50;
|
||||||
|
const int hidden_layer_count = 3;
|
||||||
|
const double learning_rate = 0.1;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Loading Images from Dataset
|
||||||
|
*/
|
||||||
|
|
||||||
|
Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, amount_of_images_to_load);
|
||||||
|
|
||||||
// img_visualize(images[0]);
|
// img_visualize(images[0]);
|
||||||
|
// img_print(images[0]);
|
||||||
|
|
||||||
Neural_Network* nn = new_network(28*28, 100, 5, 10, 0.01);
|
/*
|
||||||
randomize_network(nn, 10);
|
* Create a new network and randomize the weights
|
||||||
// save_network(nn);
|
*/
|
||||||
// Neural_Network* nn = load_network("../networks/test1.txt");
|
|
||||||
|
|
||||||
for (int i = 0; i < 60000; ++i) {
|
Neural_Network* network = new_network(input_size, hidden_layer_size, hidden_layer_count, 10, learning_rate);
|
||||||
train_network(nn, images[i], images[i]->label);
|
randomize_network(network, 1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Training
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (int i = 0; i < amount_of_images_used_to_train; i++) {
|
||||||
|
train_network(network, images[i], images[i]->label);
|
||||||
}
|
}
|
||||||
|
|
||||||
// train_network(nn, images[0], images[0]->label);
|
// Batch training works if you change the train_network method, but the results are not that good (needs further testing)
|
||||||
// train_network(nn, images[0], images[0]->label);
|
// batch_train(nn, images, 30000, 2);
|
||||||
|
|
||||||
printf("%lf\n", measure_network_accuracy(nn, images, 10000));
|
printf("Trinaing Done!\n");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Saving and Loading
|
||||||
|
*/
|
||||||
|
|
||||||
|
// save_network(network);
|
||||||
|
// Neural_Network* network = load_network("../networks/newest_network.txt");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
}
|
}
|
||||||
41
matrix.h
41
matrix.h
|
|
@ -1,41 +0,0 @@
|
||||||
#pragma once
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int rows, columns;
|
|
||||||
double **numbers;
|
|
||||||
} Matrix;
|
|
||||||
|
|
||||||
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);
|
|
||||||
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); // don't understand the usage of the 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);
|
|
||||||
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);
|
|
||||||
139
matrix/matrix.c
Normal file
139
matrix/matrix.c
Normal file
|
|
@ -0,0 +1,139 @@
|
||||||
|
#include "matrix.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_BYTES 100
|
||||||
|
|
||||||
|
Matrix* matrix_create(int rows, int columns) {
|
||||||
|
|
||||||
|
// allocate memory for the matrix
|
||||||
|
Matrix* matrix = malloc(sizeof(Matrix));
|
||||||
|
|
||||||
|
// set size variables to the correct size
|
||||||
|
matrix->rows = rows;
|
||||||
|
matrix->columns = columns;
|
||||||
|
|
||||||
|
// allocate memory for the numbers (2D-Array)
|
||||||
|
matrix->numbers = malloc(sizeof(double*) * rows);
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
|
matrix->numbers[i] = calloc(sizeof(double), columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the pointer to the allocated memory
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_fill(Matrix* matrix, double value) {
|
||||||
|
|
||||||
|
// simple for loop to populate the 2D-array with a value
|
||||||
|
for (int i = 0; i < matrix->rows; i++) {
|
||||||
|
for (int j = 0; j < matrix->columns; j++) {
|
||||||
|
matrix->numbers[i][j] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_free(Matrix* matrix) {
|
||||||
|
|
||||||
|
// de-allocate every column
|
||||||
|
for (int i = 0; i < matrix->rows; i++) {
|
||||||
|
free(matrix->numbers[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// de-allocate the rows
|
||||||
|
free(matrix->numbers);
|
||||||
|
|
||||||
|
// de-allocate the matrix
|
||||||
|
free(matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_print(Matrix *matrix) {
|
||||||
|
|
||||||
|
// print the dimensions of the matrix
|
||||||
|
printf("Rows: %d, Columns: %d\n", matrix->rows, matrix->columns);
|
||||||
|
|
||||||
|
// loop through all values and format them into the correct matrix representation
|
||||||
|
for (int i = 0; i < matrix->rows; i++) {
|
||||||
|
for (int j = 0; j < matrix->columns; j++) {
|
||||||
|
printf("%lf ", matrix->numbers[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix* matrix_copy(Matrix *matrix) {
|
||||||
|
|
||||||
|
// create another matrix of the same size
|
||||||
|
Matrix* copy_of_matrix = matrix_create(matrix->rows, matrix->columns);
|
||||||
|
|
||||||
|
// copy the values from the original matrix into the copy
|
||||||
|
for (int i = 0; i < matrix->rows; i++) {
|
||||||
|
for (int j = 0; j < matrix->columns; j++) {
|
||||||
|
copy_of_matrix->numbers[i][j] = matrix->numbers[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the pointer to the copy
|
||||||
|
return copy_of_matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_save(Matrix* matrix, char* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(file, "%.10f\n", matrix->numbers[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// close the file
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix* matrix_load(char* file_string){
|
||||||
|
|
||||||
|
FILE *fptr = fopen(file_string, "r");
|
||||||
|
|
||||||
|
if(!fptr){
|
||||||
|
printf("Could not open \"%s\"", file_string);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix * m = load_next_matrix(fptr);
|
||||||
|
|
||||||
|
fclose(fptr);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix* load_next_matrix(FILE *save_file){
|
||||||
|
|
||||||
|
char buffer[MAX_BYTES];
|
||||||
|
|
||||||
|
fgets(buffer, MAX_BYTES, save_file);
|
||||||
|
int rows = (int)strtol(buffer, NULL, 10);
|
||||||
|
fgets(buffer, MAX_BYTES, save_file);
|
||||||
|
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, save_file);
|
||||||
|
matrix->numbers[i][j] = strtod(buffer, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
15
matrix/matrix.h
Normal file
15
matrix/matrix.h
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int rows, columns;
|
||||||
|
double **numbers;
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
|
Matrix* matrix_create(int rows, int columns);
|
||||||
|
void matrix_fill(Matrix* matrix, double value);
|
||||||
|
void matrix_free(Matrix* matrix);
|
||||||
|
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);
|
||||||
|
|
@ -1,92 +1,10 @@
|
||||||
#include "matrix.h"
|
#include <process.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#define MAX_BYTES 100
|
#include "math.h"
|
||||||
|
#include "operations.h"
|
||||||
|
|
||||||
static int RANDOMIZED = 0;
|
static int RANDOMIZED = 0;
|
||||||
// operational functions
|
|
||||||
Matrix* matrix_create(int rows, int columns) {
|
|
||||||
|
|
||||||
// allocate memory for the matrix
|
|
||||||
Matrix* matrix = malloc(sizeof(Matrix));
|
|
||||||
|
|
||||||
// set size variables to the correct size
|
|
||||||
matrix->rows = rows;
|
|
||||||
matrix->columns = columns;
|
|
||||||
|
|
||||||
// allocate memory for the numbers (2D-Array)
|
|
||||||
matrix->numbers = malloc(sizeof(double*) * rows);
|
|
||||||
for (int i = 0; i < rows; i++) {
|
|
||||||
matrix->numbers[i] = calloc(sizeof(double), columns);
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the pointer to the allocated memory
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
void matrix_fill(Matrix* matrix, double value) {
|
|
||||||
|
|
||||||
// simple for loop to populate the 2D-array with a value
|
|
||||||
for (int i = 0; i < matrix->rows; i++) {
|
|
||||||
for (int j = 0; j < matrix->columns; j++) {
|
|
||||||
matrix->numbers[i][j] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void matrix_free(Matrix* matrix) {
|
|
||||||
|
|
||||||
// de-allocate every column
|
|
||||||
for (int i = 0; i < matrix->rows; i++) {
|
|
||||||
free(matrix->numbers[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// de-allocate the rows
|
|
||||||
free(matrix->numbers);
|
|
||||||
|
|
||||||
// de-allocate the matrix
|
|
||||||
free(matrix);
|
|
||||||
}
|
|
||||||
|
|
||||||
void matrix_print(Matrix *matrix) {
|
|
||||||
|
|
||||||
// print the dimensions of the matrix
|
|
||||||
printf("Rows: %d, Columns: %d\n", matrix->rows, matrix->columns);
|
|
||||||
|
|
||||||
// loop through all values and format them into the correct matrix representation
|
|
||||||
for (int i = 0; i < matrix->rows; i++) {
|
|
||||||
for (int j = 0; j < matrix->columns; j++) {
|
|
||||||
printf("%lf ", matrix->numbers[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix* matrix_copy(Matrix *matrix) {
|
|
||||||
|
|
||||||
// create another matrix of the same size
|
|
||||||
Matrix* copy_of_matrix = matrix_create(matrix->rows, matrix->columns);
|
|
||||||
|
|
||||||
// copy the values from the original matrix into the copy
|
|
||||||
for (int i = 0; i < matrix->rows; i++) {
|
|
||||||
for (int j = 0; j < matrix->columns; j++) {
|
|
||||||
copy_of_matrix->numbers[i][j] = matrix->numbers[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the pointer to the copy
|
|
||||||
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) {
|
Matrix* multiply(Matrix* matrix1, Matrix* matrix2) {
|
||||||
|
|
||||||
|
|
@ -216,22 +134,6 @@ Matrix* scale(Matrix* matrix, double value) {
|
||||||
return result_matrix;
|
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) {
|
Matrix* transpose(Matrix* matrix) {
|
||||||
|
|
||||||
// create a new matrix of the size n-m, based on the original matrix of size m-n
|
// create a new matrix of the size n-m, based on the original matrix of size m-n
|
||||||
|
|
@ -249,77 +151,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
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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(file, "%.10f\n", matrix->numbers[i][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// close the file
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix* matrix_load(char* file_string){
|
|
||||||
|
|
||||||
FILE *fptr = fopen(file_string, "r");
|
|
||||||
|
|
||||||
if(!fptr){
|
|
||||||
printf("Could not open \"%s\"", file_string);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix * m = load_next_matrix(fptr);
|
|
||||||
|
|
||||||
fclose(fptr);
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix* load_next_matrix(FILE *save_file){
|
|
||||||
|
|
||||||
char buffer[MAX_BYTES];
|
|
||||||
|
|
||||||
fgets(buffer, MAX_BYTES, save_file);
|
|
||||||
int rows = (int)strtol(buffer, NULL, 10);
|
|
||||||
fgets(buffer, MAX_BYTES, save_file);
|
|
||||||
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, save_file);
|
|
||||||
matrix->numbers[i][j] = strtod(buffer, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix* matrix_flatten(Matrix* matrix, int axis) {
|
Matrix* matrix_flatten(Matrix* matrix, int axis) {
|
||||||
// Axis = 0 -> Column Vector, Axis = 1 -> Row Vector
|
// Axis = 0 -> Column Vector, Axis = 1 -> Row Vector
|
||||||
Matrix* result_matrix;
|
Matrix* result_matrix;
|
||||||
|
|
@ -343,10 +174,10 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) {
|
||||||
return result_matrix;
|
return result_matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
int matrix_argmax(Matrix* matrix) {
|
int argmax(Matrix* matrix) {
|
||||||
// Expects a Mx1 matrix
|
// Expects a Mx1 matrix
|
||||||
if (matrix->columns != 1){
|
if (matrix->columns != 1){
|
||||||
printf("ERROR: Matrix is not Mx1 (matrix_argmax)");
|
printf("ERROR: Matrix is not Mx1 (argmax)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
25
matrix/operations.h
Normal file
25
matrix/operations.h
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#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);
|
||||||
44872
networks/89.txt
Normal file
44872
networks/89.txt
Normal file
File diff suppressed because it is too large
Load diff
63384
networks/90.txt
Normal file
63384
networks/90.txt
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,45 +0,0 @@
|
||||||
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
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "neuronal_network.h"
|
#include "neuronal_network.h"
|
||||||
|
#include "matrix\operations.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
double sigmoid(double input);
|
double sigmoid(double input);
|
||||||
Matrix* predict(Neural_Network* network, Matrix* image_data);
|
Matrix* predict(Neural_Network* network, Matrix* image_data);
|
||||||
double square(double input);
|
|
||||||
Matrix* sigmoid_derivative(Matrix* matrix);
|
Matrix* sigmoid_derivative(Matrix* matrix);
|
||||||
Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix);
|
Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix);
|
||||||
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);
|
||||||
Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output);
|
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){
|
Neural_Network* new_network(int input_size, int hidden_size, int hidden_amount, int output_size, double learning_rate){
|
||||||
|
|
@ -72,7 +72,7 @@ void save_network(Neural_Network* network) {
|
||||||
matrix_save(network->weights[i], file_name);
|
matrix_save(network->weights[i], file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Network Saved!");
|
printf("Network Saved!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Neural_Network* load_network(char* file) {
|
Neural_Network* load_network(char* file) {
|
||||||
|
|
@ -117,11 +117,17 @@ void print_network(Neural_Network* network) {
|
||||||
|
|
||||||
double measure_network_accuracy(Neural_Network* network, Image** images, int amount) {
|
double measure_network_accuracy(Neural_Network* network, Image** images, int amount) {
|
||||||
int num_correct = 0;
|
int num_correct = 0;
|
||||||
|
|
||||||
for (int i = 0; i < amount; i++) {
|
for (int i = 0; i < amount; i++) {
|
||||||
Matrix* prediction = predict_image(network, images[i]);
|
Matrix* prediction = predict_image(network, images[i]);
|
||||||
if (matrix_argmax(prediction) == images[i]->label) {
|
|
||||||
|
int guess = argmax(prediction);
|
||||||
|
int answer = (unsigned char) images[i]->label;
|
||||||
|
|
||||||
|
if (guess == answer) {
|
||||||
num_correct++;
|
num_correct++;
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix_free(prediction);
|
matrix_free(prediction);
|
||||||
}
|
}
|
||||||
return ((double) num_correct) / amount;
|
return ((double) num_correct) / amount;
|
||||||
|
|
@ -160,6 +166,52 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) {
|
||||||
return output[network->hidden_amount];
|
return output[network->hidden_amount];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//void batch_train(Neural_Network* network, Image** images, int amount, int batch_size) {
|
||||||
|
//
|
||||||
|
// if(amount % batch_size != 0) {
|
||||||
|
// printf("ERROR: Batch Size is not compatible with image amount! (batch_train)");
|
||||||
|
// exit(1);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// 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[image_index], images[image_index]->label);
|
||||||
|
//
|
||||||
|
// for (int k = 0; k < network->hidden_amount + 1; k++) {
|
||||||
|
//
|
||||||
|
// Matrix* temp_result = add(batch_weights[k], delta_weights[k]);
|
||||||
|
//
|
||||||
|
// matrix_free(batch_weights[k]);
|
||||||
|
// matrix_free(delta_weights[k]);
|
||||||
|
//
|
||||||
|
// batch_weights[k] = temp_result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// free(delta_weights);
|
||||||
|
//
|
||||||
|
// image_index++;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// 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, network->learning_rate);
|
||||||
|
//
|
||||||
|
// matrix_free(batch_weights[j]);
|
||||||
|
// matrix_free(average_delta_weight);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
void train_network(Neural_Network* network, Image *image, int label) {
|
void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
|
|
||||||
Matrix* image_data = matrix_flatten(image->pixel_values, 0);
|
Matrix* image_data = matrix_flatten(image->pixel_values, 0);
|
||||||
|
|
@ -181,7 +233,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
// back propagation
|
// back propagation
|
||||||
|
|
||||||
//list to store the new weights
|
//list to store the new weights
|
||||||
Matrix* delta_weights[network->hidden_amount + 1];
|
Matrix** delta_weights = malloc(sizeof(Matrix*) * (network->hidden_amount + 1));
|
||||||
|
|
||||||
// calculate the derivative of the sigmoid function of the input of the result layer
|
// calculate the derivative of the sigmoid function of the input of the result layer
|
||||||
Matrix* sigmoid_prime = sigmoid_derivative(output[network->hidden_amount]);
|
Matrix* sigmoid_prime = sigmoid_derivative(output[network->hidden_amount]);
|
||||||
|
|
@ -210,8 +262,14 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
delta = calculate_delta_hidden(previous_delta, network->weights[1], output[0]);
|
delta = calculate_delta_hidden(previous_delta, network->weights[1], output[0]);
|
||||||
delta_weights[0] = calculate_weights_delta(image_data, delta);
|
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) {
|
for (int i = 0; i < network->hidden_amount + 1; ++i) {
|
||||||
apply_weights(network, delta_weights[i], i);
|
apply_weights(network, delta_weights[i], i, network->learning_rate);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < network->hidden_amount + 1; ++i) {
|
||||||
|
matrix_free(delta_weights[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// De-allocate stuff
|
// De-allocate stuff
|
||||||
|
|
@ -222,9 +280,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
matrix_free(output[i]);
|
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(sigmoid_prime);
|
||||||
matrix_free(wanted_output);
|
matrix_free(wanted_output);
|
||||||
|
|
@ -232,6 +288,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
matrix_free(delta);
|
matrix_free(delta);
|
||||||
matrix_free(previous_delta);
|
matrix_free(previous_delta);
|
||||||
|
|
||||||
|
// return delta_weights;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output) {
|
Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output) {
|
||||||
|
|
@ -262,7 +319,7 @@ Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix
|
||||||
return new_deltas;
|
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) {
|
if(index > network->hidden_amount || index < 0) {
|
||||||
printf("ERROR: Index out of range! (apply_weights)");
|
printf("ERROR: Index out of range! (apply_weights)");
|
||||||
|
|
@ -274,11 +331,16 @@ void apply_weights(Neural_Network* network, Matrix* delta_weights_matrix, int in
|
||||||
exit(1);
|
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 i = 0; i < delta_weights_matrix->rows; i++) {
|
||||||
for (int j = 0; j < delta_weights_matrix->columns; j++) {
|
for (int j = 0; j < scaled_delta_weights_matrix->columns; j++) {
|
||||||
network->weights[index]->numbers[i][j] += delta_weights_matrix->numbers[i][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) {
|
Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix) {
|
||||||
|
|
@ -308,7 +370,3 @@ Matrix* sigmoid_derivative(Matrix* matrix) {
|
||||||
double sigmoid(double input) {
|
double sigmoid(double input) {
|
||||||
return 1.0 / (1 + exp(-1 * input));
|
return 1.0 / (1 + exp(-1 * input));
|
||||||
}
|
}
|
||||||
|
|
||||||
double square(double input) {
|
|
||||||
return input * input;
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
#include "matrix.h"
|
#include "matrix/matrix.h"
|
||||||
#include "image.h"
|
#include "image/image.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int input_size;
|
int input_size;
|
||||||
|
|
@ -26,6 +26,7 @@ Neural_Network* load_network(char* file);
|
||||||
|
|
||||||
void print_network(Neural_Network* network);
|
void print_network(Neural_Network* network);
|
||||||
|
|
||||||
|
void batch_train(Neural_Network* network, Image** images, int amount, int batch_size);
|
||||||
double measure_network_accuracy(Neural_Network* network, Image** images, int amount);
|
double measure_network_accuracy(Neural_Network* network, Image** images, int amount);
|
||||||
Matrix* predict_image(Neural_Network* network, Image* image);
|
Matrix* predict_image(Neural_Network* network, Image* image);
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue