HolyFuckItsAlive #13

Merged
jastornig merged 105 commits from Delta-Error-Test into main 2023-09-23 22:27:54 +02:00
19 changed files with 109466 additions and 0 deletions

60
.gitignore vendored Normal file
View file

@ -0,0 +1,60 @@
CLion Tings
cmake-build-debug
.idea/workspace.xml
Prerequisites
*.d
Object files
*.o
*.ko
*.obj
*.elf
Linker output
*.ilk
*.map
*.exp
Precompiled Headers
*.gch
*.pch
Libraries
*.lib
*.a
*.la
*.lo
Shared objects (inc. Windows DLLs)
*.dll
*.so
.so.
*.dylib
Executables
*.exe
*.out
*.app
.i86
*.x86_64
*.hex
Debug files
*.dSYM/
*.su
*.idb
*.pdb
Kernel Module Compile Results
.mod
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
/.idea/.name
/.idea/misc.xml
/.idea/shelf/Uncommitted_changes_before_Update_at_21_09_23,_09_38_[Changes]/shelved.patch
/.idea/shelf/Uncommitted_changes_before_Update_at_21_09_23,_09_38_[Changes]/shelved.patch

8
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

2
.idea/c-net.iml generated Normal file
View file

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/c-net.iml" filepath="$PROJECT_DIR$/.idea/c-net.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

7
CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.22)
project(c_net C)
set(CMAKE_C_STANDARD 11)
add_executable(c_net main.c matrix.c image.c neuronal_network.c util.c util.h)
target_link_libraries(c_net m)

Binary file not shown.

Binary file not shown.

193
image.c Normal file
View file

@ -0,0 +1,193 @@
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
#include "matrix.h"
#include "util.h"
void big_endian_to_c_uint(const char * bytes, void * target, int size) {
char* helper = (char*)target;
for(int i = 0; i < size; i++){
*(helper+i) = *(bytes+size-i-1);
}
}
void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){
int bufferOffset = 0;
char c = -1;
do{
c = (char)getc(fptr);
buff[bufferOffset++] = c;
}while(!feof(fptr) && c != 0 && c != ' ' && c !='\n');
buff[bufferOffset-1] = 0;
}
Image * load_pgm_image(char * image_file_string){
FILE * fptr = fopen(image_file_string, "r");
Image *image = malloc(sizeof(Image));
image->label = -1;
char buffer[100];
int magic_number = 0;
fgets(buffer, 4, fptr);
if(buffer[0] != 'P' || buffer[1] != '5'){
printf("Wrong file Format");
exit(1);
}
if(fgetc(fptr) == '#'){
fgets(buffer, 1024, fptr);
}
int image_width, image_height, image_length, image_white ;
read_until_space_or_newline(buffer, 10, fptr);
image_width = strtol(buffer, NULL, 10);
read_until_space_or_newline(buffer, 10, fptr);
image_height = strtol(buffer, NULL, 10);
read_until_space_or_newline(buffer, 10, fptr);
image_white = strtol(buffer, NULL, 10);
image_length = image_width * image_height;
image->pixel_values = matrix_create(image_height, image_width);
for(int i = 0; i < image_height; i++){
fread(buffer, 1, 28, fptr);
for(int j = 0; j < image_width; j++){
image->pixel_values->numbers[i][j] = (image_white - (unsigned char)buffer[j]) / 255.0;
}
}
fclose(fptr);
return image;
}
Image** import_images(char* image_file_string, char* label_file_string, int* _number_imported, int count) {
printf("Loading Images\n");
// create file pointer for the image and label data
FILE* image_file = fopen(image_file_string, "r");
FILE* label_file = fopen(label_file_string, "r");
// check if the file could be opened
if(image_file == NULL || label_file == NULL) {
printf("ERROR: File could not be opened! (import_images)");
exit(1);
}
// check magic number of the files
char word_buffer[4];
int buffer_size = sizeof(word_buffer);
int magic_number_label, magic_number_images, label_count, image_count;
//Read description of label file
fread(word_buffer, buffer_size, 1, label_file);
big_endian_to_c_uint(word_buffer, &magic_number_label, buffer_size);
fread(word_buffer, 4, 1, label_file);
big_endian_to_c_uint(word_buffer, &label_count, buffer_size);
//Read description of file
fread(word_buffer, 4, 1, image_file);
big_endian_to_c_uint(word_buffer, &magic_number_images, buffer_size);
fread(word_buffer, 4, 1, image_file);
big_endian_to_c_uint(word_buffer, &image_count, buffer_size);
// compare magic numbers with pre-defined value
if(magic_number_label != MAGIC_NUMBER_LABEL || magic_number_images != MAGIC_NUMBER_IMAGES) {
printf("TrainingData or Labels are malformed. Exiting...");
exit(1);
}
if(label_count != image_count){
printf("Number of images and labels does not match. Exiting...");
exit(1);
}
if(count <= 0){
count = image_count;
}
if(count > image_count){
count = image_count;
printf("Number of images exceeds number of available images. Loading all available images");
}
int image_height, image_width, image_length;
//read image dimensions;
fread(word_buffer, 4, 1, image_file);
big_endian_to_c_uint(word_buffer, &image_height, buffer_size);
fread(word_buffer, 4, 1, image_file);
big_endian_to_c_uint(word_buffer, &image_width, 4);
image_length = image_height*image_width;
// allocate memory for the storage of images
Image** images = malloc(sizeof(Image*) * count);
if(!images){
printf("not enough memory");
exit(1);
}
unsigned char byteBuffer[image_length];
for(int i = 0; i < count; i++){
if(i%1000 == 0){
updateBar(i*100/count);
}
images[i] = malloc(sizeof(Image));
fread(&images[i]->label, 1, 1, label_file);
fread(&byteBuffer, image_width*image_height, 1, image_file);
images[i]->pixel_values = matrix_create(image_height, image_width);
for(int j = 0; j < image_length; j++) {
images[i]->pixel_values->numbers[j / image_width][j % image_width] = byteBuffer[j] / 255.0;
}
}
if(_number_imported != NULL)*_number_imported = count;
fclose(image_file);
fclose(label_file);
updateBar(100);
printf("\n");
return images;
}
void img_print (Image* 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->label);
}
void img_visualize(Image* img){
for(int i = 0; i < img->pixel_values->rows; i++){
for(int j = 0; j < img->pixel_values->columns; j++){
img->pixel_values->numbers[i][j] > 0.5 ? putc('#', stdout) : putc(' ', stdout);
}
putc('\n', stdout);
}
printf("Should be %d\n", img->label);
}
void img_free (Image* img) {
//frees the matrix of image (deep free)
matrix_free(img->pixel_values);
//frees the rest of img
free(img);
}
void images_free (Image** images, int quantity){
//frees every single image
for(int i=0;i<quantity;i++){
img_free(images[i]);
}
//frees the rest of images
free(images);
}

32
image.h Normal file
View file

@ -0,0 +1,32 @@
#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);

29
main.c Normal file
View file

@ -0,0 +1,29 @@
#include <stdio.h>
#include "image.h"
#include "neuronal_network.h"
int main() {
Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 60000);
// img_visualize(images[0]);
// img_visualize(images[1]);
// matrix_print(images[0]->pixel_values);
// matrix_print(images[1]->pixel_values);
Neural_Network* nn = new_network(28*28, 40, 5, 10, 0.08);
randomize_network(nn, 1);
// Neural_Network* nn = load_network("../networks/newest_network.txt");
// printf("Done loading!\n");
// batch_train(nn, images, 20000, 20);
for (int i = 0; i < 30000; ++i) {
train_network(nn, images[i], images[i]->label);
}
save_network(nn);
printf("%lf\n", measure_network_accuracy(nn, images, 10000));
}

402
matrix.c Normal file
View file

@ -0,0 +1,402 @@
#include "matrix.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define MAX_BYTES 100
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) {
// check if the two matrices are of the same size
if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) {
printf("ERROR: Size of matrices are not compatible! (Multiply)");
exit(1);
}
// create result matrix
Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns);
// multiply the values and save them into the result matrix
for (int i = 0; i < matrix1->rows; i++) {
for (int j = 0; j < matrix1->columns; j++) {
result_matrix->numbers[i][j] = matrix1->numbers[i][j] * matrix2->numbers[i][j];
}
}
// return resulting matrix
return result_matrix;
}
Matrix* add(Matrix* matrix1, Matrix* matrix2) {
// check if the two matrices are of the same size
if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) {
printf("ERROR: Size of matrices are not compatible! (Add)");
exit(1);
}
// create result matrix
Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns);
// add the value of the number in matrix 1 to the value of the number in matrix 2
for (int i = 0; i < matrix1->rows; i++) {
for (int j = 0; j < matrix1->columns; j++) {
result_matrix->numbers[i][j] = matrix1->numbers[i][j] + matrix2->numbers[i][j];
}
}
// return the result matrix
return result_matrix;
}
Matrix* subtract(Matrix* matrix1, Matrix* matrix2) {
// check if the two matrices are of the same size
if(matrix1->rows != matrix2->rows || matrix1->columns != matrix2->columns) {
printf("ERROR: Size of matrices are not compatible! (Subtract)");
exit(1);
}
// create result matrix
Matrix* result_matrix = matrix_create(matrix1->rows, matrix1->columns);
// subtract the value of the number in matrix 2 from the value of the number in matrix 1
for (int i = 0; i < matrix1->rows; i++) {
for (int j = 0; j < matrix1->columns; j++) {
result_matrix->numbers[i][j] = matrix1->numbers[i][j] - matrix2->numbers[i][j];
}
}
// return the resulting matrix
return result_matrix;
}
Matrix* dot(Matrix* matrix1, Matrix* matrix2) {
// check if the dimensions of the matrices are compatible to calculate the dot product
if(matrix1->columns != matrix2->rows) {
printf("ERROR: Size of matrices are not compatible! (Dot-Product)");
exit(1);
}
// create a new matrix with the dimensions of the dot product;
Matrix* result_matrix = matrix_create(matrix1->rows, matrix2->columns);
// iterate through all rows of matrix 1
for (int i = 0; i < matrix1->rows; i++) {
// iterate though all columns of matrix 2
for (int j = 0; j < matrix2->columns; j++) {
// sum up the products and save them into the result matrix
result_matrix->numbers[i][j] = 0;
for (int k = 0; k < matrix2->rows; k++) {
result_matrix->numbers[i][j] += matrix1->numbers[i][k] * matrix2->numbers[k][j];
}
}
}
// return result
return result_matrix;
}
Matrix* apply(double (*function)(double), Matrix* matrix) {
// create a new matrix used to calculate the result
Matrix* result_matrix = matrix_create(matrix->rows, matrix->columns);
// apply the function to all values in the matrix
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->columns; j++) {
result_matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]);
int k = 0;
}
}
// return resulting matrix
return result_matrix;
}
Matrix* scale(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 multiply by 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* 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) {
// create a new matrix of the size n-m, based on the original matrix of size m-n
Matrix* result_matrix = matrix_create(matrix->columns, matrix->rows);
// copy the values from the original into the correct place in the copy
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->columns; j++) {
result_matrix->numbers[j][i] = matrix->numbers[i][j];
}
}
// return the result matrix
return result_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) {
// Axis = 0 -> Column Vector, Axis = 1 -> Row Vector
Matrix* result_matrix;
// Column Vector
if (axis == 0) {
result_matrix = matrix_create(matrix -> rows * matrix -> columns, 1);
}
// Row Vector
else if (axis == 1) {
result_matrix = matrix_create(1, matrix -> rows * matrix -> columns);
} else {
printf("ERROR: Argument must be 1 or 0 (matrix_flatten)");
exit(EXIT_FAILURE);
}
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->columns; j++) {
if (axis == 0) result_matrix->numbers[i * matrix->columns + j][0] = matrix->numbers[i][j];
else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j];
}
}
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) {
if(!RANDOMIZED){
srand(time(NULL));
RANDOMIZED = 1;
}
//make a min and max
double min = -1.0f / sqrt(n);
double max = 1.0f / sqrt(n);
//calculate difference
double difference = max - min;
//move decimal
int scaled_difference = (int)(difference * scaling_value);
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->columns; j++) {
matrix->numbers[i][j] = min + (1.0 * (rand() % scaled_difference) / scaling_value);
}
}
}
Matrix* matrix_add_bias(Matrix* matrix) {
if(matrix->columns != 1) {
printf("ERROR: The size of the matrix does not match an input matrix! (matrix_add_bias)");
exit(1);
}
Matrix* result = matrix_create(matrix->rows + 1, matrix->columns);
result->numbers[0][0] = 1.0;
for (int i = 0; i < matrix->rows; ++i) {
result->numbers[i + 1][0] = matrix->numbers[i][0];
}
return result;
}

41
matrix.h Normal file
View file

@ -0,0 +1,41 @@
#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);

44872
networks/89.txt Normal file

File diff suppressed because it is too large Load diff

63384
networks/90.txt Normal file

File diff suppressed because it is too large Load diff

360
neuronal_network.c Normal file
View file

@ -0,0 +1,360 @@
#include <stdlib.h>
#include "neuronal_network.h"
#include <stdio.h>
#include <math.h>
double sigmoid(double input);
Matrix* predict(Neural_Network* network, Matrix* image_data);
Matrix* sigmoid_derivative(Matrix* matrix);
Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix, double learning_rate);
void apply_weights(Neural_Network* network, Matrix* delta_weights_matrix, int index);
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* network = malloc(sizeof(Neural_Network));
network->input_size = input_size;
network->hidden_size = hidden_size;
network->hidden_amount = hidden_amount;
network->output_size = output_size;
network->learning_rate = learning_rate;
Matrix** weights = malloc(sizeof(Matrix*) * (hidden_amount + 1));
network->weights = weights;
network->weights[0] = matrix_create(hidden_size, input_size + 1);
for(int i=1;i<hidden_amount;i++){
network->weights[i] = matrix_create(hidden_size, hidden_size + 1);
}
network->weights[hidden_amount] = matrix_create(output_size, hidden_size + 1);
return network;
}
void randomize_network(Neural_Network* network, int scope){
for (int i = 0; i < network->hidden_amount + 1; i++) {
matrix_randomize(network->weights[i], scope);
}
}
void free_network(Neural_Network* network){
for (int i = 0; i < network->hidden_amount + 1; i++) {
matrix_free(network->weights[i]);
}
free(network->weights);
free(network);
}
void save_network(Neural_Network* network) {
char* file_name = "../networks/newest_network.txt";
// 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->hidden_amount);
fprintf(save_file, "%d\n", network->output_size);
// close the file
fclose(save_file);
for (int i = 0; i < network->hidden_amount + 1; ++i) {
matrix_save(network->weights[i], file_name);
}
printf("Network Saved!\n");
}
Neural_Network* load_network(char* file) {
// 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 hidden_amount = (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, hidden_amount, output_size, 0);
for (int i = 0; i < saved_network->hidden_amount + 1; ++i) {
saved_network->weights[i] = load_next_matrix(save_file);
}
// return saved network
fclose(save_file);
return saved_network;
}
void print_network(Neural_Network* network) {
for (int i = 0; i < network->hidden_amount; ++i) {
matrix_print(network->weights[i]);
}
}
double measure_network_accuracy(Neural_Network* network, Image** images, int amount) {
int num_correct = 0;
for (int i = 0; i < amount; i++) {
Matrix* prediction = predict_image(network, images[i]);
int guess = matrix_argmax(prediction);
int answer = (unsigned char) images[i]->label;
if (guess == answer) {
num_correct++;
}
matrix_free(prediction);
}
return ((double) num_correct) / amount;
}
Matrix* predict_image(Neural_Network* network, Image* image){
Matrix* image_data = matrix_flatten(image->pixel_values, 0);
Matrix* res = predict(network, image_data);
matrix_free(image_data);
return res;
}
Matrix* predict(Neural_Network* network, Matrix* image_data) {
Matrix* input = matrix_add_bias(image_data);
Matrix* output[network->hidden_amount + 1];
for (int i = 0; i < network->hidden_amount + 1; ++i) {
Matrix* neuron_input = dot(network->weights[i], input);
Matrix* neuron_activation = apply(sigmoid, neuron_input);
output[i] = neuron_activation;
matrix_free(neuron_input);
matrix_free(input);
input = matrix_add_bias(neuron_activation);
}
for (int i = 0; i < network->hidden_amount; ++i) {
matrix_free(output[i]);
}
matrix_free(input);
return output[network->hidden_amount];
}
//void batch_train(Neural_Network* network, Image** images, int amount, int batch_size) {
//
// for (int i = 0; i < amount; ++i) {
//
// if(amount % 1000 == 0) {
// printf("1k pics!\n");
// }
//
// Matrix* batch_weights[network->hidden_amount + 1];
//
// for (int j = 0; j < batch_size; ++j) {
// Matrix** delta_weights = train_network(network, images[i], images[i]->label);
//
// for (int k = 0; k < network->hidden_amount + 1; k++) {
// if(j == 0) {
// batch_weights[k] = delta_weights[k];
// continue;
// }
//
// 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);
// }
//
// 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);
//
// matrix_free(average_delta_weight);
// matrix_free(batch_weights[j]);
// }
// }
//}
void train_network(Neural_Network* network, Image *image, int label) {
Matrix* image_data = matrix_flatten(image->pixel_values, 0);
Matrix* input = matrix_add_bias(image_data);
Matrix* output[network->hidden_amount + 1];
for (int i = 0; i < network->hidden_amount + 1; ++i) {
Matrix* neuron_input = dot(network->weights[i], input);
Matrix* neuron_activation = apply(sigmoid, neuron_input);
output[i] = neuron_activation;
matrix_free(neuron_input);
matrix_free(input);
input = matrix_add_bias(neuron_activation);
}
// back propagation
//list to store the new weights
Matrix** delta_weights = malloc(sizeof(Matrix*) * (network->hidden_amount + 1));
// calculate the derivative of the sigmoid function of the input of the result layer
Matrix* sigmoid_prime = sigmoid_derivative(output[network->hidden_amount]);
// create wanted out-put matrix, calculate the difference and delta values (output layer only)
Matrix* wanted_output = matrix_create(output[network->hidden_amount]->rows, output[network->hidden_amount]->columns);
matrix_fill(wanted_output, 0);
wanted_output->numbers[label][0] = 1;
Matrix* error = subtract(wanted_output, output[network->hidden_amount]);
Matrix* delta = multiply(sigmoid_prime, error);
//calculate and apply the delta for all weights in out-put layer
delta_weights[network->hidden_amount] = calculate_weights_delta(output[network->hidden_amount - 1], delta, network->learning_rate);
//hidden layers
Matrix* previous_delta = delta;
for (int i = network->hidden_amount; i > 1; i--) {
delta = calculate_delta_hidden(previous_delta, network->weights[i], output[i - 1]);
delta_weights[i - 1] = calculate_weights_delta(output[i - 2], delta, network->learning_rate);
matrix_free(previous_delta);
previous_delta = delta;
}
// Input Layer
delta = calculate_delta_hidden(previous_delta, network->weights[1], output[0]);
delta_weights[0] = calculate_weights_delta(image_data, delta, network->learning_rate);
for (int i = 0; i < network->hidden_amount + 1; ++i) {
apply_weights(network, delta_weights[i], i);
}
// De-allocate stuff
matrix_free(image_data);
matrix_free(input);
for (int i = 0; i < network->hidden_amount + 1; ++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(wanted_output);
matrix_free(error);
matrix_free(delta);
matrix_free(previous_delta);
// return delta_weights;
}
Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output) {
// remove bias weights from weights
Matrix* weights_without_biases = matrix_create(weights->rows, weights->columns - 1);
for (int i = 0; i < weights->rows; ++i) {
for (int j = 0; j < weights->columns - 1; ++j) {
weights_without_biases->numbers[i][j] = weights->numbers[i][j + 1];
}
}
// transpose the new weights and multiply with deltas
Matrix* transposed_weight_without_biases = transpose(weights_without_biases);
Matrix* sum_delta_weights = dot(transposed_weight_without_biases, next_layer_delta);
//multiply with derivative of current layer output
Matrix* sigmoid_prime = sigmoid_derivative(current_layer_output);
// multiply to find deltas for current layer
Matrix* new_deltas = multiply(sigmoid_prime, sum_delta_weights);
matrix_free(weights_without_biases);
matrix_free(transposed_weight_without_biases);
matrix_free(sum_delta_weights);
matrix_free(sigmoid_prime);
return new_deltas;
}
void apply_weights(Neural_Network* network, Matrix* delta_weights_matrix, int index) {
if(index > network->hidden_amount || index < 0) {
printf("ERROR: Index out of range! (apply_weights)");
exit(1);
}
if(delta_weights_matrix->rows != network->weights[index]->rows ||
delta_weights_matrix->columns != network->weights[index]->columns) {
printf("ERROR: Size of weight matrices do not match! (apply_weights)");
exit(1);
}
for (int i = 0; i < delta_weights_matrix->rows; i++) {
for (int j = 0; j < delta_weights_matrix->columns; j++) {
network->weights[index]->numbers[i][j] += delta_weights_matrix->numbers[i][j]; // multiply delta_weights_matrix with learning rate AND - instead of + because soll-ist
}
}
}
Matrix *calculate_weights_delta(Matrix *previous_layer_output, Matrix *delta_matrix, double learning_rate) {
Matrix* previous_out_with_one = matrix_add_bias(previous_layer_output);
Matrix* transposed_previous_out_with_bias = transpose(previous_out_with_one);
Matrix* weights_delta_matrix = dot(delta_matrix, transposed_previous_out_with_bias);
// scale by learning rate
Matrix* result = scale(weights_delta_matrix, learning_rate);
matrix_free(previous_out_with_one);
matrix_free(transposed_previous_out_with_bias);
matrix_free(weights_delta_matrix);
return result;
}
Matrix* sigmoid_derivative(Matrix* matrix) {
Matrix* ones = matrix_create(matrix->rows, matrix->columns);
matrix_fill(ones, 1);
Matrix* ones_minus_out = subtract(ones, matrix);
Matrix* sigmoid_derivative = multiply(matrix, ones_minus_out);
matrix_free(ones);
matrix_free(ones_minus_out);
return sigmoid_derivative;
}
double sigmoid(double input) {
return 1.0 / (1 + exp(-1 * input));
}

33
neuronal_network.h Normal file
View file

@ -0,0 +1,33 @@
#include "matrix.h"
#include "image.h"
typedef struct {
int input_size;
int hidden_size;
int hidden_amount;
int output_size;
Matrix** weights;
double learning_rate;
} Neural_Network;
static const int MAX_BYTES = 100;
Neural_Network* new_network(int input_size, int hidden_size, int hidden_amount, int output_size, double learning_rate);
void randomize_network(Neural_Network* network, int scope);
void free_network(Neural_Network* network);
void save_network(Neural_Network* network);
Neural_Network* load_network(char* file);
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);
Matrix* predict_image(Neural_Network* network, Image* image);
void train_network(Neural_Network* network, Image *image, int label);

20
util.c Normal file
View file

@ -0,0 +1,20 @@
//
// Created by jakob on 21.09.2023.
//
#include "util.h"
#include <stdio.h>
void updateBar(int percent_done){
const int BAR_LENGTH = 30;
int numChar = percent_done * BAR_LENGTH / 100;
printf("\r[");
for(int i = 0; i < numChar; i++){
printf("#");
}
for(int i = 0; i < BAR_LENGTH - numChar; i++){
printf(".");
}
printf("] (%d%%)", percent_done);
fflush(stdout);
}

9
util.h Normal file
View file

@ -0,0 +1,9 @@
//
// Created by jakob on 21.09.2023.
//
#ifndef C_NET_UTIL_H
#define C_NET_UTIL_H
#endif //C_NET_UTIL_H
void updateBar(int percentDone);