2022-04-19 13:55:08 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "neural_network.c"
|
|
|
|
#include "neuron_io.c"
|
2022-04-22 15:03:21 +02:00
|
|
|
#include "mnist.c"
|
2022-04-19 13:55:08 +02:00
|
|
|
|
2022-04-19 21:30:23 +02:00
|
|
|
/*
|
|
|
|
Contient un ensemble de fonctions utiles pour le débogage
|
|
|
|
*/
|
|
|
|
void help(char* call) {
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("Usage: %s ( print-poids | print-biais | creer-reseau ) [OPTIONS]\n\n", call);
|
2022-04-19 21:30:23 +02:00
|
|
|
printf("OPTIONS:\n");
|
|
|
|
printf("\tprint-poids:\n");
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("\t\t--reseau | -r [FILENAME]\tFichier contenant le réseau de neurones.\n");
|
|
|
|
printf("\tprint-biais:\n");
|
|
|
|
printf("\t\t--reseau | -r [FILENAME]\tFichier contenant le réseau de neurones.\n");
|
2022-04-22 15:03:21 +02:00
|
|
|
printf("\tcount-labels:\n");
|
|
|
|
printf("\t\t--labels | -l [FILENAME]\tFichier contenant les labels.\n");
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("\tcreer-reseau:\n");
|
|
|
|
printf("\t\t--out | -o [FILENAME]\tFichier où écrire le réseau de neurones.\n");
|
2022-04-19 21:30:23 +02:00
|
|
|
printf("\t\t--number | -n [int]\tNuméro à privilégier\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-25 14:39:45 +02:00
|
|
|
void print_bias(char* filename) {
|
|
|
|
Network* network = read_network(".cache/network.bin");
|
2022-04-19 13:55:08 +02:00
|
|
|
|
2022-04-25 14:39:45 +02:00
|
|
|
for (int i=1; i < network->nb_layers -1; i++) {
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("Couche %d\n", i);
|
2022-04-25 14:39:45 +02:00
|
|
|
for (int j=0; j < network->layers[i]->nb_neurons; j++) {
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("Couche %d\tNeurone %d\tBiais: %f\n", i, j, network->layers[i]->neurons[j]->bias);
|
2022-04-19 13:55:08 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 14:39:45 +02:00
|
|
|
deletion_of_network(network);
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_poids(char* filename) {
|
2022-04-25 14:39:45 +02:00
|
|
|
Network* network = read_network(".cache/network.bin");
|
|
|
|
|
|
|
|
for (int i=0; i < network->nb_layers -1; i++) {
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("Couche %d\n", i);
|
2022-04-25 14:39:45 +02:00
|
|
|
for (int j=0; j < network->layers[i]->nb_neurons; j++) {
|
2022-04-30 09:31:52 +02:00
|
|
|
printf("Couche %d\tNeurone %d\tPoids: [", i, j);
|
2022-04-25 14:39:45 +02:00
|
|
|
for (int k=0; k < network->layers[i+1]->nb_neurons; k++) {
|
|
|
|
printf("%f, ", network->layers[i]->neurons[j]->weights[k]);
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 14:39:45 +02:00
|
|
|
deletion_of_network(network);
|
2022-04-19 13:55:08 +02:00
|
|
|
}
|
|
|
|
|
2022-04-22 15:03:21 +02:00
|
|
|
void count_labels(char* filename) {
|
|
|
|
uint32_t number_of_images = read_mnist_labels_nb_images(filename);
|
|
|
|
|
|
|
|
unsigned int* labels = malloc(sizeof(unsigned int)*number_of_images);
|
|
|
|
labels = read_mnist_labels(filename);
|
|
|
|
|
|
|
|
unsigned int* tab[10];
|
|
|
|
|
|
|
|
for (int i=0; i < 10; i++) {
|
|
|
|
tab[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i < number_of_images; i++) {
|
|
|
|
tab[(int)labels[i]]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i < 10; i++) {
|
|
|
|
printf("Nombre de %d: %d\n", i, tab[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 14:39:45 +02:00
|
|
|
void create_network(char* filename, int sortie) {
|
|
|
|
Network* network = malloc(sizeof(Network));
|
|
|
|
Layer* layer;
|
|
|
|
Neuron* neuron;
|
|
|
|
network->nb_layers = 3;
|
2022-04-19 21:30:23 +02:00
|
|
|
|
2022-04-25 14:39:45 +02:00
|
|
|
network->layers = malloc(sizeof(Layer*)*network->nb_layers);
|
|
|
|
int neurons_per_layer[4] = {784, 1, 10, 0};
|
|
|
|
for (int i=0; i < network->nb_layers; i++) {
|
|
|
|
network->layers[i] = malloc(sizeof(Layer));
|
|
|
|
layer = network->layers[i];
|
|
|
|
layer->nb_neurons = neurons_per_layer[i];
|
|
|
|
layer->neurons = malloc(sizeof(Neuron*)*layer->nb_neurons);
|
|
|
|
for (int j=0; j < layer->nb_neurons; j++) {
|
|
|
|
layer->neurons[j] = malloc(sizeof(Neuron));
|
|
|
|
neuron = layer->neurons[j];
|
|
|
|
|
|
|
|
neuron->bias = 0.;
|
|
|
|
neuron->z = 0.;
|
|
|
|
|
|
|
|
neuron->back_bias = 0.;
|
|
|
|
neuron->last_back_bias = 0.;
|
|
|
|
|
|
|
|
neuron->weights = malloc(sizeof(float)*neurons_per_layer[i+1]);
|
|
|
|
neuron->back_weights = malloc(sizeof(float)*neurons_per_layer[i+1]);
|
|
|
|
neuron->last_back_weights = malloc(sizeof(float)*neurons_per_layer[i+1]);
|
|
|
|
for (int k=0; k < neurons_per_layer[i+1]; k++) {
|
|
|
|
neuron->weights[k] = 0.;
|
|
|
|
neuron->back_weights[k] = 0.;
|
|
|
|
neuron->last_back_weights[k] = 0.;
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 14:39:45 +02:00
|
|
|
for (int j=0; j < neurons_per_layer[0]; j++) {
|
|
|
|
network->layers[0]->neurons[j]->weights[0] = 1;
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
2022-04-25 14:39:45 +02:00
|
|
|
network->layers[1]->neurons[0]->weights[sortie] = 1;
|
|
|
|
write_network(filename, network);
|
|
|
|
deletion_of_network(network);
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-19 16:26:28 +02:00
|
|
|
int main(int argc, char* argv[]) {
|
2022-04-19 21:30:23 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
printf("Pas d'action spécifiée\n");
|
|
|
|
help(argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (! strcmp(argv[1], "print-poids")) {
|
|
|
|
char* filename = NULL;
|
|
|
|
int i = 2;
|
|
|
|
while (i < argc) {
|
2022-04-30 09:31:52 +02:00
|
|
|
if ((! strcmp(argv[i], "--reseau"))||(! strcmp(argv[i], "-r"))) {
|
2022-04-19 21:30:23 +02:00
|
|
|
filename = argv[i+1];
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
printf("%s : Argument non reconnu\n", argv[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! filename) {
|
2022-04-25 14:39:45 +02:00
|
|
|
printf("Pas de fichier spécifié, utilisation de '.cache/network.bin'\n");
|
|
|
|
filename = ".cache/network.bin";
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
|
|
|
print_poids(filename);
|
|
|
|
exit(1);
|
2022-04-30 09:31:52 +02:00
|
|
|
} else if (! strcmp(argv[1], "print-biais")) {
|
2022-04-19 21:30:23 +02:00
|
|
|
char* filename = NULL;
|
|
|
|
int i = 2;
|
|
|
|
while (i < argc) {
|
2022-04-30 09:31:52 +02:00
|
|
|
if ((! strcmp(argv[i], "--reseau"))||(! strcmp(argv[i], "-r"))) {
|
2022-04-19 21:30:23 +02:00
|
|
|
filename = argv[i+1];
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
printf("%s : Argument non reconnu\n", argv[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! filename) {
|
2022-04-25 14:39:45 +02:00
|
|
|
printf("Pas de fichier spécifié, utilisation de '.cache/network.bin'\n");
|
|
|
|
filename = ".cache/network.bin";
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
2022-04-25 14:39:45 +02:00
|
|
|
print_bias(filename);
|
2022-04-19 21:30:23 +02:00
|
|
|
exit(1);
|
2022-04-30 09:31:52 +02:00
|
|
|
} else if (! strcmp(argv[1], "creer-reseau")) {
|
2022-04-19 21:30:23 +02:00
|
|
|
char* out = NULL;
|
|
|
|
int n = -1;
|
|
|
|
int i = 2;
|
|
|
|
while (i < argc) {
|
|
|
|
if ((! strcmp(argv[i], "--out"))||(! strcmp(argv[i], "-o"))) {
|
|
|
|
out = argv[i+1];
|
|
|
|
i += 2;
|
|
|
|
} else if ((! strcmp(argv[i], "--number"))||(! strcmp(argv[i], "-n"))) {
|
|
|
|
n = strtol(argv[i+1], NULL, 10);
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
printf("%s : Argument non reconnu\n", argv[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 15:03:21 +02:00
|
|
|
} else if (! strcmp(argv[1], "count-labels")) {
|
|
|
|
char* labels = NULL;
|
|
|
|
int i = 2;
|
|
|
|
while (i < argc) {
|
|
|
|
if ((! strcmp(argv[i], "--labels"))||(! strcmp(argv[i], "-l"))) {
|
|
|
|
labels = argv[i+1];
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
printf("%s : Argument non reconnu\n", argv[i]);
|
|
|
|
i++;
|
|
|
|
}
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
2022-04-22 15:03:21 +02:00
|
|
|
if (! labels) {
|
|
|
|
printf("Pas de fichier spécifié, défaut: 'data/mnist/train-labels-idx1-ubyte'\n");
|
|
|
|
labels = "data/mnist/train-labels-idx1-ubyte";
|
2022-04-19 21:30:23 +02:00
|
|
|
}
|
2022-04-22 15:03:21 +02:00
|
|
|
count_labels(labels);
|
2022-04-19 21:30:23 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Option choisie non reconnue: %s\n", argv[1]);
|
|
|
|
help(argv[0]);
|
2022-04-19 13:55:08 +02:00
|
|
|
return 1;
|
|
|
|
}
|