From d94f61bb772ecb25a8fc239f35e61d70616ac191 Mon Sep 17 00:00:00 2001
From: julienChemillier <chemillier.julien@gmail.com>
Date: Tue, 15 Nov 2022 12:50:38 +0100
Subject: [PATCH] First results in backprop ?

---
 src/cnn/make.c   |  3 +--
 src/cnn/train.c  | 13 ++++++++++---
 src/cnn/update.c | 23 ++++++++++++-----------
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/cnn/make.c b/src/cnn/make.c
index 855cac7..b403d3c 100644
--- a/src/cnn/make.c
+++ b/src/cnn/make.c
@@ -10,7 +10,6 @@ void make_average_pooling(float*** input, float*** output, int size, int output_
     // output[output_depth][output_dim][output_dim]
     float average;
     int n = size*size;
-
     for (int i=0; i < output_depth; i++) {
         for (int j=0; j < output_dim; j++) {
             for (int k=0; k < output_dim; k++) {
@@ -20,7 +19,7 @@ void make_average_pooling(float*** input, float*** output, int size, int output_
                         average += input[i][size*j +a][size*k +b];
                     }
                 }
-                output[i][j][k] = average/n;
+                output[i][j][k] = average/(float)n;
             }
         }
     }
diff --git a/src/cnn/train.c b/src/cnn/train.c
index b4aad18..afdcd98 100644
--- a/src/cnn/train.c
+++ b/src/cnn/train.c
@@ -10,6 +10,7 @@
 #include "../include/colors.h"
 #include "include/function.h"
 #include "include/creation.h"
+#include "include/update.h"
 #include "include/utils.h"
 #include "include/free.h"
 #include "include/cnn.h"
@@ -45,14 +46,20 @@ void* train_thread(void* parameters) {
     int start = param->start;
     int nb_images = param->nb_images;
     float accuracy = 0.;
-
+    int cpt=1;
     for (int i=start;  i < start+nb_images; i++) {
         if (dataset_type == 0) {
             write_image_in_network_32(images[i], height, width, network->input[0][0]);
             forward_propagation(network);
+            maxi = indice_max(network, 10);
             backward_propagation(network, labels[i]);
-
-            maxi = indice_max(network->input[network->size-1][0][0], network->width[network->size-1]);
+            if (cpt==16) { // Update the network
+                printf("a\n");
+                update_weights(network);
+                update_bias(network);
+                cpt = 0;
+            }
+            cpt++;
             if (maxi == labels[i]) {
                 accuracy += 1.;
             }
diff --git a/src/cnn/update.c b/src/cnn/update.c
index f3ab286..0e2e2bf 100644
--- a/src/cnn/update.c
+++ b/src/cnn/update.c
@@ -2,22 +2,25 @@
 #include "include/update.h"
 #include "include/struct.h"
 
+
+#include <stdio.h>
+
+
+
 void update_weights(Network* network) {
     int n = network->size;
-    int input_depth, input_width, output_depth, output_width;
+    int input_depth, input_width, output_depth, output_width, k_size;
     Kernel* k_i;
-    Kernel* k_i_1;
     for (int i=0; i<(n-1); i++) {
         k_i = network->kernel[i];
-        k_i_1 = network->kernel[i+1];
         input_depth = network->depth[i];
         input_width = network->width[i];
         output_depth = network->depth[i+1];
         output_width = network->width[i+1];
 
         if (k_i->cnn) { // Convolution
-            Kernel_cnn* cnn = k_i_1->cnn;
-            int k_size = cnn->k_size;
+            Kernel_cnn* cnn = k_i->cnn; // ERRORS
+            k_size = cnn->k_size;
             for (int a=0; a<input_depth; a++) {
                 for (int b=0; b<output_depth; b++) {
                     for (int c=0; c<k_size; c++) {
@@ -30,7 +33,7 @@ void update_weights(Network* network) {
             }
         } else if (k_i->nn) { // Full connection
             if (input_depth==1) { // Vecteur -> Vecteur
-                Kernel_nn* nn = k_i_1->nn;
+                Kernel_nn* nn = k_i->nn;
                 for (int a=0; a<input_width; a++) {
                     for (int b=0; b<output_width; b++) {
                         nn->weights[a][b] += network->learning_rate * nn->d_weights[a][b];
@@ -38,7 +41,7 @@ void update_weights(Network* network) {
                     }
                 }
             } else { // Matrice -> vecteur
-                Kernel_nn* nn = k_i_1->nn;
+                Kernel_nn* nn = k_i->nn;
                 int input_size = input_width*input_width*input_depth;
                 for (int a=0; a<input_size; a++) {
                     for (int b=0; b<output_width; b++) {
@@ -57,15 +60,13 @@ void update_bias(Network* network) {
     int n = network->size;
     int output_width, output_depth;
     Kernel* k_i;
-    Kernel* k_i_1;
     for (int i=0; i<(n-1); i++) {
         k_i = network->kernel[i];
-        k_i_1 = network->kernel[i+1];
         output_width = network->width[i+1];
         output_depth = network->depth[i+1];
 
         if (k_i->cnn) { // Convolution
-            Kernel_cnn* cnn = k_i_1->cnn;
+            Kernel_cnn* cnn = k_i->cnn;
             for (int a=0; a<output_depth; a++) {
                 for (int b=0; b<output_width; b++) {
                     for (int c=0; c<output_width; c++) {
@@ -75,7 +76,7 @@ void update_bias(Network* network) {
                 }
             }
         } else if (k_i->nn) { // Full connection
-            Kernel_nn* nn = k_i_1->nn;
+            Kernel_nn* nn = k_i->nn;
             for (int a=0; a<output_width; a++) {
                 nn->bias[a] += network->learning_rate * nn->d_bias[a];
                 nn->d_bias[a] = 0;