mirror of
https://github.com/augustin64/projet-tipe
synced 2025-02-02 19:39:39 +01:00
Deletion of useless files
This commit is contained in:
parent
38f850988b
commit
9a032a4933
@ -1,507 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <float.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct Matrix {
|
|
||||||
int rows; // Nombre de lignes de la matrice
|
|
||||||
int columns; // Nombre de colonnes de la matrice
|
|
||||||
float** value; // Tableau 2d comportant les valeurs de matrice
|
|
||||||
|
|
||||||
} Matrix;
|
|
||||||
|
|
||||||
// Mis ici jusqu'à le rassemblement des fichiers
|
|
||||||
typedef struct Neuron{
|
|
||||||
float* weights; // Liste de tous les poids des arêtes sortants du neurone
|
|
||||||
float bias; // Caractérise le bias du neurone
|
|
||||||
float z; // Sauvegarde des calculs faits sur le neurone (programmation dynamique)
|
|
||||||
|
|
||||||
float *back_weights; // Changement des poids sortants lors de la backpropagation
|
|
||||||
float *last_back_weights; // Dernier changement de d_poid_sortants
|
|
||||||
float back_bias; // Changement du bias lors de la backpropagation
|
|
||||||
float last_back_bias; // Dernier changement de back_bias
|
|
||||||
} Neuron;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float exp_float(float a);
|
|
||||||
float max_float(float a, float b);
|
|
||||||
float min_float(float a, float b);
|
|
||||||
Matrix* create_matrix(int nb_rows, int nb_columns);
|
|
||||||
void uniformity_matrix(Matrix* m, float v);
|
|
||||||
void print_matrix(Matrix* m);
|
|
||||||
float number_from_matrix(Matrix* m);
|
|
||||||
float max_in_matrix(Matrix* m);
|
|
||||||
void free_matrix(Matrix* m);
|
|
||||||
void product_of_a_scalar_matrix(Matrix* m, float scalar);
|
|
||||||
void sum_of_a_scalar_matrix(Matrix* m, float scalar);
|
|
||||||
Matrix* new_copy_matrix(Matrix* m);
|
|
||||||
Matrix* apply_function_new_matrix(Matrix* m, float (*f)(float));
|
|
||||||
void apply_function_matrix(Matrix* m, float (*f)(float));
|
|
||||||
void transpose_matrix(Matrix* m);
|
|
||||||
void add_matrix(Matrix* m1, Matrix* m2);
|
|
||||||
Matrix* product_matrix(Matrix* m1, Matrix* m2);
|
|
||||||
void max_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void min_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void average_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void valid_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void full_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void softmax_matrix(Matrix* m);
|
|
||||||
float quadratic_cost_matrix(Matrix* m, int i_number, int j_number);
|
|
||||||
void rotation_180_matrix(Matrix* m);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float exp_float(float a) {
|
|
||||||
/* Renvoie l'exponentiel d'un flotant '*/
|
|
||||||
return (float)exp(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float max_float(float a, float b) {
|
|
||||||
/* Renvoie le max entre les deux flotants */
|
|
||||||
return a>b?a:b;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float min_float(float a, float b) {
|
|
||||||
/* Renvoie le min entre les deux flotants */
|
|
||||||
return a<b?a:b;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* create_matrix(int nb_rows, int nb_columns) {
|
|
||||||
/* Créé une matrice en lui allouant de la mémoire */
|
|
||||||
Matrix* m = malloc(sizeof(Matrix));
|
|
||||||
m->rows = nb_rows;
|
|
||||||
m->columns = nb_columns;
|
|
||||||
m->value = malloc(sizeof(float*)*m->rows);
|
|
||||||
for (int i=0; i < m->rows; i++)
|
|
||||||
m->value[i] = malloc(sizeof(float)*m->columns);
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void uniformity_matrix(Matrix* m, float v) {
|
|
||||||
/* Insère la même valeur partout dans la matrice */
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
m->value[i][j] = v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void print_matrix(Matrix* m) {
|
|
||||||
/* Affiche la matrice */
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
if (j!=0)
|
|
||||||
printf(",");
|
|
||||||
printf("%f ", m->value[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float number_from_matrix(Matrix* m) {
|
|
||||||
/* Renvoie la somme des éléments de la matrice */
|
|
||||||
float tmp=0;
|
|
||||||
for (int i=0; i < m->rows ; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
tmp += m->value[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float max_in_matrix(Matrix* m) {
|
|
||||||
/* Renvoie l'élément maximal de la matrice */
|
|
||||||
float max_tmp = FLT_MIN;
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
max_tmp = max_float(max_tmp, m->value[i][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return max_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void free_matrix(Matrix* m) {
|
|
||||||
/* Libère l'espace mémoire alloué à la matrice */
|
|
||||||
for (int i=0; i < m->rows; i++)
|
|
||||||
free(m->value[i]);
|
|
||||||
free(m->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void product_of_a_scalar_matrix(Matrix* m, float scalar) {
|
|
||||||
/* Multiplie la matrice par un scalaire */
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
m->value[i][j] *= scalar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void sum_of_a_scalar_matrix(Matrix* m, float scalar) {
|
|
||||||
/* Ajoute un scalaire à la matrice */
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
m->value[i][j] += scalar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* new_copy_matrix(Matrix* m) {
|
|
||||||
/* Renvoie une copie de la matrice */
|
|
||||||
Matrix* new_m = create_matrix(m->rows, m->columns);
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
new_m->value[i][j] = m->value[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new_m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void copy_matrix(Matrix* m1, Matrix* m2) {
|
|
||||||
/* Copie le contenu de la matrice m1 dans la matrice m2 */
|
|
||||||
if (m1->rows != m2->rows || m1->columns != m2->columns) {
|
|
||||||
printf("Erreur, copie dans de deux matrices dont les dimensions diffèrent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i=0; i < m1->rows; i++) {
|
|
||||||
for (int j=0; j < m2->columns; j++) {
|
|
||||||
m2->value[i][j] = m1->value[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* apply_function_new_matrix(Matrix* m, float (*f)(float)) {
|
|
||||||
/* Renvoie une matrice avec une fonction appliquée
|
|
||||||
à tous les éléments de l'ancienne matrice */
|
|
||||||
Matrix* new_m = create_matrix(m->rows, m->columns);
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m ->columns; j++) {
|
|
||||||
new_m->value[i][j] = (*f)(m->value[i][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new_m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void apply_function_matrix(Matrix* m, float (*f)(float)) {
|
|
||||||
/* Applique une fonction à tous les éléments de la matrice */
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m ->columns; j++) {
|
|
||||||
m->value[i][j] = (*f)(m->value[i][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void transpose_matrix(Matrix* m) {
|
|
||||||
/* Transpose la matrice si c'est possible */
|
|
||||||
if (m->rows != m->columns) {
|
|
||||||
printf("Erreur, matrice non compatible avec la transposition");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
float cpt;
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=i+1; j < m->columns; j++) {
|
|
||||||
cpt = m->value[i][j];
|
|
||||||
m->value[i][j] = m->value[j][i];
|
|
||||||
m->value[j][i] = cpt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void add_matrix(Matrix* m1, Matrix* m2) {
|
|
||||||
/* Ajoute la matrice m1 à la matrice m2 */
|
|
||||||
if (m1->rows != m2->rows || m1->columns != m2->columns) {
|
|
||||||
printf("Erreur, matrices non compatibles avec la somme");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i=0; i < m2->rows; i++) {
|
|
||||||
for (int j=0; j < m2->columns; j++) {
|
|
||||||
m2->value[i][j] += m1->value[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* product_matrix(Matrix* m1, Matrix* m2) {
|
|
||||||
/* Renvoie une nouvelle matrice produit (classique)
|
|
||||||
des deux matrices si les dimensions sont correctes*/
|
|
||||||
if (m1->columns != m2->rows) {
|
|
||||||
printf("Erreur, matrices non compatibles avec le produit");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
float cpt;
|
|
||||||
Matrix* m = create_matrix(m1->rows, m2->columns);
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
cpt=0;
|
|
||||||
for (int k=0; k < m2->rows; k++) {
|
|
||||||
cpt += m1->value[i][j]* m2->value[k][j];
|
|
||||||
}
|
|
||||||
m->value[i][j] = cpt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void max_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère le résultat de max pooling avec un décalage
|
|
||||||
de (stride) pixels dans la matrice m_out */
|
|
||||||
if (m_in->columns < kernel->columns || m_in->rows < kernel->rows) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans max pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->columns - kernel->columns)/stride)+1 != m_out->columns || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans max pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, a ,b;
|
|
||||||
float tmp;
|
|
||||||
for (i=0; i < m_out->rows; i++) {
|
|
||||||
for (j=0; j < m_out->columns; j++) {
|
|
||||||
tmp = FLT_MIN;
|
|
||||||
for (a=0; a < kernel->rows; a++) {
|
|
||||||
for (b=0; b < kernel->columns; b++) {
|
|
||||||
tmp = max_float(tmp, m_in->value[i*stride +a][j*stride +b]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_out->value[i][j] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void min_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère le résultat de min pooling avec un décalage
|
|
||||||
de (stride) pixels dans la matrice m_out */
|
|
||||||
if (m_in->columns < kernel->columns || m_in->rows < kernel->rows) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans min pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->columns - kernel->columns)/stride)+1 != m_out->columns || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans min pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, a ,b;
|
|
||||||
float tmp;
|
|
||||||
for (i=0; i < m_out->rows; i++) {
|
|
||||||
for (j=0; j < m_out->columns; j++) {
|
|
||||||
tmp = FLT_MAX;
|
|
||||||
for (a=0; a < kernel->rows; a++) {
|
|
||||||
for (b=0; b < kernel->columns; b++) {
|
|
||||||
tmp = min_float(tmp, m_in->value[i*stride +a][j*stride +b]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_out->value[i][j] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void average_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère le résultat de max pooling avec un décalage
|
|
||||||
de (stride) pixels dans la matrice m_out */
|
|
||||||
if (m_in->columns < kernel->columns || m_in->rows < kernel->rows) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans average pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->columns - kernel->columns)/stride)+1 != m_out->columns || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans average pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, a, b, nb= kernel->rows*kernel->columns;
|
|
||||||
for (i=0; i < m_out->rows; i++) {
|
|
||||||
for (j=0; j < m_out->columns; j++) {
|
|
||||||
m_out->value[i][j] = 0;
|
|
||||||
for (a=0; a < kernel->rows; a++) {
|
|
||||||
for (b=0; b < kernel->columns; b++) {
|
|
||||||
m_out->value[i][j] += m_in->value[i*stride +a][j*stride +b];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_out->value[i][j] = m_out->value[i][j]/nb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void valid_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Ajoute, la cross-correlation valide de m_in et
|
|
||||||
kernel avec un décalage de stride, dans m_out */
|
|
||||||
if (m_in->columns < kernel->columns || m_in->rows < kernel->rows) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans valid cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->columns - kernel->columns)/stride)+1 != m_out->columns || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans valid cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, a, b;
|
|
||||||
for (i=0; i < m_out->rows; i++) {
|
|
||||||
for (j=0; j < m_out->columns; j++) {
|
|
||||||
for (a=0; a < kernel->rows; a++) {
|
|
||||||
for (b=0; b < kernel->columns; b++) {
|
|
||||||
m_out->value[i][j] += m_in->value[i*stride +a][j*stride +b]*kernel->value[a][b];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void full_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Ajoute, la cross-correlation entière de m_in et
|
|
||||||
kernel avec un décalage de stride, dans m_out */
|
|
||||||
int rows_k = kernel->rows-1;
|
|
||||||
int columns_k = kernel->columns-1;
|
|
||||||
if (m_in->columns < kernel->columns || m_in->rows < kernel->rows) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans full cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ((m_in->columns + 2*columns_k)/stride != m_out->columns || (m_in->rows + 2*rows_k)/stride != m_out->rows) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans full cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, a, b, new_i, new_j;
|
|
||||||
for (i=-rows_k; i < (m_out->rows + kernel->rows -1); i++) {
|
|
||||||
for (j=-columns_k; j < (m_out->columns + kernel->columns -1); j++) {
|
|
||||||
m_out->value[i+rows_k][j+columns_k] = 0;
|
|
||||||
for (a=0; a < kernel->rows; a++) {
|
|
||||||
for (b=0; b < kernel->columns; b++) {
|
|
||||||
new_i = i*stride +a;
|
|
||||||
new_j = j*stride +b;
|
|
||||||
if (new_i >= 0 || new_i < m_in->rows || new_j >= 0 || new_j < m_in->columns)
|
|
||||||
m_out->value[i+rows_k][j+columns_k] += m_in->value[i*stride +a][j*stride +b]*kernel->value[a][b];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void softmax_matrix(Matrix* m) {
|
|
||||||
/* Applique la fonction softmax sur la matrice en changeant ses valeurs */
|
|
||||||
float max = max_in_matrix(m);
|
|
||||||
sum_of_a_scalar_matrix(m, (-1)*max);
|
|
||||||
apply_function_matrix(m, exp_float);
|
|
||||||
float sum = number_from_matrix(m);
|
|
||||||
sum = 1/sum;
|
|
||||||
product_of_a_scalar_matrix(m, sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float quadratic_cost_matrix(Matrix* m, int i_number, int j_number) {
|
|
||||||
/* Renvoie l'erreur de la matrice où les valeurs sont des probabailités */
|
|
||||||
float loss = 0;
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
if (i==i_number && j==j_number)
|
|
||||||
loss += (1-m->value[i][j])*(1-m->value[i][j]);
|
|
||||||
else
|
|
||||||
loss += m->value[i][j]*m->value[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return loss;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void rotation_180_matrix(Matrix* m) {
|
|
||||||
/* Modifie la matrice en pivotant ses valeurs de 180° */
|
|
||||||
if (m->rows != m-> columns) {
|
|
||||||
printf("Erreur, une matrice non carrée ne peut pas être retourner");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
float tmp;
|
|
||||||
int half_rows = m->rows/2;
|
|
||||||
int max_r = m->rows-1;
|
|
||||||
int max_c = m->columns-1;
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=i; j < m->columns; j++) {
|
|
||||||
if (i!=j || i>=half_rows) {
|
|
||||||
tmp = m->value[i][j];
|
|
||||||
m->value[i][j] = m->value[max_r-i][max_c-j];
|
|
||||||
m->value[max_r-i][max_c-j] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void valid__cross_correlation_step_forward(Matrix** layer_input, Matrix*** layer_kernel, Matrix** layer_bias, Matrix** layer_output, int len_layer, int depth_kernel, int stride) {
|
|
||||||
/* Effectue une étape de la forward-propagation
|
|
||||||
à l'aide d'une cross-correlation valide */
|
|
||||||
for (int i=0; i < depth_kernel; i++) {
|
|
||||||
copy_matrix(layer_bias[i], layer_output[i]);
|
|
||||||
|
|
||||||
for (int j=0; j < len_layer; j++) {
|
|
||||||
valid_cross_correlation_matrix(layer_input[j], layer_kernel[i][j], stride, layer_output[j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void max_pooling_step_forward(Matrix** layer_input, Matrix*** layer_kernel, Matrix** layer_bias, Matrix** layer_output, int len_layer, int depth_kernel, int stride) {
|
|
||||||
/* Effectue une étape de la forward-propagation
|
|
||||||
à l'aide d'un max_pooling */
|
|
||||||
for (int i=0; i < depth_kernel; i++) {
|
|
||||||
copy_matrix(layer_bias[i], layer_output[i]);
|
|
||||||
|
|
||||||
for (int j=0; j < len_layer; j++) {
|
|
||||||
max_pooling_matrix(layer_input[j], layer_kernel[i][j], stride, layer_output[j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void average_pooling_step_forward(Matrix** layer_input, Matrix*** layer_kernel, Matrix** layer_bias, Matrix** layer_output, int len_layer, int depth_kernel, int stride) {
|
|
||||||
/* Effectue une étape de la forward-propagation
|
|
||||||
à l'aide d'un average_pooling */
|
|
||||||
for (int i=0; i < depth_kernel; i++) {
|
|
||||||
copy_matrix(layer_bias[i], layer_output[i]);
|
|
||||||
|
|
||||||
for (int j=0; j < len_layer; j++) {
|
|
||||||
average_pooling_matrix(layer_input[j], layer_kernel[i][j], stride, layer_output[j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void reshape_step_forward(Matrix** layer_input, Neuron** output, int len_layer) {
|
|
||||||
/* Effectue une étape de la forward-propagation
|
|
||||||
en redimensionnant la matrice */
|
|
||||||
int cpt = 0;
|
|
||||||
for (int i=0; i < len_layer; i++) {
|
|
||||||
for (int j=0; j < layer_input[i]->rows; j++) {
|
|
||||||
for (int k=0; k < layer_input[i]->columns; k++) {
|
|
||||||
output[cpt]->z = layer_input[i]->value[j][k];
|
|
||||||
cpt++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
Matrix* m = create_matrix(2, 2);
|
|
||||||
uniformity_matrix(m, 1);
|
|
||||||
print_matrix(m);
|
|
||||||
free_matrix(m);
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,467 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <float.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct Matrix {
|
|
||||||
int depths; // Nombre de couches de la matrice
|
|
||||||
int rows; // Nombre de lignes de la matrice
|
|
||||||
int columns; // Nombre de colonnes de la matrice
|
|
||||||
float*** value; // Tableau 2d comportant les valeurs de matrice
|
|
||||||
|
|
||||||
} Matrix;
|
|
||||||
|
|
||||||
float exp_float(float a);
|
|
||||||
float max_float(float a, float b);
|
|
||||||
float min_float(float a, float b);
|
|
||||||
Matrix* create_matrix(int nb_layers, int nb_rows, int nb_columns);
|
|
||||||
void uniformise_matrix(Matrix* m, float x);
|
|
||||||
float max_in_matrix(Matrix* m);
|
|
||||||
void free_matrix(Matrix* m);
|
|
||||||
float number_from_matrix(Matrix* m);
|
|
||||||
void product_of_a_scalar_matrix(Matrix* m, float scalar);
|
|
||||||
void sum_of_a_scalar_matrix(Matrix* m, float scalar);
|
|
||||||
Matrix* copy_matrix(Matrix* m);
|
|
||||||
Matrix* apply_function_new_matrix(Matrix* m, float (*f)(float));
|
|
||||||
void apply_function_matrix(Matrix* m, float (*f)(float));
|
|
||||||
Matrix* add_matrix(Matrix* m1, Matrix* m2);
|
|
||||||
Matrix* product_matrix(Matrix* m1, Matrix* m2);
|
|
||||||
void max_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void min_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void average_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void valid_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void full_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out);
|
|
||||||
void softmax_matrix(Matrix* m);
|
|
||||||
float quadratic_cost_matrix(Matrix* m, int i_number, int j_number, int k_number);
|
|
||||||
void rotation_180_matrix(Matrix* m);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float exp_float(float a) {
|
|
||||||
/* Renvoie l'exponentiel d'un flotant '*/
|
|
||||||
return (float)exp(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float max_float(float a, float b) {
|
|
||||||
/* Renvoie le max entre les deux flotants */
|
|
||||||
return a>b?a:b;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float min_float(float a, float b) {
|
|
||||||
/* Renvoie le min entre les deux flotants */
|
|
||||||
return a<b?a:b;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* create_matrix(int nb_layers, int nb_rows, int nb_columns) {
|
|
||||||
/* Créé une matrice en lui allouant de la mémoire */
|
|
||||||
Matrix* m = malloc(sizeof(Matrix));
|
|
||||||
m->rows = nb_rows;
|
|
||||||
m->columns = nb_columns;
|
|
||||||
m->depths = nb_layers;
|
|
||||||
m->value = malloc(sizeof(float**)*m->depths);
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
m->value[i] = malloc(sizeof(float*)*m->rows);
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
m->value[i][j] = malloc(sizeof(float*)*m->columns);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void uniformise_matrix(Matrix* m, float x) {
|
|
||||||
/* Donne la même valeur x à tous les éléments de la matrice */
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
m->value[i][j][k] = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void print_matrix(Matrix* m) {
|
|
||||||
/* Affiche la matrice */
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
if (i!=0)
|
|
||||||
printf("-----------------\n");
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
if (k!=0)
|
|
||||||
printf(",");
|
|
||||||
printf("%f ", m->value[i][j][k]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float max_in_matrix(Matrix* m) {
|
|
||||||
/* Renvoie l'élément maximal de la matrice */
|
|
||||||
float max_tmp = FLT_MIN;
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
max_tmp = max_float(max_tmp, m->value[i][j][k]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return max_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void free_matrix(Matrix* m) {
|
|
||||||
/* Libère l'espace mémoire alloué à la matrice */
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
free(m->value[i][j]);
|
|
||||||
}
|
|
||||||
free(m->value[i]);
|
|
||||||
}
|
|
||||||
free(m->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float number_from_matrix(Matrix* m) {
|
|
||||||
/* Renvoie la somme des éléments de la matrice */
|
|
||||||
float tmp=0;
|
|
||||||
for (int i=0; i < m->depths ; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
tmp += m->value[i][j][k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void product_of_a_scalar_matrix(Matrix* m, float scalar) {
|
|
||||||
/* Multiplie la matrice par un scalaire */
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
m->value[i][j][k] *= scalar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void sum_of_a_scalar_matrix(Matrix* m, float scalar) {
|
|
||||||
/* Ajoute un scalaire à la matrice */
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
m->value[i][j][k] += scalar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* copy_matrix(Matrix* m) {
|
|
||||||
/* Renvoie une copie de la matrice */
|
|
||||||
Matrix* new_m = create_matrix(m->depths, m->rows, m->columns);
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
new_m->value[i][j][k] = m->value[i][j][k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new_m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* apply_function_new_matrix(Matrix* m, float (*f)(float)) {
|
|
||||||
/* Renvoie une matrice avec une fonction appliquée
|
|
||||||
à tous les éléments de l'ancienne matrice */
|
|
||||||
Matrix* new_m = create_matrix(m->depths, m->rows, m->columns);
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m ->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
new_m->value[i][j][k] = (*f)(m->value[i][j][k]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new_m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void apply_function_matrix(Matrix* m, float (*f)(float)) {
|
|
||||||
/* Applique une fonction à tous les éléments de la matrice */
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m ->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
m->value[i][j][k] = (*f)(m->value[i][j][k]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix* add_matrix(Matrix* m1, Matrix* m2) {
|
|
||||||
/* Renvoie la somme de deux matrices */
|
|
||||||
if (m1->depths != m2->depths || m1->rows != m2->rows || m1->columns != m2->columns) {
|
|
||||||
printf("Erreur, matrices non compatibles avec la somme");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
Matrix* m = create_matrix(m1->depths, m1->rows, m2->columns);
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
m->value[i][j][k] = m1->value[i][j][k] + m2->value[i][j][k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*Matrix* product_matrix(Matrix* m1, Matrix* m2) { // TO DO
|
|
||||||
Renvoie une nouvelle matrice produit (classique)
|
|
||||||
des deux matrices si les dimensions sont correctes
|
|
||||||
if (m1->depths != m2->rows || m1->rows != ) {
|
|
||||||
printf("Erreur, matrices non compatibles avec le produit");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
float cpt;
|
|
||||||
Matrix* m = create_matrix(m1->rows, m2->columns);
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=0; j < m->columns; j++) {
|
|
||||||
cpt=0;
|
|
||||||
for (int k=0; k < m2->rows; k++) {
|
|
||||||
cpt += m1->value[i][j]* m2->value[k][j];
|
|
||||||
}
|
|
||||||
m->value[i][j] = cpt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
void max_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère le résultat de max pooling avec un décalage
|
|
||||||
de (stride) éléments dans la matrice m_out */
|
|
||||||
if (m_in->depths < kernel->depths || m_in->rows < kernel->rows || m_in->columns < kernel->columns) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans max pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->depths - kernel->depths)/stride)+1 != m_out->depths || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows || ((m_in->columns - kernel->columns)/stride)+1 != m_out->columns) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans max pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, k, a, b, c;
|
|
||||||
float tmp;
|
|
||||||
for (i=0; i < m_out->depths; i++) {
|
|
||||||
for (j=0; j < m_out->rows; j++) {
|
|
||||||
for (k=0; k < m_out->columns; k++) {
|
|
||||||
tmp = FLT_MIN;
|
|
||||||
for (a=0; a < kernel->depths; a++) {
|
|
||||||
for (b=0; b < kernel->rows; b++) {
|
|
||||||
for (c=0; c < kernel->columns; c++) {
|
|
||||||
tmp = max_float(tmp, m_in->value[i*stride +a][j*stride +b][k*stride +c]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_out->value[i][j][k] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void min_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère le résultat de min pooling avec un décalage
|
|
||||||
de (stride) éléments dans la matrice m_out */
|
|
||||||
if (m_in->depths < kernel->depths || m_in->rows < kernel->rows || m_in->columns < kernel->columns) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans min pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->depths - kernel->depths)/stride)+1 != m_out->depths || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows || ((m_in->columns - kernel->columns)/stride)+1 != m_out->columns) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans min pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, k, a, b, c;
|
|
||||||
float tmp;
|
|
||||||
for (i=0; i < m_out->depths; i++) {
|
|
||||||
for (j=0; j < m_out->rows; j++) {
|
|
||||||
for (k=0; k < m_out->columns; k++) {
|
|
||||||
tmp = FLT_MAX;
|
|
||||||
for (a=0; a < kernel->depths; a++) {
|
|
||||||
for (b=0; b < kernel->rows; b++) {
|
|
||||||
for (c=0; c < kernel->columns; c++) {
|
|
||||||
tmp = min_float(tmp, m_in->value[i*stride +a][j*stride +b][k*stride +c]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_out->value[i][j][k] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void average_pooling_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère le résultat de average pooling avec un décalage
|
|
||||||
de (stride) éléments dans la matrice m_out */
|
|
||||||
if (m_in->depths < kernel->depths || m_in->rows < kernel->rows || m_in->columns < kernel->columns) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans average pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->depths - kernel->depths)/stride)+1 != m_out->depths || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows || ((m_in->columns - kernel->columns)/stride)+1 != m_out->columns) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans average pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, k, a, b, c, nb=kernel->depths*kernel->rows*kernel->columns;
|
|
||||||
float tmp;
|
|
||||||
for (i=0; i < m_out->depths; i++) {
|
|
||||||
for (j=0; j < m_out->rows; j++) {
|
|
||||||
for (k=0; k < m_out->columns; k++) {
|
|
||||||
tmp = 0;
|
|
||||||
for (a=0; a < kernel->depths; a++) {
|
|
||||||
for (b=0; b < kernel->rows; b++) {
|
|
||||||
for (c=0; c < kernel->columns; c++) {
|
|
||||||
tmp += m_in->value[i*stride +a][j*stride +b][k*stride +c];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_out->value[i][j][k] = tmp/nb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void valid_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère, la cross-correlation valide de m_in et
|
|
||||||
kernel avec un décalage de stride, dans m_out */
|
|
||||||
if (m_in->depths < kernel->depths || m_in->rows < kernel->rows || m_in->columns < kernel->columns) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans valid cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (((m_in->depths - kernel->depths)/stride)+1 != m_out->depths || ((m_in->rows - kernel->rows)/stride)+1 != m_out->rows || ((m_in->columns - kernel->columns)/stride)+1 != m_out->columns) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans valid cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, k, a, b, c, new_i, new_j, new_k;
|
|
||||||
for (i=0; i < m_out->depths; i++) {
|
|
||||||
for (j=0; j < m_out->rows; j++) {
|
|
||||||
for (k=0; k < m_out->columns; k++) {
|
|
||||||
m_out->value[i][j][k] = 0;
|
|
||||||
for (a=0; a < kernel->depths; a++) {
|
|
||||||
for (b=0; b < kernel->rows; b++) {
|
|
||||||
for (c=0; c < kernel->columns; c++) {
|
|
||||||
m_out->value[i][j][k] += m_in->value[i*stride +a][j*stride +b][k*stride +c]*kernel->value[a][b][c];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void full_cross_correlation_matrix(Matrix* m_in, Matrix* kernel, int stride, Matrix* m_out) {
|
|
||||||
/* Insère, la cross-correlation entière de m_in et
|
|
||||||
kernel avec un décalage de stride, dans m_out */
|
|
||||||
int rows_k = kernel->rows-1;
|
|
||||||
int columns_k = kernel->columns-1;
|
|
||||||
int depths_k = kernel->depths-1;
|
|
||||||
if (m_in->depths < kernel->depths || m_in->rows < kernel->rows || m_in->columns < kernel->columns) {
|
|
||||||
printf("Erreur, kernel plus grand que la matrice dans full cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ((m_in->depths + 2*depths_k)/stride != m_out->depths || (m_in->rows + 2*rows_k)/stride != m_out->rows || (m_in->columns + 2*columns_k)/stride != m_out->columns) {
|
|
||||||
printf("Erreur, matrice et kernel non compatibles avec le décalage ou la matrice sortante dans full cross-correlation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, k, a, b, c, new_i, new_j, new_k;
|
|
||||||
for (i=-depths_k; i < (m_out->depths + depths_k); i++) {
|
|
||||||
for (j=-rows_k; j < (m_out->rows + rows_k); j++) {
|
|
||||||
for (k=--columns_k; k < (m_out->columns + columns_k); k++) {
|
|
||||||
m_out->value[i+rows_k][j+columns_k] = 0;
|
|
||||||
for (a=0; a < kernel->depths; a++) {
|
|
||||||
for (b=0; b < kernel->rows; b++) {
|
|
||||||
for (c=0; c < kernel->columns; c++) {
|
|
||||||
new_i = i*stride +a;
|
|
||||||
new_j = j*stride +b;
|
|
||||||
new_k = k*stride +c;
|
|
||||||
if (new_k >= 0 || new_k < m_in->columns || new_i >= 0 || new_i < m_in->depths || new_j >= 0 || new_j < m_in->rows)
|
|
||||||
m_out->value[i+depths_k][j+rows_k][k+columns_k] += m_in->value[new_i][new_j][new_k]*kernel->value[a][b][c];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void softmax_matrix(Matrix* m) {
|
|
||||||
/* Applique la fonction softmax sur la matrice en changeant ses valeurs */
|
|
||||||
float max = max_in_matrix(m);
|
|
||||||
sum_of_a_scalar_matrix(m, (-1)*max);
|
|
||||||
apply_function_matrix(m, exp_float);
|
|
||||||
float sum = number_from_matrix(m);
|
|
||||||
sum = 1/sum;
|
|
||||||
product_of_a_scalar_matrix(m, sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float quadratic_cost_matrix(Matrix* m, int i_number, int j_number, int k_number) {
|
|
||||||
/* Renvoie l'erreur de la matrice où les valeurs sont des probabailités */
|
|
||||||
float loss = 0;
|
|
||||||
for (int i=0; i < m->depths; i++) {
|
|
||||||
for (int j=0; j < m->rows; j++) {
|
|
||||||
for (int k=0; k < m->columns; k++) {
|
|
||||||
if (i==i_number && j==j_number && k==k_number)
|
|
||||||
loss += (1-m->value[i][j][k])*(1-m->value[i][j][k]);
|
|
||||||
else
|
|
||||||
loss += m->value[i][j][k]*m->value[i][j][k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return loss;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*void rotation_180_matrix(Matrix* m) { // TO DO
|
|
||||||
if (m->rows != m-> columns) {
|
|
||||||
printf("Erreur, une matrice non carrée ne peut pas être retourner");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
float tmp;
|
|
||||||
int half_rows = m->rows/2;
|
|
||||||
int max_r = m->rows-1;
|
|
||||||
int max_c = m->columns-1;
|
|
||||||
for (int i=0; i < m->rows; i++) {
|
|
||||||
for (int j=i; j < m->columns; j++) {
|
|
||||||
if (i!=j || i>=half_rows) {
|
|
||||||
tmp = m->value[i][j];
|
|
||||||
m->value[i][j] = m->value[max_r-i][max_c-j];
|
|
||||||
m->value[max_r-i][max_c-j] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
Matrix* m = create_matrix(3, 3, 3);
|
|
||||||
m->value[0][1][2]=10;
|
|
||||||
softmax_matrix(m);
|
|
||||||
print_matrix(m);
|
|
||||||
free_matrix(m);
|
|
||||||
return 1;
|
|
||||||
}
|
|
127
src/test.c
127
src/test.c
@ -1,127 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
|
||||||
typedef struct Neuron{
|
|
||||||
float bias; // Caractérise le bias du neurone
|
|
||||||
float z; // Sauvegarde des calculs faits sur le neurone (programmation dynamique)
|
|
||||||
|
|
||||||
float back_bias; // Changement du bias lors de la backpropagation
|
|
||||||
float last_back_bias; // Dernier changement de back_bias
|
|
||||||
} Neuron;
|
|
||||||
|
|
||||||
typedef struct Bias{
|
|
||||||
float bias;
|
|
||||||
float back_bias;
|
|
||||||
float last_back_bias;
|
|
||||||
} Bias;
|
|
||||||
|
|
||||||
typedef struct Matrix {
|
|
||||||
int rows; // Nombre de lignes de la matrice
|
|
||||||
int columns; // Nombre de colonnes de la matrice
|
|
||||||
float** value; // Tableau 2d comportant les valeurs de matrice
|
|
||||||
} Matrix;
|
|
||||||
|
|
||||||
typedef struct Matrix_of_neurons {
|
|
||||||
int rows; // Nombre de lignes de la matrice
|
|
||||||
int columns; // Nombre de colonnes de la matrice
|
|
||||||
float** Neuron; // Tableau 2d comportant les valeurs de matrice
|
|
||||||
} Matrix_of_neurons;
|
|
||||||
|
|
||||||
typedef struct Matrix_of_bias {
|
|
||||||
int rows;
|
|
||||||
int columns;
|
|
||||||
float*** bias;
|
|
||||||
} Matrix_of_bias;
|
|
||||||
|
|
||||||
typedef struct Layer {
|
|
||||||
int rows; // Nombre de matrices du tableau de neurones
|
|
||||||
Matrix** conv; // Tableau de matrices des neurones dans la couche
|
|
||||||
} Layer;
|
|
||||||
|
|
||||||
typedef struct Filter {
|
|
||||||
int columns;
|
|
||||||
int rows;
|
|
||||||
int dim_bias;
|
|
||||||
Matrix_of_neurons*** kernel; // De dimension columns*rows
|
|
||||||
Matrix_of_bias** bias; // De dimension columns
|
|
||||||
} Filter;
|
|
||||||
|
|
||||||
typedef struct Network{
|
|
||||||
int dropout; // Contains the probability of dropout bewteen 0 and 100
|
|
||||||
int max_size;
|
|
||||||
int size; // Taille total du réseau
|
|
||||||
int size_cnn; // Nombre de couches dans le cnn
|
|
||||||
int* type_kernel; //De taille size -1
|
|
||||||
|
|
||||||
Layer** input; // Tableau des couches dans le réseau neuronal
|
|
||||||
Filter** kernel;
|
|
||||||
} Network;
|
|
||||||
|
|
||||||
void write_image_in_newtork_32(int** image, int height, int width, float** network) {
|
|
||||||
/* Ecrit une image 28*28 au centre d'un tableau 32*32 et met à 0 le reste */
|
|
||||||
|
|
||||||
for (int i=0; i < height+2*PADING_INPUT; i++) {
|
|
||||||
for (int j=PADING_INPUT; j < width+2*PADING_INPUT; j++) {
|
|
||||||
if (i<PADING_INPUT || i>height+PADING_INPUT || j<PADING_INPUT || j>width+PADING_INPUT){
|
|
||||||
network[i][j] = 0.;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
network[i][j] = (float)image[i][j] / 255.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void make_convolution(Layer* input, Filter* filter, Layer* output){
|
|
||||||
/* Effectue une convolution sans stride */
|
|
||||||
if (filter->columns != output->rows) {
|
|
||||||
printf("Erreur, le filtre de la convolution et la sortie ne sont pas compatibles");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (filter->dim_bias != output->rows) {
|
|
||||||
printf("Erreur, le biais et la sortie de la convolution n'ont pas les mêmes dimensions");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// MISS CONDITIONS ON THE CONVOLUTION
|
|
||||||
int i, j, k;
|
|
||||||
for (i=0; i < filter->rows; i++) {
|
|
||||||
for (j=0; j < filter->dim_bias; j++) {
|
|
||||||
for (int k=0; k < filter->dim_bias; k++) {
|
|
||||||
//output->conv[j][k] = filter->bias[i]->bias
|
|
||||||
// COPY BIAS OF FILTERS IN OUTPUT
|
|
||||||
// POUR CHAQUE COLONNE DANS LE KERNEL
|
|
||||||
// ON APPLIQUE LE FILTRE SUR CHAQUE LIGNE DE L'INPUT ET LES SOMMES
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void make_average_pooling(Layer* input, int dim_pooling, Layer* output){
|
|
||||||
/* Effectue un average pooling avec full strides */
|
|
||||||
|
|
||||||
if (input->rows != output->rows || output->conv[0]->rows*dim_pooling != input->conv[0]->rows || input->rows != output->rows) {
|
|
||||||
printf("Erreur, dimension de la sortie et de l'entrée ne sont pas compatibles avec l'average pooling");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int i, j, k, a, b, nb=dim_pooling*dim_pooling;
|
|
||||||
for (i=0; i < input->rows; i++) {
|
|
||||||
for (j=0; j < output->conv[i]->rows; j++) {
|
|
||||||
for (k=0; k < output->conv[i]->columns; k++) {
|
|
||||||
output->conv[i]->value[j][k] = 0;
|
|
||||||
for (a=0; a < dim_pooling; a++) {
|
|
||||||
for (b=0; b < dim_pooling; b++) {
|
|
||||||
output->conv[i]->value[j][k] += input->conv[i]->value[dim_pooling*j + a][dim_pooling*k + b];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
output->conv[i]->value[j][k] /= nb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void forward_propagation_cnn() {
|
|
||||||
/* Effectue une forward propagation d'un cnn */
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user