This commit is contained in:
julienChemillier 2022-11-08 20:16:54 +01:00
commit 9d77080de7
6 changed files with 39 additions and 30 deletions

View File

@ -13,8 +13,8 @@ jobs:
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: print versions - name: make all
run: make --version; gcc --version run: make all
- name: run-tests - name: run-tests
run: make run-tests run: make run-tests
- name: mnist main train - name: mnist main train

View File

@ -20,7 +20,7 @@ void write_network(char* filename, Network* network);
/* /*
* Écrit une couche dans le fichier spécifié par le pointeur ptr * Écrit une couche dans le fichier spécifié par le pointeur ptr
*/ */
void write_couche(Kernel* kernel, int type_couche, FILE* ptr); void write_couche(Network* network, int indice_couche, int type_couche, FILE* ptr);
// Lecture d'un réseau neuronal // Lecture d'un réseau neuronal
@ -33,5 +33,5 @@ Network* read_network(char* filename);
/* /*
* Lit une kernel dans le fichier spécifié par le pointeur ptr * Lit une kernel dans le fichier spécifié par le pointeur ptr
*/ */
Kernel* read_kernel(int type_couche, FILE* ptr); Kernel* read_kernel(int type_couche, int output_dim, FILE* ptr);
#endif #endif

View File

@ -47,17 +47,19 @@ void write_network(char* filename, Network* network) {
// Écriture du pré-corps et corps // Écriture du pré-corps et corps
for (int i=0; i < size; i++) { for (int i=0; i < size; i++) {
write_couche(network->kernel[i], type_couche[i], ptr); write_couche(network, i, type_couche[i], ptr);
} }
fclose(ptr); fclose(ptr);
} }
void write_couche(Kernel* kernel, int type_couche, FILE* ptr) { void write_couche(Network* network, int indice_couche, int type_couche, FILE* ptr) {
Kernel* kernel = network->kernel[indice_couche];
int indice_buffer = 0; int indice_buffer = 0;
if (type_couche == 0) { // Cas du CNN if (type_couche == 0) { // Cas du CNN
Kernel_cnn* cnn = kernel->cnn; Kernel_cnn* cnn = kernel->cnn;
int output_dim = network->width[indice_couche];
// Écriture du pré-corps // Écriture du pré-corps
uint32_t pre_buffer[4]; uint32_t pre_buffer[4];
@ -68,11 +70,12 @@ void write_couche(Kernel* kernel, int type_couche, FILE* ptr) {
fwrite(pre_buffer, sizeof(pre_buffer), 1, ptr); fwrite(pre_buffer, sizeof(pre_buffer), 1, ptr);
// Écriture du corps // Écriture du corps
float buffer[cnn->k_size*cnn->k_size*cnn->columns*(cnn->rows+1)]; float buffer[cnn->columns*(cnn->k_size*cnn->k_size*cnn->rows+output_dim*output_dim)];
for (int i=0; i < cnn->columns; i++) { for (int i=0; i < cnn->columns; i++) {
for (int j=0; j < cnn->k_size; j++) { for (int j=0; j < output_dim; j++) {
for (int k=0; k < cnn->k_size; k++) { for (int k=0; k < output_dim; k++) {
printf("%f\n", cnn->bias[i][j][k]);
bufferAdd(cnn->bias[i][j][k]); bufferAdd(cnn->bias[i][j][k]);
} }
} }
@ -166,7 +169,7 @@ Network* read_network(char* filename) {
network->kernel = (Kernel**)malloc(sizeof(Kernel*)*size); network->kernel = (Kernel**)malloc(sizeof(Kernel*)*size);
for (int i=0; i < (int)size; i++) { for (int i=0; i < (int)size; i++) {
network->kernel[i] = read_kernel(type_couche[i], ptr); network->kernel[i] = read_kernel(type_couche[i], network->width[i], ptr);
} }
network->input = (float****)malloc(sizeof(float***)*size); network->input = (float****)malloc(sizeof(float***)*size);
@ -187,7 +190,7 @@ Network* read_network(char* filename) {
return network; return network;
} }
Kernel* read_kernel(int type_couche, FILE* ptr) { Kernel* read_kernel(int type_couche, int output_dim, FILE* ptr) {
Kernel* kernel = (Kernel*)malloc(sizeof(Kernel)); Kernel* kernel = (Kernel*)malloc(sizeof(Kernel));
if (type_couche == 0) { // Cas du CNN if (type_couche == 0) { // Cas du CNN
// Lecture du "Pré-corps" // Lecture du "Pré-corps"
@ -209,12 +212,12 @@ Kernel* read_kernel(int type_couche, FILE* ptr) {
cnn->bias = (float***)malloc(sizeof(float**)*cnn->columns); cnn->bias = (float***)malloc(sizeof(float**)*cnn->columns);
cnn->d_bias = (float***)malloc(sizeof(float**)*cnn->columns); cnn->d_bias = (float***)malloc(sizeof(float**)*cnn->columns);
for (int i=0; i < cnn->columns; i++) { for (int i=0; i < cnn->columns; i++) {
cnn->bias[i] = (float**)malloc(sizeof(float*)*cnn->k_size); cnn->bias[i] = (float**)malloc(sizeof(float*)*output_dim);
cnn->d_bias[i] = (float**)malloc(sizeof(float*)*cnn->k_size); cnn->d_bias[i] = (float**)malloc(sizeof(float*)*output_dim);
for (int j=0; j < cnn->k_size; j++) { for (int j=0; j < output_dim; j++) {
cnn->bias[i][j] = (float*)malloc(sizeof(float)*cnn->k_size); cnn->bias[i][j] = (float*)malloc(sizeof(float)*output_dim);
cnn->d_bias[i][j] = (float*)malloc(sizeof(float)*cnn->k_size); cnn->d_bias[i][j] = (float*)malloc(sizeof(float)*output_dim);
for (int k=0; k < cnn->k_size; k++) { for (int k=0; k < output_dim; k++) {
fread(&tmp, sizeof(tmp), 1, ptr); fread(&tmp, sizeof(tmp), 1, ptr);
cnn->bias[i][j][k] = tmp; cnn->bias[i][j][k] = tmp;
cnn->d_bias[i][j][k] = 0.; cnn->d_bias[i][j][k] = 0.;

View File

@ -12,12 +12,13 @@
if (network1->var != network2->var) { \ if (network1->var != network2->var) { \
printf_error("network1->" name " et network2->" name " ne sont pas égaux\n"); \ printf_error("network1->" name " et network2->" name " ne sont pas égaux\n"); \
if (indice != -1) { \ if (indice != -1) { \
printf(BOLDBLUE"[ INFO_ ]"RESET" indice: %d\n", indice); \ printf(BOLDBLUE"[ INFO_ ]" RESET " indice: %d\n", indice); \
} \ } \
return false; \ return false; \
} }
bool equals_networks(Network* network1, Network* network2) { bool equals_networks(Network* network1, Network* network2) {
int output_dim;
checkEquals(size, "size", -1); checkEquals(size, "size", -1);
checkEquals(initialisation, "initialisation", -1); checkEquals(initialisation, "initialisation", -1);
checkEquals(dropout, "dropout", -1); checkEquals(dropout, "dropout", -1);
@ -50,12 +51,13 @@ bool equals_networks(Network* network1, Network* network2) {
} }
} else { } else {
// Type CNN // Type CNN
output_dim = network1->width[i];
checkEquals(kernel[i]->cnn->k_size, "kernel[i]->k_size", i); checkEquals(kernel[i]->cnn->k_size, "kernel[i]->k_size", i);
checkEquals(kernel[i]->cnn->rows, "kernel[i]->rows", i); checkEquals(kernel[i]->cnn->rows, "kernel[i]->rows", i);
checkEquals(kernel[i]->cnn->columns, "kernel[i]->columns", i); checkEquals(kernel[i]->cnn->columns, "kernel[i]->columns", i);
for (int j=0; j < network1->kernel[i]->cnn->columns; j++) { for (int j=0; j < network1->kernel[i]->cnn->columns; j++) {
for (int k=0; k < network1->kernel[i]->cnn->k_size; k++) { for (int k=0; k < output_dim; k++) {
for (int l=0; l < network1->kernel[i]->cnn->k_size; l++) { for (int l=0; l < output_dim; l++) {
checkEquals(kernel[i]->cnn->bias[j][k][l], "kernel[i]->cnn->bias[j][k][l]", l); checkEquals(kernel[i]->cnn->bias[j][k][l], "kernel[i]->cnn->bias[j][k][l]", l);
} }
} }
@ -87,6 +89,7 @@ Network* copy_network(Network* network) {
int rows; int rows;
int k_size; int k_size;
int columns; int columns;
int output_dim;
copyVar(dropout); copyVar(dropout);
copyVar(learning_rate); copyVar(learning_rate);
@ -149,6 +152,8 @@ Network* copy_network(Network* network) {
rows = network->kernel[i]->cnn->rows; rows = network->kernel[i]->cnn->rows;
k_size = network->kernel[i]->cnn->k_size; k_size = network->kernel[i]->cnn->k_size;
columns = network->kernel[i]->cnn->columns; columns = network->kernel[i]->cnn->columns;
output_dim = network->width[i];
network_cp->kernel[i]->nn = NULL; network_cp->kernel[i]->nn = NULL;
network_cp->kernel[i]->cnn = (Kernel_cnn*)malloc(sizeof(Kernel_cnn)); network_cp->kernel[i]->cnn = (Kernel_cnn*)malloc(sizeof(Kernel_cnn));
@ -160,12 +165,12 @@ Network* copy_network(Network* network) {
network_cp->kernel[i]->cnn->bias = (float***)malloc(sizeof(float**)*columns); network_cp->kernel[i]->cnn->bias = (float***)malloc(sizeof(float**)*columns);
network_cp->kernel[i]->cnn->d_bias = (float***)malloc(sizeof(float**)*columns); network_cp->kernel[i]->cnn->d_bias = (float***)malloc(sizeof(float**)*columns);
for (int j=0; j < columns; j++) { for (int j=0; j < columns; j++) {
network_cp->kernel[i]->cnn->bias[j] = (float**)malloc(sizeof(float*)*k_size); network_cp->kernel[i]->cnn->bias[j] = (float**)malloc(sizeof(float*)*output_dim);
network_cp->kernel[i]->cnn->d_bias[j] = (float**)malloc(sizeof(float*)*k_size); network_cp->kernel[i]->cnn->d_bias[j] = (float**)malloc(sizeof(float*)*output_dim);
for (int k=0; k < k_size; k++) { for (int k=0; k < output_dim; k++) {
network_cp->kernel[i]->cnn->bias[j][k] = (float*)malloc(sizeof(float)*k_size); network_cp->kernel[i]->cnn->bias[j][k] = (float*)malloc(sizeof(float)*output_dim);
network_cp->kernel[i]->cnn->d_bias[j][k] = (float*)malloc(sizeof(float)*k_size); network_cp->kernel[i]->cnn->d_bias[j][k] = (float*)malloc(sizeof(float)*output_dim);
for (int l=0; l < k_size; l++) { for (int l=0; l < output_dim; l++) {
copyVar(kernel[i]->cnn->bias[j][k][l]); copyVar(kernel[i]->cnn->bias[j][k][l]);
network_cp->kernel[i]->cnn->d_bias[j][k][l] = 0.; network_cp->kernel[i]->cnn->d_bias[j][k][l] = 0.;
} }

View File

@ -78,7 +78,7 @@ void count_labels(char* filename) {
} }
for (int i=0; i < 10; i++) { for (int i=0; i < 10; i++) {
printf("Nombre de %d: %x\n", i, tab[i]); printf("Nombre de %d: %u\n", i, tab[i]);
} }
free(labels); free(labels);
} }

View File

@ -3,14 +3,15 @@
set -e set -e
OUT="build" OUT="build"
[[ -f "$OUT/mnist-utils" ]] || make $OUT/mnist-utils make $OUT/mnist-utils
echo "Compte des labels" echo "Compte des labels"
"$OUT/mnist-utils" count-labels -l data/mnist/t10k-labels-idx1-ubyte > /dev/null "$OUT/mnist-utils" count-labels -l data/mnist/t10k-labels-idx1-ubyte
echo "OK" echo "OK"
echo "Création du réseau" echo "Création du réseau"
"$OUT/mnist-utils" creer-reseau -n 3 -o .test-cache/reseau.bin > /dev/null mkdir -p .test-cache
"$OUT/mnist-utils" creer-reseau -n 3 -o .test-cache/reseau.bin
echo "OK" echo "OK"
echo "Affichage poids" echo "Affichage poids"