diff --git a/src/cnn/cnn.c b/src/cnn/cnn.c index 03daa07..7581005 100644 --- a/src/cnn/cnn.c +++ b/src/cnn/cnn.c @@ -6,6 +6,7 @@ #include "include/initialisation.h" #include "include/function.h" #include "include/creation.h" +#include "include/update.h" #include "include/make.h" #include "../include/colors.h" @@ -130,85 +131,6 @@ void copy_input_to_input_z(float*** output, float*** output_a, int output_depth, } } -void update_weights(Network* network) { - int n = network->size; - int input_depth, input_width, output_depth, output_width; - Kernel* k_i; - Kernel* k_i_1; - for (int i=0; i<(n-1); i++) { - k_i = network->kernel[i]; - k_i_1 = network->kernel[i+1]; - input_depth = network->depth[i]; - input_width = network->width[i]; - output_depth = network->depth[i+1]; - output_width = network->width[i+1]; - - if (k_i->cnn) { // Convolution - Kernel_cnn* cnn = k_i_1->cnn; - int k_size = cnn->k_size; - for (int a=0; aw[a][b][c][d] += cnn->d_w[a][b][c][d]; - } - } - } - } - } else if (k_i->nn) { // Full connection - if (input_depth==1) { // Vecteur -> Vecteur - Kernel_nn* nn = k_i_1->nn; - for (int a=0; aweights[a][b] += nn->d_weights[a][b]; - } - } - } else { // Matrice -> vecteur - Kernel_nn* nn = k_i_1->nn; - int input_size = input_width*input_width*input_depth; - for (int a=0; aweights[a][b] += nn->d_weights[a][b]; - } - } - } - } else { // Pooling - (void)0; // Ne rien faire pour la couche pooling - } - } -} - -void update_bias(Network* network) { - int n = network->size; - int output_width, output_depth; - Kernel* k_i; - Kernel* k_i_1; - for (int i=0; i<(n-1); i++) { - k_i = network->kernel[i]; - k_i_1 = network->kernel[i+1]; - output_width = network->width[i+1]; - output_depth = network->depth[i+1]; - - if (k_i->cnn) { // Convolution - Kernel_cnn* cnn = k_i_1->cnn; - for (int a=0; abias[a][b][c] += cnn->d_bias[a][b][c]; - } - } - } - } else if (k_i->nn) { // Full connection - Kernel_nn* nn = k_i_1->nn; - for (int a=0; abias[a] += nn->d_bias[a]; - } - } else { // Pooling - (void)0; // Ne rien faire pour la couche pooling - } - } -} - float compute_mean_squared_error(float* output, float* wanted_output, int len) { if (len==0) { printf("Erreur MSE: la longueur de la sortie est de 0 -> division par 0 impossible\n"); diff --git a/src/cnn/include/update.h b/src/cnn/include/update.h new file mode 100644 index 0000000..345060f --- /dev/null +++ b/src/cnn/include/update.h @@ -0,0 +1,26 @@ +#ifndef DEF_UPDATE_H +#define DEF_UPDATE_H + +/* +* Met à jours les poids à partir de données obtenus après plusieurs backpropagations +* Puis met à 0 tous les d_weights +*/ +void update_weights(Network* network); + +/* +* Met à jours les biais à partir de données obtenus après plusieurs backpropagations +* Puis met à 0 tous les d_bias +*/ +void update_bias(Network* network); + +/* +* Met à 0 toutes les données de backpropagation de poids +*/ +void reset_d_weights(Network* network); + +/* +* Met à 0 toutes les données de backpropagation de biais +*/ +void reset_d_bias(Network* network); + +#endif \ No newline at end of file diff --git a/src/cnn/update.c b/src/cnn/update.c new file mode 100644 index 0000000..aab9c8f --- /dev/null +++ b/src/cnn/update.c @@ -0,0 +1,165 @@ + +#include "update.h" + +void update_weights(Network* network) { + int n = network->size; + int input_depth, input_width, output_depth, output_width; + Kernel* k_i; + Kernel* k_i_1; + for (int i=0; i<(n-1); i++) { + k_i = network->kernel[i]; + k_i_1 = network->kernel[i+1]; + input_depth = network->depth[i]; + input_width = network->width[i]; + output_depth = network->depth[i+1]; + output_width = network->width[i+1]; + + if (k_i->cnn) { // Convolution + Kernel_cnn* cnn = k_i_1->cnn; + int k_size = cnn->k_size; + for (int a=0; aw[a][b][c][d] += cnn->d_w[a][b][c][d]; + cnn->d_w[a][b][c][d] = 0; + } + } + } + } + } else if (k_i->nn) { // Full connection + if (input_depth==1) { // Vecteur -> Vecteur + Kernel_nn* nn = k_i_1->nn; + for (int a=0; aweights[a][b] += nn->d_weights[a][b]; + nn->d_weights[a][b] = 0; + } + } + } else { // Matrice -> vecteur + Kernel_nn* nn = k_i_1->nn; + int input_size = input_width*input_width*input_depth; + for (int a=0; aweights[a][b] += nn->d_weights[a][b]; + nn->d_weights[a][b] = 0; + } + } + } + } else { // Pooling + (void)0; // Ne rien faire pour la couche pooling + } + } +} + +void update_bias(Network* network) { + int n = network->size; + int output_width, output_depth; + Kernel* k_i; + Kernel* k_i_1; + for (int i=0; i<(n-1); i++) { + k_i = network->kernel[i]; + k_i_1 = network->kernel[i+1]; + output_width = network->width[i+1]; + output_depth = network->depth[i+1]; + + if (k_i->cnn) { // Convolution + Kernel_cnn* cnn = k_i_1->cnn; + for (int a=0; abias[a][b][c] += cnn->d_bias[a][b][c]; + cnn->d_bias[a][b][c] = 0; + } + } + } + } else if (k_i->nn) { // Full connection + Kernel_nn* nn = k_i_1->nn; + for (int a=0; abias[a] += nn->d_bias[a]; + nn->d_bias[a] = 0; + } + } else { // Pooling + (void)0; // Ne rien faire pour la couche pooling + } + } +} + +void reset_d_weights(Network* network) { + int n = network->size; + int input_depth, input_width, output_depth, output_width; + Kernel* k_i; + Kernel* k_i_1; + for (int i=0; i<(n-1); i++) { + k_i = network->kernel[i]; + k_i_1 = network->kernel[i+1]; + input_depth = network->depth[i]; + input_width = network->width[i]; + output_depth = network->depth[i+1]; + output_width = network->width[i+1]; + + if (k_i->cnn) { // Convolution + Kernel_cnn* cnn = k_i_1->cnn; + int k_size = cnn->k_size; + for (int a=0; ad_w[a][b][c][d] = 0; + } + } + } + } + } else if (k_i->nn) { // Full connection + if (input_depth==1) { // Vecteur -> Vecteur + Kernel_nn* nn = k_i_1->nn; + for (int a=0; ad_weights[a][b] = 0; + } + } + } else { // Matrice -> vecteur + Kernel_nn* nn = k_i_1->nn; + int input_size = input_width*input_width*input_depth; + for (int a=0; ad_weights[a][b] = 0; + } + } + } + } else { // Pooling + (void)0; // Ne rien faire pour la couche pooling + } + } +} + +void reset_d_bias(Network* network) { + int n = network->size; + int output_width, output_depth; + Kernel* k_i; + Kernel* k_i_1; + for (int i=0; i<(n-1); i++) { + k_i = network->kernel[i]; + k_i_1 = network->kernel[i+1]; + output_width = network->width[i+1]; + output_depth = network->depth[i+1]; + + if (k_i->cnn) { // Convolution + Kernel_cnn* cnn = k_i_1->cnn; + for (int a=0; ad_bias[a][b][c] = 0; + } + } + } + } else if (k_i->nn) { // Full connection + Kernel_nn* nn = k_i_1->nn; + for (int a=0; ad_bias[a] = 0; + } + } else { // Pooling + (void)0; // Ne rien faire pour la couche pooling + } + } +} \ No newline at end of file