tipe/test/dense_neuron_io.c

85 lines
2.4 KiB
C
Raw Permalink Normal View History

2022-03-27 14:42:00 +02:00
#include <stdlib.h>
#include <stdio.h>
2022-10-24 13:56:32 +02:00
#include <math.h>
2022-03-27 14:42:00 +02:00
#include <stdint.h>
#include <inttypes.h>
2023-11-10 23:09:48 +01:00
#include "include/test.h"
2023-03-13 13:55:09 +01:00
#include "../src/dense/include/neural_network.h"
2023-05-12 16:16:34 +02:00
#include "../src/dense/include/neuron_io.h"
#include "../src/common/include/colors.h"
2022-03-27 14:42:00 +02:00
2022-04-25 14:39:45 +02:00
Neuron* creer_neuron(int nb_sortants) {
2022-05-14 16:04:35 +02:00
Neuron* neuron = (Neuron*)malloc(sizeof(Neuron));
2023-01-14 14:52:40 +01:00
if (nb_sortants != 0) {
neuron->weights = (float*)malloc(sizeof(float)*nb_sortants);
neuron->back_weights = (float*)malloc(sizeof(float)*nb_sortants);
neuron->last_back_weights = (float*)malloc(sizeof(float)*nb_sortants);
for (int i=0; i < nb_sortants; i++) {
neuron->weights[i] = 0.5;
neuron->back_weights[i] = 0.0;
neuron->last_back_weights[i] = 0.0;
}
neuron->z = 0.0;
neuron->bias = 0.0;
neuron->back_bias = 0.0;
neuron->last_back_bias = 0.0;
2022-03-27 14:42:00 +02:00
}
2022-04-25 14:39:45 +02:00
return neuron;
2022-03-27 14:42:00 +02:00
}
2022-04-25 14:39:45 +02:00
Layer* creer_layer(int nb_neurons, int nb_sortants) {
2022-05-14 16:04:35 +02:00
Layer* layer = (Layer*)malloc(sizeof(Layer));
Neuron** tab = (Neuron**)malloc(sizeof(Neuron*)*nb_neurons);
2022-03-27 14:42:00 +02:00
2022-04-25 14:39:45 +02:00
layer->nb_neurons = nb_neurons;
layer->neurons = tab;
2022-03-27 14:42:00 +02:00
2022-09-10 18:23:16 +02:00
for (int i=0; i < nb_neurons; i++) {
2022-04-25 14:39:45 +02:00
tab[i] = creer_neuron(nb_sortants);
2022-03-27 14:42:00 +02:00
}
2022-04-25 14:39:45 +02:00
return layer;
2022-03-27 14:42:00 +02:00
};
2022-04-25 14:39:45 +02:00
Network* create_network(int nb_layers, int nb_max_neurons, int nb_min_neurons) {
2022-05-14 16:04:35 +02:00
Network* network = (Network*)malloc(sizeof(Network));
network->layers = (Layer**)malloc(sizeof(Layer*)*nb_layers);
2022-04-25 14:39:45 +02:00
int nb_neurons[nb_layers+1];
2022-03-27 14:42:00 +02:00
2022-04-25 14:39:45 +02:00
network->nb_layers = nb_layers;
2022-03-27 14:42:00 +02:00
2022-04-25 14:39:45 +02:00
for (int i=0; i < nb_layers; i++) {
nb_neurons[i] = i*(nb_min_neurons-nb_max_neurons)/(nb_layers-1) + nb_max_neurons;
2022-03-27 14:42:00 +02:00
}
2022-04-25 14:39:45 +02:00
nb_neurons[nb_layers] = 0;
2022-03-27 14:42:00 +02:00
2022-04-25 14:39:45 +02:00
for (int i=0; i < nb_layers; i++) {
network->layers[i] = creer_layer(nb_neurons[i], nb_neurons[i+1]);
2022-03-27 14:42:00 +02:00
}
2022-04-25 14:39:45 +02:00
return network;
2022-03-27 14:42:00 +02:00
}
int main() {
2023-11-10 23:09:48 +01:00
_TEST_PRESENTATION("Dense: Lecture/Écriture")
2022-04-25 14:39:45 +02:00
Network* network = create_network(5, 300, 10);
2023-11-10 23:09:48 +01:00
_TEST_ASSERT(true, "Création du réseau");
2022-05-16 17:26:04 +02:00
write_network((char*)".test-cache/neuron_io.bin", network);
2023-11-10 23:09:48 +01:00
_TEST_ASSERT(true, "Écriture du réseau");
Network* network2 = read_network((char*)".test-cache/neuron_io.bin");
2023-11-10 23:09:48 +01:00
_TEST_ASSERT(true, "Accès en lecture");
2022-05-23 17:27:38 +02:00
deletion_of_network(network);
deletion_of_network(network2);
2023-11-10 23:09:48 +01:00
_TEST_ASSERT(true, "Suppression des réseaux");
2022-05-14 16:04:35 +02:00
return 0;
2022-03-27 14:42:00 +02:00
}