tipe/src/cnn/initialisation.c

215 lines
6.4 KiB
C
Raw Normal View History

2022-07-05 08:13:25 +02:00
#include <stdlib.h>
#include <math.h>
2022-09-28 10:20:08 +02:00
2023-05-12 16:16:34 +02:00
#include "../common/include/colors.h"
2022-09-16 14:53:35 +02:00
#include "include/initialisation.h"
2022-07-05 08:13:25 +02:00
2022-11-04 10:54:32 +01:00
// glorot (wavier initialisation) linear, tanh, softmax, logistic (1/(fan_in+fan_out/2))
// he initialisation : RELU (2/fan_in)
// LeCun initialisation: SELU (1/fan_in)
2022-07-05 08:13:25 +02:00
2023-03-09 06:57:51 +01:00
// Explained in https://machinelearningmastery.com/weight-initialization-for-deep-learning-neural-networks/
2023-01-17 15:34:29 +01:00
2023-03-09 06:57:51 +01:00
float randn() {
float f1=0.;
while (f1 == 0) {
f1 = RAND_FLT();
2022-11-04 10:54:32 +01:00
}
2023-06-05 21:00:55 +02:00
return sqrt(-2.0*log(f1))*cos(2*M_PI*RAND_FLT());
2023-03-09 06:57:51 +01:00
}
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");
2022-07-05 08:13:25 +02:00
}
}
2022-11-04 10:54:32 +01:00
void initialisation_2d_matrix(int initialisation, float** matrix, int dim1, int dim2, int n_in, int n_out) {
2023-03-09 06:57:51 +01:00
float lower_bound, distance_bounds;
2023-01-17 15:34:29 +01:00
2023-03-09 06:57:51 +01:00
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;
}
2022-07-05 08:13:25 +02:00
}
2023-03-09 06:57:51 +01:00
}
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");
2022-07-05 08:13:25 +02:00
}
}
2022-11-04 10:54:32 +01:00
void initialisation_3d_matrix(int initialisation, float*** matrix, int depth, int dim1, int dim2, int n_in, int n_out) {
2023-03-09 06:57:51 +01:00
float lower_bound, distance_bounds;
2023-01-17 15:34:29 +01:00
2023-03-09 06:57:51 +01:00
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;
}
2022-07-05 08:13:25 +02:00
}
}
2023-03-09 06:57:51 +01:00
}
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");
2022-07-05 08:13:25 +02:00
}
}
2022-11-04 10:54:32 +01:00
void initialisation_4d_matrix(int initialisation, float**** matrix, int depth1, int depth2, int dim1, int dim2, int n_in, int n_out) {
2023-03-09 06:57:51 +01:00
float lower_bound, distance_bounds;
2023-01-17 15:34:29 +01:00
2023-03-09 06:57:51 +01:00
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;
}
2022-07-05 08:13:25 +02:00
}
}
}
2023-03-09 06:57:51 +01:00
}
else
{
printf_warning("Initialisation non reconnue dans 'initialisation_4d_matrix' \n");
2022-07-05 08:13:25 +02:00
}
}