HolyFuckItsAlive #13
3 changed files with 37 additions and 19 deletions
8
main.c
8
main.c
|
|
@ -5,13 +5,15 @@
|
|||
#include "neuronal_network.h"
|
||||
|
||||
int main() {
|
||||
// Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", NULL, 60000);
|
||||
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);
|
||||
// randomize_network(nn, 20);
|
||||
Neural_Network* nn = new_network(28*28, 16, 10, 0.5);
|
||||
randomize_network(nn, 20);
|
||||
// save_network(nn);
|
||||
|
||||
// Neural_Network* nn = load_network("../networks/test1.txt");
|
||||
|
||||
train_network(nn, images[0], 5);
|
||||
|
||||
}
|
||||
|
|
@ -4,9 +4,12 @@
|
|||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
double relu(double input);
|
||||
Matrix* softmax(Matrix* matrix);
|
||||
double sigmoid(double input);
|
||||
Matrix* sigmoidPrime(Matrix* m);
|
||||
|
||||
Matrix* softmax(Matrix* matrix);
|
||||
double square(double input);
|
||||
|
||||
double loss_function(Matrix* output_matrix, int image_label);
|
||||
|
||||
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){
|
||||
|
|
@ -152,13 +155,13 @@ Matrix* predict_image(Neural_Network* network, Image* image){
|
|||
}
|
||||
|
||||
Matrix* predict(Neural_Network* network, Matrix* image_data) {
|
||||
Matrix* hidden1_outputs = apply(relu, add(dot(network->weights_1, image_data), network->bias_1));
|
||||
Matrix* hidden1_outputs = apply(sigmoid, add(dot(network->weights_1, image_data), network->bias_1));
|
||||
|
||||
Matrix* hidden2_outputs = apply(relu, add(dot(network->weights_2, hidden1_outputs), network->bias_2));
|
||||
Matrix* hidden2_outputs = apply(sigmoid, add(dot(network->weights_2, hidden1_outputs), network->bias_2));
|
||||
|
||||
Matrix* hidden3_outputs = apply(relu, add(dot(network->weights_3, hidden2_outputs), network->bias_3));
|
||||
Matrix* hidden3_outputs = apply(sigmoid, add(dot(network->weights_3, hidden2_outputs), network->bias_3));
|
||||
|
||||
Matrix* final_outputs = apply(relu, add(dot(network->weights_output, hidden3_outputs), network->bias_output));
|
||||
Matrix* final_outputs = apply(sigmoid, add(dot(network->weights_output, hidden3_outputs), network->bias_output));
|
||||
|
||||
Matrix* result = softmax(final_outputs);
|
||||
|
||||
|
|
@ -176,18 +179,31 @@ double cost_function(Matrix* calculated, int expected){
|
|||
|
||||
}
|
||||
|
||||
//void train_network(Neural_Network* network, Matrix* input, Matrix* output);
|
||||
//void batch_train_network(Neural_Network* network, Image** images, int size);
|
||||
void train_network(Neural_Network* network, Image *image, int label) {
|
||||
|
||||
Matrix* input = matrix_flatten(image->pixel_values, 0);
|
||||
|
||||
Matrix* hidden1_outputs = apply(sigmoid, add(dot(network->weights_1, input), network->bias_1));
|
||||
Matrix* hidden2_outputs = apply(sigmoid, add(dot(network->weights_2, hidden1_outputs), network->bias_2));
|
||||
Matrix* hidden3_outputs = apply(sigmoid, add(dot(network->weights_3, hidden2_outputs), network->bias_3));
|
||||
Matrix* final_outputs = apply(sigmoid, add(dot(network->weights_output, hidden3_outputs), network->bias_output));
|
||||
|
||||
double relu(double input) {
|
||||
if (input <= 0){
|
||||
return 0.0;
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
double relu_derivative(double x) {
|
||||
return (x > 0) ? 1 : 0;
|
||||
//void batch_train_network(Neural_Network* network, Image** images, int size);
|
||||
|
||||
double sigmoid(double input) {
|
||||
return 1.0 / (1 + exp(-1 * input));
|
||||
}
|
||||
|
||||
Matrix* sigmoidPrime(Matrix* m) {
|
||||
Matrix* ones = matrix_create(m->rows, m->columns);
|
||||
matrix_fill(ones, 1);
|
||||
Matrix* subtracted = subtract(ones, m);
|
||||
Matrix* multiplied = multiply(m, subtracted);
|
||||
matrix_free(ones);
|
||||
matrix_free(subtracted);
|
||||
return multiplied;
|
||||
}
|
||||
|
||||
Matrix* softmax(Matrix* matrix) {
|
||||
|
|
|
|||
|
|
@ -39,5 +39,5 @@ double measure_network_accuracy(Neural_Network* network, Image** images, int amo
|
|||
Matrix* predict_image(Neural_Network* network, Image* image);
|
||||
Matrix* predict(Neural_Network* network, Matrix* image_data);
|
||||
|
||||
void train_network(Neural_Network* network, Matrix* input, Matrix* output);
|
||||
void train_network(Neural_Network* network, Image *image, int label);
|
||||
void batch_train_network(Neural_Network* network, Image** images, int size);
|
||||
|
|
|
|||
Reference in a new issue