batch train

This commit is contained in:
Thomas 2023-09-23 20:44:13 +02:00
parent 3557c28392
commit 2cf75cc1ac
2 changed files with 40 additions and 3 deletions

View file

@ -160,7 +160,43 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) {
return output[network->hidden_amount]; return output[network->hidden_amount];
} }
void train_network(Neural_Network* network, Image *image, int label) { void batch_train(Neural_Network* network, Image** images, int amount, int batch_size) {
for (int i = 0; i < amount; ++i) {
Matrix* batch_weights[network->hidden_amount + 1];
for (int j = 0; j < batch_size; ++j) {
Matrix** delta_weights = train_network(network, images[i], images[i]->label);
for (int k = 0; k < network->hidden_amount + 1; k++) {
if(j == 0) {
batch_weights[k] = delta_weights[k];
continue;
}
Matrix* temp_result = add(batch_weights[k], delta_weights[k]);
matrix_free(batch_weights[k]);
matrix_free(delta_weights[k]);
batch_weights[k] = temp_result;
}
free(delta_weights);
}
for (int j = 0; j < network->hidden_amount + 1; ++j) {
Matrix* average_delta_weight = scale(batch_weights[j], (1.0 / batch_size));
apply_weights(network, average_delta_weight, j);
matrix_free(average_delta_weight);
matrix_free(batch_weights[j]);
}
}
}
Matrix ** train_network(Neural_Network* network, Image *image, int label) {
Matrix* image_data = matrix_flatten(image->pixel_values, 0); Matrix* image_data = matrix_flatten(image->pixel_values, 0);
Matrix* input = matrix_add_bias(image_data); Matrix* input = matrix_add_bias(image_data);
@ -181,7 +217,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
// back propagation // back propagation
//list to store the new weights //list to store the new weights
Matrix* delta_weights[network->hidden_amount + 1]; Matrix** delta_weights = malloc(sizeof(Matrix*) * (network->hidden_amount + 1));
// calculate the derivative of the sigmoid function of the input of the result layer // calculate the derivative of the sigmoid function of the input of the result layer
Matrix* sigmoid_prime = sigmoid_derivative(output[network->hidden_amount]); Matrix* sigmoid_prime = sigmoid_derivative(output[network->hidden_amount]);
@ -232,6 +268,7 @@ void train_network(Neural_Network* network, Image *image, int label) {
matrix_free(delta); matrix_free(delta);
matrix_free(previous_delta); matrix_free(previous_delta);
return delta_weights;
} }
Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output) { Matrix* calculate_delta_hidden(Matrix* next_layer_delta, Matrix* weights, Matrix* current_layer_output) {

View file

@ -29,4 +29,4 @@ void print_network(Neural_Network* network);
double measure_network_accuracy(Neural_Network* network, Image** images, int amount); double measure_network_accuracy(Neural_Network* network, Image** images, int amount);
Matrix* predict_image(Neural_Network* network, Image* image); Matrix* predict_image(Neural_Network* network, Image* image);
void train_network(Neural_Network* network, Image *image, int label); Matrix ** train_network(Neural_Network* network, Image *image, int label);