loss function and support functions

This commit is contained in:
Thomas Schleicher 2023-09-21 12:23:59 +02:00
parent 0f7a0a5f11
commit f18a7714a2
4 changed files with 32 additions and 7 deletions

View file

@ -4,6 +4,11 @@
#include <time.h>
#include <math.h>
double relu(double input);
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){
Neural_Network *network = malloc(sizeof(Neural_Network));
// initialize networks variables
@ -19,9 +24,7 @@ Neural_Network* new_network(int input_size, int hidden_size, int output_size, do
network->bias_1 = matrix_create(hidden_size, 1);
network->bias_2 = matrix_create(hidden_size, 1);
network->bias_3 = matrix_create(hidden_size, 1);
network.bias_output = matrix_create(output_size, 1);
network->bias_output = matrix_create(output_size, 1);
return network;
}
@ -51,7 +54,6 @@ void free_network(Neural_Network* network){
free(network);
}
void save_network(Neural_Network* network) {
char* file_name = "../networks/newest_network.txt";
@ -193,4 +195,19 @@ Matrix* softmax(Matrix* matrix) {
}
}
return result_matrix;
}
double square(double input) {
return input * input;
}
double loss_function(Matrix* output_matrix, int image_label) {
Matrix* temp = matrix_copy(output_matrix);
temp->numbers[1, image_label] -= 1;
apply(square, temp);
matrix_free(temp);
return matrix_sum(temp);;
}