Creation of update (.h and .c)

This commit is contained in:
julienChemillier 2022-11-03 16:28:03 +01:00
parent 3a029b3d16
commit 0e317549a5
3 changed files with 192 additions and 79 deletions

View File

@ -6,6 +6,7 @@
#include "include/initialisation.h" #include "include/initialisation.h"
#include "include/function.h" #include "include/function.h"
#include "include/creation.h" #include "include/creation.h"
#include "include/update.h"
#include "include/make.h" #include "include/make.h"
#include "../include/colors.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; a<input_depth; a++) {
for (int b=0; b<output_depth; b++) {
for (int c=0; c<k_size; c++) {
for (int d=0; d<k_size; d++) {
cnn->w[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; a<input_width; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[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; a<input_size; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[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; a<output_depth; a++) {
for (int b=0; b<output_width; b++) {
for (int c=0; c<output_width; c++) {
cnn->bias[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; a<output_width; a++) {
nn->bias[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) { float compute_mean_squared_error(float* output, float* wanted_output, int len) {
if (len==0) { if (len==0) {
printf("Erreur MSE: la longueur de la sortie est de 0 -> division par 0 impossible\n"); printf("Erreur MSE: la longueur de la sortie est de 0 -> division par 0 impossible\n");

26
src/cnn/include/update.h Normal file
View File

@ -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

165
src/cnn/update.c Normal file
View File

@ -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; a<input_depth; a++) {
for (int b=0; b<output_depth; b++) {
for (int c=0; c<k_size; c++) {
for (int d=0; d<k_size; d++) {
cnn->w[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; a<input_width; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[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; a<input_size; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[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; a<output_depth; a++) {
for (int b=0; b<output_width; b++) {
for (int c=0; c<output_width; c++) {
cnn->bias[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; a<output_width; a++) {
nn->bias[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; a<input_depth; a++) {
for (int b=0; b<output_depth; b++) {
for (int c=0; c<k_size; c++) {
for (int d=0; d<k_size; 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; a<input_width; a++) {
for (int b=0; b<output_width; 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; a<input_size; a++) {
for (int b=0; b<output_width; b++) {
nn->d_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; a<output_depth; a++) {
for (int b=0; b<output_width; b++) {
for (int c=0; c<output_width; 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; a<output_width; a++) {
nn->d_bias[a] = 0;
}
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
}
}
}