tipe/src/cnn/creation.c

235 lines
10 KiB
C
Raw Normal View History

2022-07-05 08:13:25 +02:00
#include <stdio.h>
#include <stdlib.h>
2022-10-24 12:54:51 +02:00
#include "include/initialisation.h"
2022-09-16 14:53:35 +02:00
#include "include/function.h"
2022-10-24 12:54:51 +02:00
#include "include/creation.h"
2022-07-05 08:13:25 +02:00
2022-11-15 18:15:18 +01:00
Network* create_network(int max_size, float learning_rate, int dropout, int initialisation, int input_dim, int input_depth) {
if (dropout < 0 || dropout > 100) {
2022-07-05 08:13:25 +02:00
printf("Erreur, la probabilité de dropout n'est pas respecté, elle doit être comprise entre 0 et 100\n");
}
2023-01-17 15:34:29 +01:00
Network* network = (Network*)malloc(sizeof(Network));
2022-10-03 10:04:11 +02:00
network->learning_rate = learning_rate;
2023-01-17 15:34:29 +01:00
network->max_size = max_size;
network->dropout = dropout;
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-1));
network->width = (int*)malloc(sizeof(int*)*max_size);
network->depth = (int*)malloc(sizeof(int*)*max_size);
2023-01-17 15:25:34 +01:00
for (int i=0; i < max_size-1; i++) {
2022-09-10 17:17:49 +02:00
network->kernel[i] = (Kernel*)malloc(sizeof(Kernel));
2022-07-05 08:13:25 +02:00
}
2023-01-17 15:34:29 +01:00
network->width[0] = input_dim;
network->depth[0] = input_depth;
network->kernel[0]->nn = NULL;
network->kernel[0]->cnn = NULL;
2022-11-23 10:41:19 +01:00
create_a_cube_input_layer(network, 0, input_depth, input_dim);
2023-01-17 15:34:29 +01:00
create_a_cube_input_z_layer(network, 0, input_depth, input_dim);
2022-07-05 08:13:25 +02:00
return network;
}
2022-11-15 18:15:18 +01:00
Network* create_network_lenet5(float learning_rate, int dropout, int activation, int initialisation, int input_dim, int input_depth) {
2023-01-17 12:49:35 +01:00
Network* network = create_network(8, learning_rate, dropout, initialisation, input_dim, input_depth);
2023-01-17 15:34:29 +01:00
network->kernel[0]->activation = activation;
2022-09-30 15:50:29 +02:00
network->kernel[0]->linearisation = 0;
add_convolution(network, 6, 28, activation);
add_2d_average_pooling(network, 14);
add_convolution(network, 16, 10, activation);
add_2d_average_pooling(network, 5);
add_dense_linearisation(network, 120, activation);
add_dense(network, 84, activation);
add_dense(network, 10, SOFTMAX);
2022-07-05 08:13:25 +02:00
return network;
}
2023-01-21 18:59:59 +01:00
Network* create_simple_one(float learning_rate, int dropout, int activation, int initialisation, int input_dim, int input_depth) {
Network* network = create_network(3, learning_rate, dropout, initialisation, input_dim, input_depth);
network->kernel[0]->activation = activation;
network->kernel[0]->linearisation = 0;
add_dense_linearisation(network, 80, activation);
add_dense(network, 10, SOFTMAX);
return network;
}
2022-07-05 08:13:25 +02:00
void create_a_cube_input_layer(Network* network, int pos, int depth, int dim) {
network->input[pos] = (float***)malloc(sizeof(float**)*depth);
for (int i=0; i < depth; i++) {
network->input[pos][i] = (float**)malloc(sizeof(float*)*dim);
for (int j=0; j < dim; j++) {
network->input[pos][i][j] = (float*)malloc(sizeof(float)*dim);
2022-07-05 08:13:25 +02:00
}
}
2022-09-12 17:56:44 +02:00
network->width[pos] = dim;
network->depth[pos] = depth;
2022-07-05 08:13:25 +02:00
}
2022-10-31 20:08:42 +01:00
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;
}
2022-07-05 08:13:25 +02:00
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*));
network->input[pos][0][0] = (float*)malloc(sizeof(float)*dim);
2022-09-19 18:39:49 +02:00
network->width[pos] = dim;
network->depth[pos] = 1;
2022-07-05 08:13:25 +02:00
}
2022-10-31 20:08:42 +01:00
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) {
2022-07-05 08:13:25 +02:00
int n = network->size;
2022-09-30 15:50:29 +02:00
int k_pos = n-1;
int dim_input = network->width[k_pos];
2022-07-05 08:13:25 +02:00
if (network->max_size == n) {
printf("Impossible de rajouter une couche d'average pooling, le réseau est déjà plein\n");
return;
}
2022-10-31 20:08:42 +01:00
if (dim_input%dim_output != 0) {
2022-09-30 15:50:29 +02:00
printf("Erreur de dimension dans l'average pooling\n");
2022-07-05 08:13:25 +02:00
return;
}
2022-09-30 15:50:29 +02:00
network->kernel[k_pos]->cnn = NULL;
network->kernel[k_pos]->nn = NULL;
2022-11-12 14:20:13 +01:00
network->kernel[k_pos]->activation = IDENTITY; // Ne contient pas de fonction d'activation
2023-01-11 12:09:41 +01:00
network->kernel[k_pos]->linearisation = 0;
2022-09-30 15:50:29 +02:00
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); // Will it be used ?
2022-07-05 08:13:25 +02:00
network->size++;
}
void add_convolution(Network* network, int depth_output, int dim_output, int activation) {
int n = network->size;
2022-09-30 15:50:29 +02:00
int k_pos = n-1;
2022-07-05 08:13:25 +02:00
if (network->max_size == n) {
2022-09-16 14:53:35 +02:00
printf("Impossible de rajouter une couche de convolution, le réseau est déjà plein \n");
2022-07-05 08:13:25 +02:00
return;
}
int depth_input = network->depth[k_pos];
int dim_input = network->width[k_pos];
2022-09-30 15:50:29 +02:00
int bias_size = dim_output;
int kernel_size = dim_input - dim_output +1;
network->kernel[k_pos]->nn = NULL;
network->kernel[k_pos]->activation = activation;
2022-11-04 08:31:58 +01:00
network->kernel[k_pos]->linearisation = 0;
2022-09-30 15:50:29 +02:00
network->kernel[k_pos]->cnn = (Kernel_cnn*)malloc(sizeof(Kernel_cnn));
Kernel_cnn* cnn = network->kernel[k_pos]->cnn;
2022-09-10 18:23:16 +02:00
cnn->k_size = kernel_size;
2022-09-16 14:53:35 +02:00
cnn->rows = depth_input;
cnn->columns = depth_output;
cnn->w = (float****)malloc(sizeof(float***)*depth_input);
cnn->d_w = (float****)malloc(sizeof(float***)*depth_input);
for (int i=0; i < depth_input; i++) {
cnn->w[i] = (float***)malloc(sizeof(float**)*depth_output);
cnn->d_w[i] = (float***)malloc(sizeof(float**)*depth_output);
for (int j=0; j < depth_output; j++) {
2022-09-10 18:23:16 +02:00
cnn->w[i][j] = (float**)malloc(sizeof(float*)*kernel_size);
cnn->d_w[i][j] = (float**)malloc(sizeof(float*)*kernel_size);
for (int k=0; k < kernel_size; k++) {
2022-09-10 18:23:16 +02:00
cnn->w[i][j][k] = (float*)malloc(sizeof(float)*kernel_size);
2022-11-04 10:54:32 +01:00
cnn->d_w[i][j][k] = (float*)calloc(kernel_size, sizeof(float));
2022-07-05 08:13:25 +02:00
}
}
}
2022-09-16 14:53:35 +02:00
cnn->bias = (float***)malloc(sizeof(float**)*depth_output);
cnn->d_bias = (float***)malloc(sizeof(float**)*depth_output);
for (int i=0; i < depth_output; i++) {
2022-09-19 18:39:49 +02:00
cnn->bias[i] = (float**)malloc(sizeof(float*)*bias_size);
cnn->d_bias[i] = (float**)malloc(sizeof(float*)*bias_size);
for (int j=0; j < bias_size; j++) {
cnn->bias[i][j] = (float*)malloc(sizeof(float)*bias_size);
2022-11-04 10:54:32 +01:00
cnn->d_bias[i][j] = (float*)calloc(bias_size, sizeof(float));
2022-07-05 08:13:25 +02:00
}
}
2022-11-04 10:54:32 +01:00
int n_in = network->width[n-1]*network->width[n-1]*network->depth[n-1];
int n_out = network->width[n]*network->width[n]*network->depth[n];
initialisation_3d_matrix(network->initialisation, cnn->bias, depth_output, dim_output, dim_output, n_in, n_out);
initialisation_4d_matrix(network->initialisation, cnn->w, depth_input, depth_output, kernel_size, kernel_size, n_in, n_out);
2022-09-19 18:39:49 +02:00
create_a_cube_input_layer(network, n, depth_output, bias_size);
2022-10-31 20:08:42 +01:00
create_a_cube_input_z_layer(network, n, depth_output, bias_size);
2022-07-05 08:13:25 +02:00
network->size++;
}
void add_dense(Network* network, int output_units, int activation) {
2022-07-05 08:13:25 +02:00
int n = network->size;
2022-09-30 15:50:29 +02:00
int k_pos = n-1;
int input_units = network->width[k_pos];
2022-07-05 08:13:25 +02:00
if (network->max_size == n) {
printf("Impossible de rajouter une couche dense, le réseau est déjà plein\n");
return;
}
2022-09-30 15:50:29 +02:00
network->kernel[k_pos]->cnn = NULL;
network->kernel[k_pos]->nn = (Kernel_nn*)malloc(sizeof(Kernel_nn));
Kernel_nn* nn = network->kernel[k_pos]->nn;
network->kernel[k_pos]->activation = activation;
2022-11-04 08:31:58 +01:00
network->kernel[k_pos]->linearisation = 0;
2022-09-10 18:23:16 +02:00
nn->input_units = input_units;
nn->output_units = output_units;
nn->bias = (float*)malloc(sizeof(float)*output_units);
2022-11-04 10:54:32 +01:00
nn->d_bias = (float*)calloc(output_units, sizeof(float));
2022-09-10 18:23:16 +02:00
nn->weights = (float**)malloc(sizeof(float*)*input_units);
nn->d_weights = (float**)malloc(sizeof(float*)*input_units);
for (int i=0; i < input_units; i++) {
2022-09-10 18:23:16 +02:00
nn->weights[i] = (float*)malloc(sizeof(float)*output_units);
2022-11-04 10:54:32 +01:00
nn->d_weights[i] = (float*)calloc(output_units, sizeof(float));
2022-07-05 08:13:25 +02:00
}
2022-11-04 10:54:32 +01:00
initialisation_1d_matrix(network->initialisation, nn->bias, output_units, input_units, output_units);
initialisation_2d_matrix(network->initialisation, nn->weights, input_units, output_units, input_units, output_units);
2022-10-02 20:31:20 +02:00
create_a_line_input_layer(network, n, output_units);
2022-10-31 20:08:42 +01:00
create_a_line_input_z_layer(network, n, output_units);
2022-09-19 18:39:49 +02:00
network->size++;
}
void add_dense_linearisation(Network* network, int output_units, int activation) {
2022-09-19 18:39:49 +02:00
// Can replace input_units by a research of this dim
int n = network->size;
2022-09-30 15:50:29 +02:00
int k_pos = n-1;
int input_units = network->depth[k_pos]*network->width[k_pos]*network->width[k_pos];
2022-09-19 18:39:49 +02:00
if (network->max_size == n) {
printf("Impossible de rajouter une couche dense, le réseau est déjà plein\n");
return;
}
2022-09-30 15:50:29 +02:00
network->kernel[k_pos]->cnn = NULL;
network->kernel[k_pos]->nn = (Kernel_nn*)malloc(sizeof(Kernel_nn));
Kernel_nn* nn = network->kernel[k_pos]->nn;
network->kernel[k_pos]->activation = activation;
2022-11-04 08:31:58 +01:00
network->kernel[k_pos]->linearisation = 1;
2022-09-19 18:39:49 +02:00
nn->input_units = input_units;
nn->output_units = output_units;
2023-01-17 15:34:29 +01:00
2022-09-19 18:39:49 +02:00
nn->bias = (float*)malloc(sizeof(float)*output_units);
2022-11-04 10:54:32 +01:00
nn->d_bias = (float*)calloc(output_units, sizeof(float));
2022-09-19 18:39:49 +02:00
nn->weights = (float**)malloc(sizeof(float*)*input_units);
nn->d_weights = (float**)malloc(sizeof(float*)*input_units);
for (int i=0; i < input_units; i++) {
nn->weights[i] = (float*)malloc(sizeof(float)*output_units);
2022-11-04 10:54:32 +01:00
nn->d_weights[i] = (float*)calloc(output_units, sizeof(float));
2022-09-19 18:39:49 +02:00
}
2022-11-04 10:54:32 +01:00
initialisation_1d_matrix(network->initialisation, nn->bias, output_units, input_units, output_units);
initialisation_2d_matrix(network->initialisation, nn->weights, input_units, output_units, input_units, output_units);
2022-09-19 18:39:49 +02:00
create_a_line_input_layer(network, n, output_units);
2022-10-31 20:08:42 +01:00
create_a_line_input_z_layer(network, n, output_units);
2022-07-05 08:13:25 +02:00
network->size++;
}