HolyFuckItsAlive #13
5 changed files with 42 additions and 26 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -56,3 +56,5 @@ 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
|
||||
|
|
|
|||
20
main.c
20
main.c
|
|
@ -5,25 +5,13 @@
|
|||
#include "neuronal_network.h"
|
||||
|
||||
int main() {
|
||||
// Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 2);
|
||||
// img_visualize(images[1]);
|
||||
// Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 60000);
|
||||
// img_visualize(images[4]);
|
||||
|
||||
// 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);
|
||||
//
|
||||
// randomize_network(nn, 20);
|
||||
// save_network(nn);
|
||||
|
||||
Neural_Network* nn = load_network("../networks/test1.txt");
|
||||
// Neural_Network* nn = load_network("../networks/test1.txt");
|
||||
|
||||
}
|
||||
30
matrix.c
30
matrix.c
|
|
@ -2,8 +2,10 @@
|
|||
#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) {
|
||||
|
||||
|
|
@ -17,7 +19,7 @@ Matrix* matrix_create(int rows, int columns) {
|
|||
// allocate memory for the numbers (2D-Array)
|
||||
matrix->numbers = malloc(sizeof(double*) * rows);
|
||||
for (int i = 0; i < rows; i++) {
|
||||
matrix->numbers[i] = malloc(sizeof(double) * columns);
|
||||
matrix->numbers[i] = calloc(sizeof(double), columns);
|
||||
}
|
||||
|
||||
// return the pointer to the allocated memory
|
||||
|
|
@ -189,7 +191,8 @@ Matrix* apply(double (*function)(double), Matrix* matrix) {
|
|||
// 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++) {
|
||||
matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]);
|
||||
result_matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]);
|
||||
int k = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -246,7 +249,6 @@ Matrix* transpose(Matrix* matrix) {
|
|||
|
||||
}
|
||||
|
||||
//file operations
|
||||
void matrix_save(Matrix* matrix, char* file_string){
|
||||
|
||||
// open the file in append mode
|
||||
|
|
@ -274,30 +276,40 @@ void matrix_save(Matrix* matrix, char* file_string){
|
|||
}
|
||||
|
||||
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, fptr);
|
||||
fgets(buffer, MAX_BYTES, save_file);
|
||||
int rows = (int)strtol(buffer, NULL, 10);
|
||||
fgets(buffer, MAX_BYTES, fptr);
|
||||
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, fptr);
|
||||
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;
|
||||
|
|
@ -340,6 +352,10 @@ int matrix_argmax(Matrix* matrix) {
|
|||
|
||||
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);
|
||||
|
|
|
|||
2
matrix.h
2
matrix.h
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
int rows, columns;
|
||||
|
|
@ -15,6 +16,7 @@ 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);
|
||||
|
|
|
|||
|
|
@ -115,8 +115,17 @@ Neural_Network* load_network(char* file) {
|
|||
// create a new network to fill with the saved data
|
||||
Neural_Network* saved_network = new_network(input_size, hidden_size, output_size, 0);
|
||||
|
||||
// load matrices from file into struct
|
||||
saved_network->bias_1 = load_next_matrix(save_file);
|
||||
saved_network->weights_1 = load_next_matrix(save_file);
|
||||
saved_network->bias_2 = load_next_matrix(save_file);
|
||||
saved_network->weights_2 = load_next_matrix(save_file);
|
||||
saved_network->bias_3 = load_next_matrix(save_file);
|
||||
saved_network->weights_3 = load_next_matrix(save_file);
|
||||
saved_network->weights_output = load_next_matrix(save_file);
|
||||
|
||||
|
||||
// return saved network
|
||||
fclose(save_file);
|
||||
return saved_network;
|
||||
}
|
||||
|
||||
|
|
@ -162,11 +171,10 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) {
|
|||
//void batch_train_network(Neural_Network* network, Image** images, int size);
|
||||
|
||||
double relu(double input) {
|
||||
if (input < 0){
|
||||
if (input <= 0){
|
||||
return 0.0;
|
||||
}
|
||||
return input;
|
||||
//TODO: relu formel
|
||||
}
|
||||
|
||||
Matrix* softmax(Matrix* matrix) {
|
||||
|
|
|
|||
Reference in a new issue