Change network->kernel definition

This commit is contained in:
augustin64 2022-09-10 17:17:49 +02:00
parent e280d3e9da
commit 4720fb18e1
3 changed files with 68 additions and 69 deletions

View File

@ -33,32 +33,32 @@ void forward_propagation(Network* network) {
int output_dim, output_depth; int output_dim, output_depth;
float*** output; float*** output;
for (int i=0; i < network->size-1; i++) { for (int i=0; i < network->size-1; i++) {
if (network->kernel[i].nn==NULL && network->kernel[i].cnn!=NULL) { //CNN if (network->kernel[i]->nn==NULL && network->kernel[i]->cnn!=NULL) { //CNN
output = network->input[i+1]; output = network->input[i+1];
output_dim = network->dim[i+1][0]; output_dim = network->dim[i+1][0];
output_depth = network->dim[i+1][1]; output_depth = network->dim[i+1][1];
make_convolution(network->input[i], network->kernel[i].cnn, output, output_dim); make_convolution(network->input[i], network->kernel[i]->cnn, output, output_dim);
choose_apply_function_input(network->kernel[i].activation, output, output_depth, output_dim, output_dim); choose_apply_function_input(network->kernel[i]->activation, output, output_depth, output_dim, output_dim);
} }
else if (network->kernel[i].nn!=NULL && network->kernel[i].cnn==NULL) { //NN else if (network->kernel[i]->nn!=NULL && network->kernel[i]->cnn==NULL) { //NN
make_fully_connected(network->input[i][0][0], network->kernel[i].nn, network->input[i+1][0][0], network->dim[i][0], network->dim[i+1][0]); make_fully_connected(network->input[i][0][0], network->kernel[i]->nn, network->input[i+1][0][0], network->dim[i][0], network->dim[i+1][0]);
choose_apply_function_input(network->kernel[i].activation, network->input[i+1], 1, 1, network->dim[i+1][0]); choose_apply_function_input(network->kernel[i]->activation, network->input[i+1], 1, 1, network->dim[i+1][0]);
} }
else { //Pooling else { //Pooling
if (network->size-2==i) { if (network->size-2==i) {
printf("Le réseau ne peut pas finir par une pooling layer"); printf("Le réseau ne peut pas finir par une pooling layer");
return; return;
} }
if (network->kernel[i+1].nn!=NULL && network->kernel[i+1].cnn==NULL) { if (network->kernel[i+1]->nn!=NULL && network->kernel[i+1]->cnn==NULL) {
make_average_pooling_flattened(network->input[i], network->input[i+1][0][0], network->kernel[i].activation/100, network->dim[i][1], network->dim[i][0]); make_average_pooling_flattened(network->input[i], network->input[i+1][0][0], network->kernel[i]->activation/100, network->dim[i][1], network->dim[i][0]);
choose_apply_function_input(network->kernel[i].activation%100, network->input[i+1], 1, 1, network->dim[i+1][0]); choose_apply_function_input(network->kernel[i]->activation%100, network->input[i+1], 1, 1, network->dim[i+1][0]);
} }
else if (network->kernel[i+1].nn==NULL && network->kernel[i+1].cnn!=NULL) { else if (network->kernel[i+1]->nn==NULL && network->kernel[i+1]->cnn!=NULL) {
make_average_pooling(network->input[i], network->input[i+1], network->kernel[i].activation/100, network->dim[i+1][1], network->dim[i+1][0]); make_average_pooling(network->input[i], network->input[i+1], network->kernel[i]->activation/100, network->dim[i+1][1], network->dim[i+1][0]);
choose_apply_function_input(network->kernel[i].activation%100, network->input[i+1], network->dim[i+1][1], network->dim[i+1][0], network->dim[i+1][0]); choose_apply_function_input(network->kernel[i]->activation%100, network->input[i+1], network->dim[i+1][1], network->dim[i+1][0], network->dim[i+1][0]);
} }
else { else {
printf("Le réseau ne peut pas contenir deux poolings layers collées"); printf("Le réseau ne peut pas contenir deux pooling layers collées");
return; return;
} }
} }
@ -71,7 +71,7 @@ void backward_propagation(Network* network, float wanted_number) {
float loss = compute_cross_entropy_loss(network->input[n][0][0], wanted_output, network->dim[n][0]); float loss = compute_cross_entropy_loss(network->input[n][0][0], wanted_output, network->dim[n][0]);
for (int i=n; i >= 0; i--) { for (int i=n; i >= 0; i--) {
if (i==n) { if (i==n) {
if (network->kernel[i].activation == SOFTMAX) { if (network->kernel[i]->activation == SOFTMAX) {
int l2 = network->dim[i][0]; // Taille de la dernière couche int l2 = network->dim[i][0]; // Taille de la dernière couche
int l1 = network->dim[i-1][0]; int l1 = network->dim[i-1][0];
for (int j=0; j < l2; j++) { for (int j=0; j < l2; j++) {
@ -79,18 +79,18 @@ void backward_propagation(Network* network, float wanted_number) {
} }
} }
else { else {
printf("Erreur, seule la fonction softmax est implémentée pour la dernière couche"); printf("Erreur, seule la fonction SOFTMAX est implémentée pour la dernière couche");
return; return;
} }
} }
else { else {
if (network->kernel[i].activation == SIGMOID) { if (network->kernel[i]->activation == SIGMOID) {
} }
else if (network->kernel[i].activation == TANH) { else if (network->kernel[i]->activation == TANH) {
} }
else if (network->kernel[i].activation == RELU) { else if (network->kernel[i]->activation == RELU) {
} }
} }
@ -127,8 +127,7 @@ float* generate_wanted_output(float wanted_number) {
} }
int main() { int main() {
Network* network; Network* network = create_network_lenet5(0, TANH, GLOROT_NORMAL);
network = create_network_lenet5(0, TANH, GLOROT_NORMAL);
forward_propagation(network); forward_propagation(network);
return 0; return 0;
} }

View File

@ -14,10 +14,11 @@ Network* create_network(int max_size, int dropout, int initialisation, int input
network->initialisation = initialisation; network->initialisation = initialisation;
network->size = 1; network->size = 1;
network->input = (float****)malloc(sizeof(float***)*max_size); network->input = (float****)malloc(sizeof(float***)*max_size);
network->kernel = (Kernel*)malloc(sizeof(Kernel)*(max_size-1)); network->kernel = (Kernel**)malloc(sizeof(Kernel*)*(max_size-1));
network->dim = (int**)malloc(sizeof(int*)*max_size); network->dim = (int**)malloc(sizeof(int*)*max_size);
for (int i=0; i < max_size; i++) { for (int i=0; i < max_size; i++) {
network->dim[i] = (int*)malloc(sizeof(int)*2); network->dim[i] = (int*)malloc(sizeof(int)*2);
network->kernel[i] = (Kernel*)malloc(sizeof(Kernel));
} }
network->dim[0][0] = input_dim; network->dim[0][0] = input_dim;
network->dim[0][1] = input_depth; network->dim[0][1] = input_depth;
@ -26,8 +27,7 @@ Network* create_network(int max_size, int dropout, int initialisation, int input
} }
Network* create_network_lenet5(int dropout, int activation, int initialisation) { Network* create_network_lenet5(int dropout, int activation, int initialisation) {
Network* network; Network* network = create_network(8, dropout, initialisation, 32, 1);
network = create_network(8, dropout, initialisation, 32, 1);
add_convolution(network, 6, 5, activation); add_convolution(network, 6, 5, activation);
add_average_pooling(network, 2, activation); add_average_pooling(network, 2, activation);
add_convolution(network, 16, 5, activation); add_convolution(network, 16, 5, activation);
@ -62,9 +62,9 @@ void add_average_pooling(Network* network, int kernel_size, int activation) {
printf("Impossible de rajouter une couche d'average pooling, le réseau est déjà plein\n"); printf("Impossible de rajouter une couche d'average pooling, le réseau est déjà plein\n");
return; return;
} }
network->kernel[n].cnn = NULL; network->kernel[n]->cnn = NULL;
network->kernel[n].nn = NULL; network->kernel[n]->nn = NULL;
network->kernel[n].activation = activation + 100*kernel_size; network->kernel[n]->activation = activation + 100*kernel_size;
create_a_cube_input_layer(network, n, network->dim[n-1][1], network->dim[n-1][0]/2); create_a_cube_input_layer(network, n, network->dim[n-1][1], network->dim[n-1][0]/2);
network->size++; network->size++;
} }
@ -75,9 +75,9 @@ void add_average_pooling_flatten(Network* network, int kernel_size, int activati
printf("Impossible de rajouter une couche d'average pooling, le réseau est déjà plein\n"); printf("Impossible de rajouter une couche d'average pooling, le réseau est déjà plein\n");
return; return;
} }
network->kernel[n].cnn = NULL; network->kernel[n]->cnn = NULL;
network->kernel[n].nn = NULL; network->kernel[n]->nn = NULL;
network->kernel[n].activation = activation + 100*kernel_size; network->kernel[n]->activation = activation + 100*kernel_size;
int dim = (network->dim[n-1][0]*network->dim[n-1][0]*network->dim[n-1][1])/(kernel_size*kernel_size); int dim = (network->dim[n-1][0]*network->dim[n-1][0]*network->dim[n-1][1])/(kernel_size*kernel_size);
create_a_line_input_layer(network, n, dim); create_a_line_input_layer(network, n, dim);
network->size++; network->size++;
@ -91,43 +91,43 @@ void add_convolution(Network* network, int nb_filter, int kernel_size, int activ
} }
int r = network->dim[n-1][1]; int r = network->dim[n-1][1];
int c = nb_filter; int c = nb_filter;
network->kernel[n].nn = NULL; network->kernel[n]->nn = NULL;
network->kernel[n].cnn = (Kernel_cnn*)malloc(sizeof(Kernel_cnn)); network->kernel[n]->cnn = (Kernel_cnn*)malloc(sizeof(Kernel_cnn));
network->kernel[n].activation = activation; network->kernel[n]->activation = activation;
network->kernel[n].cnn->k_size = kernel_size; network->kernel[n]->cnn->k_size = kernel_size;
network->kernel[n].cnn->rows = r; network->kernel[n]->cnn->rows = r;
network->kernel[n].cnn->columns = c; network->kernel[n]->cnn->columns = c;
network->kernel[n].cnn->w = (float****)malloc(sizeof(float***)*r); network->kernel[n]->cnn->w = (float****)malloc(sizeof(float***)*r);
network->kernel[n].cnn->d_w = (float****)malloc(sizeof(float***)*r); network->kernel[n]->cnn->d_w = (float****)malloc(sizeof(float***)*r);
for (int i=0; i < r; i++) { for (int i=0; i < r; i++) {
network->kernel[n].cnn->w[i] = (float***)malloc(sizeof(float**)*c); network->kernel[n]->cnn->w[i] = (float***)malloc(sizeof(float**)*c);
network->kernel[n].cnn->d_w[i] = (float***)malloc(sizeof(float**)*c); network->kernel[n]->cnn->d_w[i] = (float***)malloc(sizeof(float**)*c);
for (int j=0; j < c; j++) { for (int j=0; j < c; j++) {
network->kernel[n].cnn->w[i][j] = (float**)malloc(sizeof(float*)*kernel_size); network->kernel[n]->cnn->w[i][j] = (float**)malloc(sizeof(float*)*kernel_size);
network->kernel[n].cnn->d_w[i][j] = (float**)malloc(sizeof(float*)*kernel_size); network->kernel[n]->cnn->d_w[i][j] = (float**)malloc(sizeof(float*)*kernel_size);
for (int k=0; k < kernel_size; k++) { for (int k=0; k < kernel_size; k++) {
network->kernel[n].cnn->w[i][j][k] = (float*)malloc(sizeof(float)*kernel_size); network->kernel[n]->cnn->w[i][j][k] = (float*)malloc(sizeof(float)*kernel_size);
network->kernel[n].cnn->d_w[i][j][k] = (float*)malloc(sizeof(float)*kernel_size); network->kernel[n]->cnn->d_w[i][j][k] = (float*)malloc(sizeof(float)*kernel_size);
} }
} }
} }
network->kernel[n].cnn->bias = (float***)malloc(sizeof(float**)*c); network->kernel[n]->cnn->bias = (float***)malloc(sizeof(float**)*c);
network->kernel[n].cnn->d_bias = (float***)malloc(sizeof(float**)*c); network->kernel[n]->cnn->d_bias = (float***)malloc(sizeof(float**)*c);
for (int i=0; i < c; i++) { for (int i=0; i < c; i++) {
network->kernel[n].cnn->bias[i] = (float**)malloc(sizeof(float*)*kernel_size); network->kernel[n]->cnn->bias[i] = (float**)malloc(sizeof(float*)*kernel_size);
network->kernel[n].cnn->d_bias[i] = (float**)malloc(sizeof(float*)*kernel_size); network->kernel[n]->cnn->d_bias[i] = (float**)malloc(sizeof(float*)*kernel_size);
for (int j=0; j < kernel_size; j++) { for (int j=0; j < kernel_size; j++) {
network->kernel[n].cnn->bias[i][j] = (float*)malloc(sizeof(float)*kernel_size); network->kernel[n]->cnn->bias[i][j] = (float*)malloc(sizeof(float)*kernel_size);
network->kernel[n].cnn->d_bias[i][j] = (float*)malloc(sizeof(float)*kernel_size); network->kernel[n]->cnn->d_bias[i][j] = (float*)malloc(sizeof(float)*kernel_size);
} }
} }
create_a_cube_input_layer(network, n, c, network->dim[n-1][0] - 2*(kernel_size/2)); create_a_cube_input_layer(network, n, c, network->dim[n-1][0] - 2*(kernel_size/2));
int n_int = network->dim[n-1][0]*network->dim[n-1][0]*network->dim[n-1][1]; int n_int = network->dim[n-1][0]*network->dim[n-1][0]*network->dim[n-1][1];
int n_out = network->dim[n][0]*network->dim[n][0]*network->dim[n][1]; int n_out = network->dim[n][0]*network->dim[n][0]*network->dim[n][1];
initialisation_3d_matrix(network->initialisation, network->kernel[n].cnn->bias, c, kernel_size, kernel_size, n_int+n_out); initialisation_3d_matrix(network->initialisation, network->kernel[n]->cnn->bias, c, kernel_size, kernel_size, n_int+n_out);
initialisation_3d_matrix(ZERO, network->kernel[n].cnn->d_bias, c, kernel_size, kernel_size, n_int+n_out); initialisation_3d_matrix(ZERO, network->kernel[n]->cnn->d_bias, c, kernel_size, kernel_size, n_int+n_out);
initialisation_4d_matrix(network->initialisation, network->kernel[n].cnn->w, r, c, kernel_size, kernel_size, n_int+n_out); initialisation_4d_matrix(network->initialisation, network->kernel[n]->cnn->w, r, c, kernel_size, kernel_size, n_int+n_out);
initialisation_4d_matrix(ZERO, network->kernel[n].cnn->d_w, r, c, kernel_size, kernel_size, n_int+n_out); initialisation_4d_matrix(ZERO, network->kernel[n]->cnn->d_w, r, c, kernel_size, kernel_size, n_int+n_out);
network->size++; network->size++;
} }
@ -137,23 +137,23 @@ void add_dense(Network* network, int input_units, int output_units, int activati
printf("Impossible de rajouter une couche dense, le réseau est déjà plein\n"); printf("Impossible de rajouter une couche dense, le réseau est déjà plein\n");
return; return;
} }
network->kernel[n].cnn = NULL; network->kernel[n]->cnn = NULL;
network->kernel[n].nn = (Kernel_nn*)malloc(sizeof(Kernel_nn)); network->kernel[n]->nn = (Kernel_nn*)malloc(sizeof(Kernel_nn));
network->kernel[n].activation = activation; network->kernel[n]->activation = activation;
network->kernel[n].nn->input_units = input_units; network->kernel[n]->nn->input_units = input_units;
network->kernel[n].nn->output_units = output_units; network->kernel[n]->nn->output_units = output_units;
network->kernel[n].nn->bias = (float*)malloc(sizeof(float)*output_units); network->kernel[n]->nn->bias = (float*)malloc(sizeof(float)*output_units);
network->kernel[n].nn->d_bias = (float*)malloc(sizeof(float)*output_units); network->kernel[n]->nn->d_bias = (float*)malloc(sizeof(float)*output_units);
network->kernel[n].nn->weights = (float**)malloc(sizeof(float*)*input_units); network->kernel[n]->nn->weights = (float**)malloc(sizeof(float*)*input_units);
network->kernel[n].nn->d_weights = (float**)malloc(sizeof(float*)*input_units); network->kernel[n]->nn->d_weights = (float**)malloc(sizeof(float*)*input_units);
for (int i=0; i < input_units; i++) { for (int i=0; i < input_units; i++) {
network->kernel[n].nn->weights[i] = (float*)malloc(sizeof(float)*output_units); network->kernel[n]->nn->weights[i] = (float*)malloc(sizeof(float)*output_units);
network->kernel[n].nn->d_weights[i] = (float*)malloc(sizeof(float)*output_units); network->kernel[n]->nn->d_weights[i] = (float*)malloc(sizeof(float)*output_units);
} }
initialisation_1d_matrix(network->initialisation, network->kernel[n].nn->bias, output_units, output_units+input_units); initialisation_1d_matrix(network->initialisation, network->kernel[n]->nn->bias, output_units, output_units+input_units);
initialisation_1d_matrix(ZERO, network->kernel[n].nn->d_bias, output_units, output_units+input_units); initialisation_1d_matrix(ZERO, network->kernel[n]->nn->d_bias, output_units, output_units+input_units);
initialisation_2d_matrix(network->initialisation, network->kernel[n].nn->weights, input_units, output_units, output_units+input_units); initialisation_2d_matrix(network->initialisation, network->kernel[n]->nn->weights, input_units, output_units, output_units+input_units);
initialisation_2d_matrix(ZERO, network->kernel[n].nn->d_weights, input_units, output_units, output_units+input_units); initialisation_2d_matrix(ZERO, network->kernel[n]->nn->d_weights, input_units, output_units, output_units+input_units);
create_a_line_input_layer(network, n, output_units); create_a_line_input_layer(network, n, output_units);
network->size++; network->size++;
} }

View File

@ -37,7 +37,7 @@ typedef struct Network{
int max_size; // Taille maximale du réseau après initialisation int max_size; // Taille maximale du réseau après initialisation
int size; // Taille actuelle du réseau int size; // Taille actuelle du réseau
int** dim; // Contient les dimensions de l'input (width*depth) int** dim; // Contient les dimensions de l'input (width*depth)
Kernel* kernel; Kernel** kernel;
float**** input; float**** input;
} Network; } Network;