Update update.c

This commit is contained in:
augustin64 2022-11-18 14:09:49 +01:00
parent d03f7493b2
commit 5a0f807a00
4 changed files with 28 additions and 25 deletions

View File

@ -34,15 +34,6 @@ void drop_neurones(float*** input, int depth, int dim1, int dim2, int dropout);
*/ */
void copy_input_to_input_z(float*** output, float*** output_a, int output_depth, int output_rows, int output_columns); void copy_input_to_input_z(float*** output, float*** output_a, int output_depth, int output_rows, int output_columns);
/*
* Bascule les données de d_weights dans weights
*/
void update_weights(Network* network);
/*
* Bascule les données de d_bias dans bias
*/
void update_bias(Network* network);
/* /*
* Renvoie l'erreur du réseau neuronal pour une sortie (RMS) * Renvoie l'erreur du réseau neuronal pour une sortie (RMS)
*/ */

View File

@ -7,13 +7,13 @@
* Met à jours les poids à partir de données obtenus après plusieurs backpropagations * Met à jours les poids à partir de données obtenus après plusieurs backpropagations
* Puis met à 0 tous les d_weights * Puis met à 0 tous les d_weights
*/ */
void update_weights(Network* network); void update_weights(Network* network, Network* d_network);
/* /*
* Met à jours les biais à partir de données obtenus après plusieurs backpropagations * Met à jours les biais à partir de données obtenus après plusieurs backpropagations
* Puis met à 0 tous les d_bias * Puis met à 0 tous les d_bias
*/ */
void update_bias(Network* network); void update_bias(Network* network, Network* d_network);
/* /*
* Met à 0 toutes les données de backpropagation de poids * Met à 0 toutes les données de backpropagation de poids

View File

@ -195,10 +195,11 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
accuracy += train_params->accuracy / (float) nb_images_total; accuracy += train_params->accuracy / (float) nb_images_total;
current_accuracy = accuracy * nb_images_total/(j*BATCHES); current_accuracy = accuracy * nb_images_total/(j*BATCHES);
update_weights(network); update_weights(network, network);
update_bias(network); update_bias(network, network);
printf("\rÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: "YELLOW"%0.1f%%"RESET" ", i, epochs, BATCHES*(j+1), nb_images_total, current_accuracy*100); printf("\rÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: "YELLOW"%0.1f%%"RESET" ", i, epochs, BATCHES*(j+1), nb_images_total, current_accuracy*100);
fflush(stdout);
#endif #endif
} }
#ifdef USE_MULTITHREADING #ifdef USE_MULTITHREADING

View File

@ -1,13 +1,16 @@
#include <stdio.h>
#include "include/update.h" #include "include/update.h"
#include "include/struct.h" #include "include/struct.h"
void update_weights(Network* network) { void update_weights(Network* network, Network* d_network) {
int n = network->size; int n = network->size;
int input_depth, input_width, output_depth, output_width, k_size; int input_depth, input_width, output_depth, output_width, k_size;
Kernel* k_i; Kernel* k_i;
Kernel* dk_i;
for (int i=0; i<(n-1); i++) { for (int i=0; i<(n-1); i++) {
k_i = network->kernel[i]; k_i = network->kernel[i];
dk_i = d_network->kernel[i];
input_depth = network->depth[i]; input_depth = network->depth[i];
input_width = network->width[i]; input_width = network->width[i];
output_depth = network->depth[i+1]; output_depth = network->depth[i+1];
@ -15,13 +18,14 @@ void update_weights(Network* network) {
if (k_i->cnn) { // Convolution if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i->cnn; Kernel_cnn* cnn = k_i->cnn;
Kernel_cnn* d_cnn = dk_i->cnn;
k_size = cnn->k_size; k_size = cnn->k_size;
for (int a=0; a<input_depth; a++) { for (int a=0; a<input_depth; a++) {
for (int b=0; b<output_depth; b++) { for (int b=0; b<output_depth; b++) {
for (int c=0; c<k_size; c++) { for (int c=0; c<k_size; c++) {
for (int d=0; d<k_size; d++) { for (int d=0; d<k_size; d++) {
cnn->w[a][b][c][d] -= network->learning_rate * cnn->d_w[a][b][c][d]; cnn->w[a][b][c][d] -= network->learning_rate * d_cnn->d_w[a][b][c][d];
cnn->d_w[a][b][c][d] = 0; d_cnn->d_w[a][b][c][d] = 0;
} }
} }
} }
@ -29,19 +33,21 @@ void update_weights(Network* network) {
} else if (k_i->nn) { // Full connection } else if (k_i->nn) { // Full connection
if (input_depth==1) { // Vecteur -> Vecteur if (input_depth==1) { // Vecteur -> Vecteur
Kernel_nn* nn = k_i->nn; Kernel_nn* nn = k_i->nn;
Kernel_nn* d_nn = dk_i->nn;
for (int a=0; a<input_width; a++) { for (int a=0; a<input_width; a++) {
for (int b=0; b<output_width; b++) { for (int b=0; b<output_width; b++) {
nn->weights[a][b] -= network->learning_rate * nn->d_weights[a][b]; nn->weights[a][b] -= network->learning_rate * d_nn->d_weights[a][b];
nn->d_weights[a][b] = 0; d_nn->d_weights[a][b] = 0;
} }
} }
} else { // Matrice -> vecteur } else { // Matrice -> vecteur
Kernel_nn* nn = k_i->nn; Kernel_nn* nn = k_i->nn;
Kernel_nn* d_nn = dk_i->nn;
int input_size = input_width*input_width*input_depth; int input_size = input_width*input_width*input_depth;
for (int a=0; a<input_size; a++) { for (int a=0; a<input_size; a++) {
for (int b=0; b<output_width; b++) { for (int b=0; b<output_width; b++) {
nn->weights[a][b] -= network->learning_rate * nn->d_weights[a][b]; nn->weights[a][b] -= network->learning_rate * d_nn->d_weights[a][b];
nn->d_weights[a][b] = 0; d_nn->d_weights[a][b] = 0;
} }
} }
} }
@ -51,30 +57,35 @@ void update_weights(Network* network) {
} }
} }
void update_bias(Network* network) { void update_bias(Network* network, Network* d_network) {
int n = network->size; int n = network->size;
int output_width, output_depth; int output_width, output_depth;
Kernel* k_i; Kernel* k_i;
Kernel* dk_i;
for (int i=0; i<(n-1); i++) { for (int i=0; i<(n-1); i++) {
k_i = network->kernel[i]; k_i = network->kernel[i];
dk_i = d_network->kernel[i];
output_width = network->width[i+1]; output_width = network->width[i+1];
output_depth = network->depth[i+1]; output_depth = network->depth[i+1];
if (k_i->cnn) { // Convolution if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i->cnn; Kernel_cnn* cnn = k_i->cnn;
Kernel_cnn* d_cnn = dk_i->cnn;
for (int a=0; a<output_depth; a++) { for (int a=0; a<output_depth; a++) {
for (int b=0; b<output_width; b++) { for (int b=0; b<output_width; b++) {
for (int c=0; c<output_width; c++) { for (int c=0; c<output_width; c++) {
cnn->bias[a][b][c] -= network->learning_rate * cnn->d_bias[a][b][c]; cnn->bias[a][b][c] -= network->learning_rate * d_cnn->d_bias[a][b][c];
cnn->d_bias[a][b][c] = 0; d_cnn->d_bias[a][b][c] = 0;
} }
} }
} }
} else if (k_i->nn) { // Full connection } else if (k_i->nn) { // Full connection
Kernel_nn* nn = k_i->nn; Kernel_nn* nn = k_i->nn;
Kernel_nn* d_nn = dk_i->nn;
for (int a=0; a<output_width; a++) { for (int a=0; a<output_width; a++) {
nn->bias[a] -= network->learning_rate * nn->d_bias[a]; nn->bias[a] -= network->learning_rate * d_nn->d_bias[a];
nn->d_bias[a] = 0; d_nn->d_bias[a] = 0;
} }
} else { // Pooling } else { // Pooling
(void)0; // Ne rien faire pour la couche pooling (void)0; // Ne rien faire pour la couche pooling