fixed pointer stuff
This commit is contained in:
parent
5774ddc116
commit
86ac3e855c
1 changed files with 28 additions and 23 deletions
|
|
@ -12,7 +12,7 @@ double square(double input);
|
||||||
|
|
||||||
double loss_function(Matrix* output_matrix, int image_label);
|
double loss_function(Matrix* output_matrix, int image_label);
|
||||||
|
|
||||||
void backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matrix* current_layer_activation, Matrix* previous_layer_activation, Matrix* sigma_old);
|
Matrix * backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matrix* current_layer_activation, Matrix* previous_layer_activation, Matrix* sigma_old);
|
||||||
|
|
||||||
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){
|
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){
|
||||||
Neural_Network *network = malloc(sizeof(Neural_Network));
|
Neural_Network *network = malloc(sizeof(Neural_Network));
|
||||||
|
|
@ -226,20 +226,20 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
Matrix* final_outputs = apply(sigmoid, final_add);
|
Matrix* final_outputs = apply(sigmoid, final_add);
|
||||||
|
|
||||||
// begin backpropagation
|
// begin backpropagation
|
||||||
Matrix* sigma = matrix_create(final_outputs->rows, 1);
|
Matrix* sigma1 = matrix_create(final_outputs->rows, 1);
|
||||||
matrix_fill(sigma, 1);
|
matrix_fill(sigma1, 1);
|
||||||
Matrix* temp1 = subtract(sigma, final_outputs);
|
Matrix* temp1 = subtract(sigma1, final_outputs);
|
||||||
Matrix* temp2 = multiply(temp1, final_outputs); // * soll-ist
|
Matrix* temp2 = multiply(temp1, final_outputs); // * soll-ist
|
||||||
Matrix* temp3 = matrix_create(final_outputs->rows, final_outputs->columns);
|
Matrix* temp3 = matrix_create(final_outputs->rows, final_outputs->columns);
|
||||||
matrix_fill(temp3, 0);
|
matrix_fill(temp3, 0);
|
||||||
temp3->numbers[label][0] = 1;
|
temp3->numbers[label][0] = 1;
|
||||||
Matrix* temp4 = subtract(temp3, final_outputs);
|
Matrix* temp4 = subtract(temp3, final_outputs);
|
||||||
sigma = multiply(temp2, temp4);
|
sigma1 = multiply(temp2, temp4);
|
||||||
|
|
||||||
Matrix* temp5 = transpose(h3_outputs);
|
Matrix* temp5 = transpose(h3_outputs);
|
||||||
Matrix* temp6 = dot(sigma, temp5);
|
Matrix* temp6 = dot(sigma1, temp5);
|
||||||
Matrix* weights_delta = scale(temp6, network->learning_rate);
|
Matrix* weights_delta = scale(temp6, network->learning_rate);
|
||||||
Matrix* bias_delta = scale(sigma, network->learning_rate);
|
Matrix* bias_delta = scale(sigma1, network->learning_rate);
|
||||||
|
|
||||||
Matrix* temp7 = add(weights_delta, network->weights_output);
|
Matrix* temp7 = add(weights_delta, network->weights_output);
|
||||||
matrix_free(network->weights_output);
|
matrix_free(network->weights_output);
|
||||||
|
|
@ -250,9 +250,9 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
network->bias_output = temp8;
|
network->bias_output = temp8;
|
||||||
|
|
||||||
// other levels
|
// other levels
|
||||||
backPropagation(network->learning_rate, network->weights_3, network->bias_3, h3_outputs, h2_outputs, sigma);
|
Matrix* sigma2 = backPropagation(network->learning_rate, network->weights_3, network->bias_3, h3_outputs, h2_outputs, sigma1);
|
||||||
backPropagation(network->learning_rate, network->weights_2, network->bias_2, h2_outputs, h1_outputs, sigma);
|
Matrix* sigma3 = backPropagation(network->learning_rate, network->weights_2, network->bias_2, h2_outputs, h1_outputs, sigma2);
|
||||||
backPropagation(network->learning_rate, network->weights_1, network->bias_1, h1_outputs, input, sigma);
|
Matrix* sigma4 = backPropagation(network->learning_rate, network->weights_1, network->bias_1, h1_outputs, input, sigma3);
|
||||||
|
|
||||||
matrix_free(input);
|
matrix_free(input);
|
||||||
|
|
||||||
|
|
@ -275,7 +275,11 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
matrix_free(weights_delta);
|
matrix_free(weights_delta);
|
||||||
matrix_free(bias_delta);
|
matrix_free(bias_delta);
|
||||||
|
|
||||||
matrix_free(sigma);
|
matrix_free(sigma1);
|
||||||
|
matrix_free(sigma2);
|
||||||
|
matrix_free(sigma3);
|
||||||
|
matrix_free(sigma4);
|
||||||
|
|
||||||
|
|
||||||
matrix_free(temp1);
|
matrix_free(temp1);
|
||||||
matrix_free(temp2);
|
matrix_free(temp2);
|
||||||
|
|
@ -287,7 +291,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
||||||
matrix_free(temp8);
|
matrix_free(temp8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matrix* current_layer_activation, Matrix* previous_layer_activation, Matrix* sigma_old) {
|
Matrix * backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matrix* current_layer_activation, Matrix* previous_layer_activation, Matrix* sigma_old) {
|
||||||
Matrix* sigma_new = matrix_create(current_layer_activation->rows, 1);
|
Matrix* sigma_new = matrix_create(current_layer_activation->rows, 1);
|
||||||
matrix_fill(sigma_new, 1);
|
matrix_fill(sigma_new, 1);
|
||||||
|
|
||||||
|
|
@ -311,19 +315,18 @@ void backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matr
|
||||||
Matrix* bias_delta = scale(sigma_new, learning_rate);
|
Matrix* bias_delta = scale(sigma_new, learning_rate);
|
||||||
|
|
||||||
Matrix* temp5 = add(weights_delta, weights);
|
Matrix* temp5 = add(weights_delta, weights);
|
||||||
free(weights->numbers);
|
for (int i = 0; i < weights->rows; ++i) {
|
||||||
weights->numbers = temp5->numbers;
|
for (int j = 0; j < weights->columns; ++j) {
|
||||||
|
weights->numbers[i][j] = temp5->numbers[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Matrix* temp6 = add(bias_delta, biases);
|
Matrix* temp6 = add(bias_delta, biases);
|
||||||
free(biases->numbers);
|
for (int i = 0; i < biases->rows; ++i) {
|
||||||
biases->numbers = temp6->numbers;
|
for (int j = 0; j < biases->columns; ++j) {
|
||||||
|
biases->numbers[i][j] = temp6->numbers[i][j];
|
||||||
sigma_old->rows = sigma_new->rows;
|
}
|
||||||
sigma_old->columns = sigma_new->columns;
|
}
|
||||||
free(sigma_old->numbers);
|
|
||||||
sigma_old->numbers = sigma_new->numbers;
|
|
||||||
|
|
||||||
free(sigma_new);
|
|
||||||
|
|
||||||
matrix_free(temp1);
|
matrix_free(temp1);
|
||||||
matrix_free(temp2);
|
matrix_free(temp2);
|
||||||
|
|
@ -333,6 +336,8 @@ void backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matr
|
||||||
matrix_free(temp6);
|
matrix_free(temp6);
|
||||||
matrix_free(weights_delta);
|
matrix_free(weights_delta);
|
||||||
matrix_free(bias_delta);
|
matrix_free(bias_delta);
|
||||||
|
|
||||||
|
return sigma_new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue