Add of input_z and and fix of issues

This commit is contained in:
julienChemillier 2022-10-31 20:08:42 +01:00
parent d6d69a1acb
commit d5c7c03f82
8 changed files with 78 additions and 44 deletions

View File

@ -41,11 +41,12 @@ void forward_propagation(Network* network) {
int n = network->size;
float*** input;
float*** output;
float*** output_a;
Kernel* k_i;
for (int i=0; i < n-1; i++) {
// Transférer les informations de 'input' à 'output'
k_i = network->kernel[i];
output_a = network->input_a[i+1];
output_a = network->input_z[i+1];
input = network->input[i];
input_depth = network->depth[i];
input_width = network->width[i];
@ -56,7 +57,7 @@ void forward_propagation(Network* network) {
if (k_i->cnn) { // Convolution
make_convolution(k_i->cnn, input, output, output_width);
copy_input_to_input_a(outtput, output_a, outpu_width, output_width, ouput_depth);
copy_input_to_input_z(output, output_a, output_depth, output_width, output_width);
choose_apply_function_matrix(activation, output, output_depth, output_width);
}
else if (k_i->nn) { // Full connection
@ -65,6 +66,7 @@ void forward_propagation(Network* network) {
} else { // Matrice -> Vecteur
make_dense_linearised(k_i->nn, input, output[0][0], input_depth, input_width, output_width);
}
copy_input_to_input_z(output, output_a, 1, 1, output_width);
choose_apply_function_vector(activation, output, output_width);
}
else { // Pooling
@ -74,6 +76,7 @@ void forward_propagation(Network* network) {
} else { // Pooling sur une matrice
make_average_pooling(input, output, activation/100, output_depth, output_width);
}
copy_input_to_input_z(output, output_a, output_depth, output_width, output_width);
}
}
}
@ -82,15 +85,12 @@ void backward_propagation(Network* network, float wanted_number) {
printf_warning("Appel de backward_propagation, incomplet\n");
float* wanted_output = generate_wanted_output(wanted_number);
int n = network->size;
//float loss = compute_mean_squared_error(network->input[n][0][0], wanted_output, network->width[n]);
// -> will it really be used ?
int activation, input_depth, input_width, output_depth, output_width;
float*** input;
float*** output;
Kernel* k_i;
Kernel* k_i_1;
rms_backward(network->input[n-1][0][0], wanted_output); // Backward sur la dernière colonne
// rms_backward(network->input[n-1][0][0], wanted_output); // Backward sur la dernière colonne
for (int i=n-3; i >= 0; i--) {
// Modifie 'k_i' à partir d'une comparaison d'informations entre 'input' et 'output'
@ -114,27 +114,40 @@ void backward_propagation(Network* network, float wanted_number) {
}
} else { // Pooling
backward_2d_pooling(input, output, input_width, output_width, input_depth) // Depth pour input et output a la même valeur
// backward_2d_pooling(input, output, input_width, output_width, input_depth) // Depth pour input et output a la même valeur
}
}
free(wanted_output);
}
void copy_input_to_input_z(float*** output, float*** output_a, int output_depth, int output_rows, int output_columns) {
for (int i=0; i<output_depth; i++) {
for (int j=0; j<output_rows; j++) {
for (int k=0; k<output_columns; k++) {
output_a[i][j][k] = output[i][j][k];
}
}
}
}
void update_weights(Network* network) {
int n = network->size;
int input_depth, input_width, output_width;
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<ouput_depth; b++) {
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];
@ -167,15 +180,18 @@ void update_weights(Network* network) {
void update_bias(Network* network) {
int n = network->size;
int output_width;
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<ouput_depth; a++) {
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];

View File

@ -17,6 +17,7 @@ Network* create_network(int max_size, int learning_rate, int dropout, int initia
network->initialisation = initialisation;
network->size = 1;
network->input = (float****)malloc(sizeof(float***)*max_size);
network->input_z = (float****)malloc(sizeof(float***)*max_size);
network->kernel = (Kernel**)malloc(sizeof(Kernel*)*max_size);
network->width = (int*)malloc(sizeof(int*)*max_size);
network->depth = (int*)malloc(sizeof(int*)*max_size);
@ -28,6 +29,7 @@ Network* create_network(int max_size, int learning_rate, int dropout, int initia
network->kernel[0]->nn = NULL;
network->kernel[0]->cnn = NULL;
create_a_cube_input_layer(network, 0, input_depth, input_dim);
create_a_cube_input_z_layer(network, 0, input_depth, input_dim);
return network;
}
@ -57,6 +59,18 @@ void create_a_cube_input_layer(Network* network, int pos, int depth, int dim) {
network->depth[pos] = depth;
}
void create_a_cube_input_z_layer(Network* network, int pos, int depth, int dim) {
network->input_z[pos] = (float***)malloc(sizeof(float**)*depth);
for (int i=0; i < depth; i++) {
network->input_z[pos][i] = (float**)malloc(sizeof(float*)*dim);
for (int j=0; j < dim; j++) {
network->input_z[pos][i][j] = (float*)malloc(sizeof(float)*dim);
}
}
network->width[pos] = dim;
network->depth[pos] = depth;
}
void create_a_line_input_layer(Network* network, int pos, int dim) {
network->input[pos] = (float***)malloc(sizeof(float**));
network->input[pos][0] = (float**)malloc(sizeof(float*));
@ -65,7 +79,15 @@ void create_a_line_input_layer(Network* network, int pos, int dim) {
network->depth[pos] = 1;
}
void add_2d_average_pooling(Network* network, int dim_ouput) {
void create_a_line_input_z_layer(Network* network, int pos, int dim) {
network->input_z[pos] = (float***)malloc(sizeof(float**));
network->input_z[pos][0] = (float**)malloc(sizeof(float*));
network->input_z[pos][0][0] = (float*)malloc(sizeof(float)*dim);
network->width[pos] = dim;
network->depth[pos] = 1;
}
void add_2d_average_pooling(Network* network, int dim_output) {
int n = network->size;
int k_pos = n-1;
int dim_input = network->width[k_pos];
@ -73,8 +95,8 @@ void add_2d_average_pooling(Network* network, int dim_ouput) {
printf("Impossible de rajouter une couche d'average pooling, le réseau est déjà plein\n");
return;
}
int kernel_size = dim_input/dim_ouput;
if (dim_input%dim_ouput != 0) {
int kernel_size = dim_input/dim_output;
if (dim_input%dim_output != 0) {
printf("Erreur de dimension dans l'average pooling\n");
return;
}
@ -82,6 +104,7 @@ void add_2d_average_pooling(Network* network, int dim_ouput) {
network->kernel[k_pos]->nn = NULL;
network->kernel[k_pos]->activation = 100*kernel_size; // Ne contient pas de fonction d'activation
create_a_cube_input_layer(network, n, network->depth[n-1], network->width[n-1]/2);
create_a_cube_input_z_layer(network, n, network->depth[n-1], network->width[n-1]/2);
network->size++;
}
@ -137,6 +160,7 @@ void add_convolution(Network* network, int depth_output, int dim_output, int act
}
}
create_a_cube_input_layer(network, n, depth_output, bias_size);
create_a_cube_input_z_layer(network, n, depth_output, bias_size);
int n_int = network->width[n-1]*network->width[n-1]*network->depth[n-1];
int n_out = network->width[n]*network->width[n]*network->depth[n];
/* Not currently used
@ -174,6 +198,7 @@ void add_dense(Network* network, int output_units, int activation) {
nn->last_d_weights[i] = (float*)malloc(sizeof(float)*output_units);
}
create_a_line_input_layer(network, n, output_units);
create_a_line_input_z_layer(network, n, output_units);
/* Not currently used
initialisation_1d_matrix(network->initialisation, nn->bias, output_units, output_units+input_units);
initialisation_1d_matrix(ZERO, nn->d_bias, output_units, output_units+input_units);
@ -217,6 +242,7 @@ void add_dense_linearisation(Network* network, int output_units, int activation)
initialisation_2d_matrix(network->initialisation, nn->weights, input_units, output_units, output_units+input_units);
initialisation_2d_matrix(ZERO, nn->d_weights, input_units, output_units, output_units+input_units); */
create_a_line_input_layer(network, n, output_units);
create_a_line_input_z_layer(network, n, output_units);
network->size++;
}

View File

@ -7,16 +7,22 @@ void free_a_cube_input_layer(Network* network, int pos, int depth, int dim) {
for (int i=0; i < depth; i++) {
for (int j=0; j < dim; j++) {
free(network->input[pos][i][j]);
free(network->input_z[pos][i][j]);
}
free(network->input[pos][i]);
free(network->input_z[pos][i]);
}
free(network->input[pos]);
free(network->input_z[pos]);
}
void free_a_line_input_layer(Network* network, int pos) {
free(network->input[pos][0][0]);
free(network->input_z[pos][0][0]);
free(network->input[pos][0]);
free(network->input_z[pos][0]);
free(network->input[pos]);
free(network->input_z[pos]);
}
void free_2d_average_pooling(Network* network, int pos) {
@ -114,6 +120,7 @@ void free_network_creation(Network* network) {
free(network->depth);
free(network->kernel);
free(network->input);
free(network->input_z);
free(network);
}

View File

@ -100,25 +100,3 @@ void choose_apply_function_vector(int activation, float*** input, int dim) {
printf("Erreur, fonction d'activation inconnue (choose_apply_function_vector): %d\n", activation);
}
}
void* get_function_activation(int activation) {
if (activation == RELU) {
return relu;
} else if (activation == -RELU) {
return relu_derivative;
} else if (activation == SIGMOID) {
return sigmoid;
} else if (activation == -SIGMOID) {
return sigmoid_derivative
} else if (activation == SOFTMAX) {
printf("Erreur, impossible de renvoyer la fonction softmax");
} else if (activation == -SOFTMAX) {
printf("Erreur, impossible de renvoyer la dérivée de la fonction softmax");
} else if (activation == TANH) {
return tanh_;
} else if (activation == -TANH) {
return tanh_derivative;
} else {
printf("Erreur, fonction d'activation inconnue (choose_apply_function_vector): %d\n", activation);
}
}

View File

@ -24,6 +24,11 @@ void forward_propagation(Network* network);
*/
void backward_propagation(Network* network, float wanted_number);
/*
* Copie les données de output dans output_a (Sachant que les deux matrices ont les mêmes dimensions)
*/
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
*/
@ -33,7 +38,6 @@ 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)
*/

View File

@ -17,7 +17,12 @@ Network* create_network_lenet5(int learning_rate, int dropout, int activation, i
/*
* Créé et alloue de la mémoire à une couche de type input cube
*/
void create_a_cube_input_layer(Network* network, int pos, int depth, int dim); // CHECKED
void create_a_cube_input_layer(Network* network, int pos, int depth, int dim);
/*
* Créé et alloue de la mémoire à une couche de type input_z cube
*/
void create_a_cube_input_z_layer(Network* network, int pos, int depth, int dim);
/*
* Créé et alloue de la mémoire à une couche de type ligne
@ -27,7 +32,7 @@ void create_a_line_input_layer(Network* network, int pos, int dim);
/*
* Ajoute au réseau une couche d'average pooling valide de dimension dim*dim
*/
void add_2d_average_pooling(Network* network, int dim_ouput);
void add_2d_average_pooling(Network* network, int dim_output);
/*
* Ajoute au réseau une couche de convolution dim*dim et initialise les kernels

View File

@ -1,6 +1,8 @@
#ifndef DEF_FUNCTION_H
#define DEF_FUNCTION_H
typedef float (*returnFunctionType)(float, float);
// Les dérivées sont l'opposé
#define TANH 1
#define SIGMOID 2
@ -44,9 +46,4 @@ void choose_apply_function_matrix(int activation, float*** input, int depth, int
*/
void choose_apply_function_vector(int activation, float*** input, int dim);
/*
* Renvoie un pointeur vers la fonction d'activation correspondante
*/
void* get_function_activation(int activation)
#endif

View File

@ -42,6 +42,7 @@ typedef struct Network{
int* depth; // depth[size]
Kernel** kernel; // kernel[size], contient tous les kernels
float**** input; // Tableau de toutes les couches du réseau input[size][couche->depth][couche->width][couche->width]
float**** input_z; // Même tableau que input mais ne contient paas la dernière fonction d'activation à chaque ligne
} Network;
#endif