mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-23 23:26:25 +01:00
Ajout lecture reseau depuis fichier
This commit is contained in:
parent
5756a7a8d6
commit
79609f4687
@ -10,14 +10,13 @@
|
||||
|
||||
|
||||
Neurone* lire_neurone(uint32_t nb_poids_sortants, FILE *ptr) {
|
||||
Neurone* neurone = malloc(sizeof(float)*(6+2*nb_poids_sortants));
|
||||
Neurone* neurone = malloc(sizeof(Neurone));
|
||||
float activation;
|
||||
float biais;
|
||||
float poids_sortants[nb_poids_sortants];
|
||||
float tmp;
|
||||
|
||||
fread(&activation, sizeof(float), 1, ptr);
|
||||
fread(&biais, sizeof(float), 1, ptr);
|
||||
fread(&poids_sortants, sizeof(float)*nb_poids_sortants, 1, ptr);
|
||||
|
||||
neurone->activation = activation;
|
||||
neurone->biais = biais;
|
||||
@ -27,8 +26,14 @@ Neurone* lire_neurone(uint32_t nb_poids_sortants, FILE *ptr) {
|
||||
neurone->dbiais = 0.0;
|
||||
neurone->dz = 0.0;
|
||||
|
||||
float* poids_sortants = malloc(sizeof(float)*nb_poids_sortants);
|
||||
|
||||
neurone->dw = malloc(sizeof(float)*nb_poids_sortants);
|
||||
neurone->poids_sortants = poids_sortants;
|
||||
|
||||
for (int i=0; i < nb_poids_sortants; i++) {
|
||||
neurone->poids_sortants[i] = poids_sortants[i];
|
||||
fread(&tmp, sizeof(float), 1, ptr);
|
||||
neurone->poids_sortants[i] = tmp;
|
||||
neurone->dw[i] = 0.0;
|
||||
}
|
||||
|
||||
@ -39,22 +44,23 @@ Neurone* lire_neurone(uint32_t nb_poids_sortants, FILE *ptr) {
|
||||
// Lit une couche de neurones
|
||||
Neurone** lire_neurones(uint32_t nb_neurones, uint32_t nb_poids_sortants, FILE *ptr) {
|
||||
Neurone** neurones = malloc(sizeof(Neurone*)*nb_neurones);
|
||||
|
||||
for (int i=0; i < nb_neurones; i++) {
|
||||
neurones[i] = lire_neurone(nb_poids_sortants, ptr);
|
||||
}
|
||||
return neurones;
|
||||
}
|
||||
|
||||
|
||||
// Charge l'entièreté du réseau neuronal depuis un fichier binaire
|
||||
Reseau* lire_reseau(char* filename) {
|
||||
FILE *ptr;
|
||||
Reseau* reseau = malloc(sizeof(int)+sizeof(Couche**));
|
||||
Reseau* reseau = malloc(sizeof(Reseau));
|
||||
|
||||
ptr = fopen(filename, "rb");
|
||||
|
||||
uint32_t magic_number;
|
||||
uint32_t nb_couches;
|
||||
uint32_t tmp;
|
||||
reseau->nb_couches = nb_couches;
|
||||
|
||||
fread(&magic_number, sizeof(uint32_t), 1, ptr);
|
||||
@ -71,15 +77,16 @@ Reseau* lire_reseau(char* filename) {
|
||||
|
||||
reseau->couches = couches;
|
||||
|
||||
fread(&nb_neurones_couche, sizeof(nb_neurones_couche)-sizeof(uint32_t), 1, ptr);
|
||||
for (int i=0; i < nb_couches; i++) {
|
||||
couches[i] = malloc(sizeof(int)+sizeof(Neurone**));
|
||||
fread(&tmp, sizeof(tmp), 1, ptr);
|
||||
couches[i]->nb_neurones = tmp;
|
||||
nb_neurones_couche[i] = tmp;
|
||||
}
|
||||
nb_neurones_couche[nb_couches] = 0;
|
||||
|
||||
for (int i=0; i < nb_couches; i++) {
|
||||
couches[i]->nb_neurones = nb_neurones_couche[i];
|
||||
}
|
||||
|
||||
for (int i=0; i < nb_couches; i++) {
|
||||
couches[i]->neurones = lire_neurones(couches[i]->nb_neurones, couches[i+1]->nb_neurones, ptr);
|
||||
couches[i]->neurones = lire_neurones(couches[i]->nb_neurones, nb_neurones_couche[i+1], ptr);
|
||||
}
|
||||
|
||||
fclose(ptr);
|
||||
@ -88,6 +95,7 @@ Reseau* lire_reseau(char* filename) {
|
||||
|
||||
|
||||
|
||||
|
||||
// Écrit un neurone dans le fichier pointé par *ptr
|
||||
void ecrire_neurone(Neurone* neurone, int poids_sortants, FILE *ptr) {
|
||||
float buffer[poids_sortants+2];
|
||||
|
@ -61,5 +61,6 @@ Reseau* creer_reseau(int nb_couches, int nb_max_neurones, int nb_min_neurones) {
|
||||
int main() {
|
||||
Reseau* reseau = creer_reseau(5, 300, 10);
|
||||
ecrire_reseau(".test-cache/neuron_io.bin", reseau);
|
||||
Reseau* reseau2 = lire_reseau(".test-cache/neuron_io.bin");
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user