fixed pointer stuff
This commit is contained in:
parent
88ac566fcf
commit
5774ddc116
1 changed files with 23 additions and 22 deletions
|
|
@ -12,7 +12,7 @@ double square(double input);
|
|||
|
||||
double loss_function(Matrix* output_matrix, int image_label);
|
||||
|
||||
Matrix* backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matrix* current_layer_activation, Matrix* previous_layer_activation, Matrix* sigma_old);
|
||||
void 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 *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);
|
||||
|
||||
// begin backpropagation
|
||||
Matrix* sigma1 = matrix_create(final_outputs->rows, 1);
|
||||
matrix_fill(sigma1, 1);
|
||||
Matrix* temp1 = subtract(sigma1, final_outputs);
|
||||
Matrix* sigma = matrix_create(final_outputs->rows, 1);
|
||||
matrix_fill(sigma, 1);
|
||||
Matrix* temp1 = subtract(sigma, final_outputs);
|
||||
Matrix* temp2 = multiply(temp1, final_outputs); // * soll-ist
|
||||
Matrix* temp3 = matrix_create(final_outputs->rows, final_outputs->columns);
|
||||
matrix_fill(temp3, 0);
|
||||
temp3->numbers[label][0] = 1;
|
||||
Matrix* temp4 = subtract(temp3, final_outputs);
|
||||
sigma1 = multiply(temp2, temp4);
|
||||
sigma = multiply(temp2, temp4);
|
||||
|
||||
Matrix* temp5 = transpose(h3_outputs);
|
||||
Matrix* temp6 = dot(sigma1, temp5);
|
||||
Matrix* temp6 = dot(sigma, temp5);
|
||||
Matrix* weights_delta = scale(temp6, network->learning_rate);
|
||||
Matrix* bias_delta = scale(sigma1, network->learning_rate);
|
||||
Matrix* bias_delta = scale(sigma, network->learning_rate);
|
||||
|
||||
Matrix* temp7 = add(weights_delta, network->weights_output);
|
||||
matrix_free(network->weights_output);
|
||||
|
|
@ -250,10 +250,9 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
|||
network->bias_output = temp8;
|
||||
|
||||
// other levels
|
||||
Matrix* sigma2 = backPropagation(network->learning_rate, network->weights_3, network->bias_3, h3_outputs, h2_outputs, sigma1);
|
||||
Matrix* sigma3 = backPropagation(network->learning_rate, network->weights_2, network->bias_2, h2_outputs, h1_outputs, sigma2);
|
||||
|
||||
Matrix* sigma4 = backPropagation(network->learning_rate, network->weights_1, network->bias_1, h1_outputs, input, sigma3);
|
||||
backPropagation(network->learning_rate, network->weights_3, network->bias_3, h3_outputs, h2_outputs, sigma);
|
||||
backPropagation(network->learning_rate, network->weights_2, network->bias_2, h2_outputs, h1_outputs, sigma);
|
||||
backPropagation(network->learning_rate, network->weights_1, network->bias_1, h1_outputs, input, sigma);
|
||||
|
||||
matrix_free(input);
|
||||
|
||||
|
|
@ -276,10 +275,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
|||
matrix_free(weights_delta);
|
||||
matrix_free(bias_delta);
|
||||
|
||||
matrix_free(sigma1);
|
||||
matrix_free(sigma2);
|
||||
matrix_free(sigma3);
|
||||
matrix_free(sigma4);
|
||||
matrix_free(sigma);
|
||||
|
||||
matrix_free(temp1);
|
||||
matrix_free(temp2);
|
||||
|
|
@ -291,7 +287,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
|
|||
matrix_free(temp8);
|
||||
}
|
||||
|
||||
Matrix* backPropagation(double learning_rate, Matrix* weights, Matrix* biases, Matrix* current_layer_activation, Matrix* previous_layer_activation, Matrix* sigma_old) {
|
||||
void 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_fill(sigma_new, 1);
|
||||
|
||||
|
|
@ -315,12 +311,19 @@ Matrix* backPropagation(double learning_rate, Matrix* weights, Matrix* biases, M
|
|||
Matrix* bias_delta = scale(sigma_new, learning_rate);
|
||||
|
||||
Matrix* temp5 = add(weights_delta, weights);
|
||||
matrix_free(weights);
|
||||
weights = temp5;
|
||||
free(weights->numbers);
|
||||
weights->numbers = temp5->numbers;
|
||||
|
||||
Matrix* temp6 = add(bias_delta, biases);
|
||||
matrix_free(biases);
|
||||
biases = temp6;
|
||||
free(biases->numbers);
|
||||
biases->numbers = temp6->numbers;
|
||||
|
||||
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(temp2);
|
||||
|
|
@ -330,8 +333,6 @@ Matrix* backPropagation(double learning_rate, Matrix* weights, Matrix* biases, M
|
|||
matrix_free(temp6);
|
||||
matrix_free(weights_delta);
|
||||
matrix_free(bias_delta);
|
||||
|
||||
return sigma_new;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Reference in a new issue