mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-23 15:16:26 +01:00
Add 3 new types of initialisation
This commit is contained in:
parent
91e4a1b316
commit
fec4651946
@ -235,7 +235,7 @@ void add_dense(Network* network, int size_output, int activation) {
|
||||
}
|
||||
}
|
||||
|
||||
initialisation_1d_matrix(network->initialisation, nn->bias, size_output, size_input);
|
||||
initialisation_1d_matrix(network->initialisation, nn->bias, size_output, size_input, size_output);
|
||||
initialisation_2d_matrix(network->initialisation, nn->weights, size_input, size_output, size_input, size_output);
|
||||
create_a_line_input_layer(network, n, size_output);
|
||||
create_a_line_input_z_layer(network, n, size_output);
|
||||
@ -275,7 +275,7 @@ void add_dense_linearisation(Network* network, int size_output, int activation)
|
||||
nn->d_weights[i][j] = 0.;
|
||||
}
|
||||
}
|
||||
initialisation_1d_matrix(network->initialisation, nn->bias, size_output, size_input);
|
||||
initialisation_1d_matrix(network->initialisation, nn->bias, size_output, size_input, size_output);
|
||||
initialisation_2d_matrix(network->initialisation, nn->weights, size_input, size_output, size_input, size_output);
|
||||
create_a_line_input_layer(network, n, size_output);
|
||||
create_a_line_input_z_layer(network, n, size_output);
|
||||
|
@ -3,16 +3,24 @@
|
||||
|
||||
// Génère un flottant entre 0 et 1
|
||||
#define RAND_FLT() ((float)rand())/((float)RAND_MAX)
|
||||
#define TWOPI 6.2831853071795864769252867665
|
||||
|
||||
#define ZERO 0
|
||||
#define GLOROT 1
|
||||
#define XAVIER 1 // Xavier and Glorot initialisations are the same
|
||||
#define HE 2
|
||||
#define XAVIER 1 // Xavier et Glorot initialisations sont indentiques
|
||||
#define NORMALIZED_XAVIER 2
|
||||
#define HE 3
|
||||
|
||||
/*
|
||||
* Renvoie un flottant à partir de la loi normale [x;y].
|
||||
* La fonction repose sur la méthode de Box-Muller
|
||||
*/
|
||||
float randn();
|
||||
|
||||
/*
|
||||
* Initialise une matrice 1d dim de float en fonction du type d'initialisation
|
||||
*/
|
||||
void initialisation_1d_matrix(int initialisation, float* matrix, int dim, int n_in);
|
||||
void initialisation_1d_matrix(int initialisation, float* matrix, int dim, int n_in, int n_out);
|
||||
|
||||
/*
|
||||
* Initialise une matrice 2d dim1*dim2 de float en fonction du type d'initialisation
|
||||
|
@ -8,87 +8,208 @@
|
||||
// he initialisation : RELU (2/fan_in)
|
||||
// LeCun initialisation: SELU (1/fan_in)
|
||||
|
||||
// Only uniform for the moment
|
||||
void initialisation_1d_matrix(int initialisation, float* matrix, int dim, int n_in) {
|
||||
int n;
|
||||
if (initialisation == GLOROT) {
|
||||
n = (n_in + dim)/2;
|
||||
// Explained in https://machinelearningmastery.com/weight-initialization-for-deep-learning-neural-networks/
|
||||
|
||||
} else if (initialisation == HE) {
|
||||
n = n_in/2;
|
||||
} else {
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_1d_matrix' \n");
|
||||
return ;
|
||||
float randn() {
|
||||
float f1=0.;
|
||||
while (f1 == 0) {
|
||||
f1 = RAND_FLT();
|
||||
}
|
||||
float lower_bound = -1/sqrt((double)n);
|
||||
float distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i < dim; i++) {
|
||||
matrix[i] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
return sqrt(-2.0*log(f1))*cos(TWOPI*RAND_FLT());
|
||||
}
|
||||
|
||||
void initialisation_1d_matrix(int initialisation, float* matrix, int dim, int n_in, int n_out) {
|
||||
float lower_bound, distance_bounds;
|
||||
|
||||
if (initialisation == ZERO) {
|
||||
for (int i=0; i<dim; i++) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
}
|
||||
else if (initialisation == XAVIER)
|
||||
{
|
||||
lower_bound = -1/sqrt((double)n_in);
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i < dim; i++) {
|
||||
matrix[i] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
else if (initialisation == NORMALIZED_XAVIER)
|
||||
{
|
||||
lower_bound = -sqrt(6/(double)(n_in + n_out));
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i < dim; i++) {
|
||||
matrix[i] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
else if (initialisation == HE)
|
||||
{
|
||||
distance_bounds = 2/sqrt((double)n_in);
|
||||
for (int i=0; i < dim; i++) {
|
||||
matrix[i] = randn()*distance_bounds;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_1d_matrix' \n");
|
||||
}
|
||||
}
|
||||
|
||||
void initialisation_2d_matrix(int initialisation, float** matrix, int dim1, int dim2, int n_in, int n_out) {
|
||||
int n;
|
||||
if (initialisation == GLOROT) {
|
||||
n = (n_in + n_out)/2;
|
||||
float lower_bound, distance_bounds;
|
||||
|
||||
} else if (initialisation == HE) {
|
||||
n = n_in/2;
|
||||
} else {
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_2d_matrix' \n");
|
||||
return ;
|
||||
}
|
||||
float lower_bound = -1/sqrt((double)n);
|
||||
float distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i < dim1; i++) {
|
||||
for (int j=0; j < dim2; j++) {
|
||||
matrix[i][j] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
if (initialisation == ZERO) {
|
||||
for (int i=0; i<dim1; i++) {
|
||||
for (int j=0; j<dim2; j++) {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == XAVIER)
|
||||
{
|
||||
lower_bound = -1/sqrt((double)n_in);
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i<dim1; i++) {
|
||||
for (int j=0; j<dim2; j++) {
|
||||
matrix[i][j] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == NORMALIZED_XAVIER)
|
||||
{
|
||||
lower_bound = -sqrt(6/(double)(n_in + n_out));
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i<dim1; i++) {
|
||||
for (int j=0; j<dim2; j++) {
|
||||
matrix[i][j] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == HE)
|
||||
{
|
||||
distance_bounds = 2/sqrt((double)n_in);
|
||||
for (int i=0; i<dim1; i++) {
|
||||
for (int j=0; j<dim2; j++) {
|
||||
matrix[i][j] = randn()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_2d_matrix' \n");
|
||||
}
|
||||
}
|
||||
|
||||
void initialisation_3d_matrix(int initialisation, float*** matrix, int depth, int dim1, int dim2, int n_in, int n_out) {
|
||||
int n;
|
||||
if (initialisation == GLOROT) {
|
||||
n = (n_in + n_out)/2;
|
||||
float lower_bound, distance_bounds;
|
||||
|
||||
} else if (initialisation == HE) {
|
||||
n = n_in/2;
|
||||
} else {
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_3d_matrix' \n");
|
||||
return ;
|
||||
}
|
||||
float lower_bound = -1/sqrt((double)n);
|
||||
float distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i < depth; i++) {
|
||||
for (int j=0; j < dim1; j++) {
|
||||
for (int k=0; k < dim2; k++) {
|
||||
matrix[i][j][k] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
if (initialisation == ZERO) {
|
||||
for (int i=0; i<depth; i++) {
|
||||
for (int j=0; j<dim1; j++) {
|
||||
for (int k=0; k<dim2; k++) {
|
||||
matrix[i][j][k] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == XAVIER)
|
||||
{
|
||||
lower_bound = -1/sqrt((double)n_in);
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i<depth; i++) {
|
||||
for (int j=0; j<dim1; j++) {
|
||||
for (int k=0; k<dim2; k++) {
|
||||
matrix[i][j][k] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == NORMALIZED_XAVIER)
|
||||
{
|
||||
lower_bound = -sqrt(6/(double)(n_in + n_out));
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i<depth; i++) {
|
||||
for (int j=0; j<dim1; j++) {
|
||||
for (int k=0; k<dim2; k++) {
|
||||
matrix[i][j][k] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == HE)
|
||||
{
|
||||
distance_bounds = 2/sqrt((double)n_in);
|
||||
for (int i=0; i<depth; i++) {
|
||||
for (int j=0; j<dim1; j++) {
|
||||
for (int k=0; k<dim2; k++) {
|
||||
matrix[i][j][k] = randn()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_3d_matrix' \n");
|
||||
}
|
||||
}
|
||||
|
||||
void initialisation_4d_matrix(int initialisation, float**** matrix, int depth1, int depth2, int dim1, int dim2, int n_in, int n_out) {
|
||||
int n;
|
||||
if (initialisation == GLOROT) {
|
||||
n = (n_in + n_out)/2;
|
||||
float lower_bound, distance_bounds;
|
||||
|
||||
} else if (initialisation == HE) {
|
||||
n = n_in/2;
|
||||
} else {
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_3d_matrix' \n");
|
||||
return ;
|
||||
}
|
||||
float lower_bound = -1/sqrt((double)n);
|
||||
float distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i < depth1; i++) {
|
||||
for (int j=0; j < depth2; j++) {
|
||||
for (int k=0; k < dim1; k++) {
|
||||
for (int l=0; l < dim2; l++) {
|
||||
matrix[i][j][k][l] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
if (initialisation == ZERO) {
|
||||
for (int i=0; i<depth1; i++) {
|
||||
for (int j=0; j<depth2; j++) {
|
||||
for (int k=0; k<dim1; k++) {
|
||||
for (int l=0; l<depth2; l++) {
|
||||
matrix[i][j][k][l] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == XAVIER)
|
||||
{
|
||||
lower_bound = -1/sqrt((double)n_in);
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i<depth1; i++) {
|
||||
for (int j=0; j<depth2; j++) {
|
||||
for (int k=0; k<dim1; k++) {
|
||||
for (int l=0; l<dim2; l++) {
|
||||
matrix[i][j][k][l] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == NORMALIZED_XAVIER)
|
||||
{
|
||||
lower_bound = -sqrt(6/(double)(n_in + n_out));
|
||||
distance_bounds = -2*lower_bound;
|
||||
for (int i=0; i<depth1; i++) {
|
||||
for (int j=0; j<depth2; j++) {
|
||||
for (int k=0; k<dim1; k++) {
|
||||
for (int l=0; l<dim2; l++) {
|
||||
matrix[i][j][k][l] = lower_bound + RAND_FLT()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (initialisation == HE)
|
||||
{
|
||||
distance_bounds = 2/sqrt((double)n_in);
|
||||
for (int i=0; i<depth1; i++) {
|
||||
for (int j=0; j<depth2; j++) {
|
||||
for (int k=0; k<dim1; k++) {
|
||||
for (int l=0; l<dim2; l++) {
|
||||
matrix[i][j][k][l] = randn()*distance_bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf_warning("Initialisation non reconnue dans 'initialisation_4d_matrix' \n");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user