From 017fb23ca1fcd3f57f295a33040da4529b7a55af Mon Sep 17 00:00:00 2001
From: Dubray Chloe <chloe.dubray@etu.ec-lyon.fr>
Date: Tue, 7 Nov 2023 15:48:37 +0000
Subject: [PATCH] Update mlp.py

---
 mlp.py | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/mlp.py b/mlp.py
index e01b908..36a2e4b 100644
--- a/mlp.py
+++ b/mlp.py
@@ -2,13 +2,13 @@ import numpy as np
 from read_cifar import *
 import matplotlib.pyplot as plt
 
-d_h = 64
-epsilon = 0.00001
-
+#Définition de la fonction sigmoïde
 def sigma (z) :
     return 1 / (1 + np.exp(-z))
 
+#Rétropropagation : une seule boucle avec la fonction MSE comme fonction de coût
 def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
+    #Forward pass
     a0 = data
     z1 = np.matmul(a0,w1) + b1
     a1 = sigma (z1)
@@ -18,6 +18,7 @@ def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
 
     d_out = np.shape(targets)[1] 
 
+    #Backpropagation
     dC_da2 = 2*(a2-targets)/d_out
     dC_dz2 = dC_da2 * a2 * (1-a2)
     dC_dw2 = np.matmul(a1.T, dC_dz2)
@@ -37,6 +38,7 @@ def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
     
     return (w1, b1, w2, b2, loss)
 
+#Définition de la fonction d'encodage one-hot
 def one_hot(labels, num_classes=None):
     if num_classes is None:
         num_classes = np.max(labels) + 1
@@ -45,6 +47,7 @@ def one_hot(labels, num_classes=None):
         one_hot_matrix[i,labels[i]]=1
     return one_hot_matrix
 
+#Rétropopagation : une seule boucle avec la fonction de perte d'entropie croisée
 def learn_one_cross_entropy (w1, b1, w2, b2, data, labels_train, learning_rate) :
     a0 = data
     z1 = np.matmul(a0,w1) + b1
@@ -72,20 +75,25 @@ def learn_one_cross_entropy (w1, b1, w2, b2, data, labels_train, learning_rate)
     w2 -= learning_rate * dC_dw2
     b2 -= learning_rate * dC_db2
 
+    #Ajout d'un coefficient epsilon très faible dans la fonction de coût pour éviter les problèmes de division par zéro
+    epsilon = 0.00001
     loss = -np.sum(y * np.log2(predictions + epsilon) + (1 - y) * np.log2(1 - predictions + epsilon)) / N
 
     return (w1, b1, w2, b2, loss)
 
-
+#Fonction de prédiction qui pour un vecteur donné renvoie la classe prédite (cad l'indice de l'élément le plus élevé)
 def predict_class(predictions):
     return np.argmax(predictions, axis=1)
 
+#Fonction taux de réussite qui compare une liste de prédictions à la liste des résultats et renvoie la proportion de vraies prédictions
 def accuracy(y_true, y_pred):
     return np.mean(y_true == y_pred)
 
+#Fonction qui réalise num_epoch boucles de rétropropagation avec la fonction learn_one_cross_entropy
 def train_mlp (w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epoch) :
 
     training_accuracies = []
+
     for k in range(num_epoch) :
         w1_new, b1_new, w2_new, b2_new, loss = learn_one_cross_entropy (w1, b1, w2, b2, data_train, labels_train, learning_rate)
 
@@ -104,6 +112,7 @@ def train_mlp (w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epoc
 
     return (w1, b1, w2, b2, training_accuracies)
 
+#Fonction qui renvoie le taux de réussite de la prédiction du réseau de neurones sur un jeu de test avec des matrices poids et biais données
 def test_mlp (w1, b1, w2, b2, data_test, labels_test) :
     a0 = data_test
     z1 = np.matmul(a0,w1) + b1
@@ -116,7 +125,7 @@ def test_mlp (w1, b1, w2, b2, data_test, labels_test) :
 
     return (test_accuracy)
 
-
+#Fonction qui fait num_epoch boucles de rétropropagation, renvoie le taux de réussite de l'entraînement à chaque boucle et enfin le taux de réussite sur le jeu de test 
 def run_mlp_training (data_train, labels_train, data_test, labels_test, d_h :int, learning_rate, num_epoch) :
     d_in = (np.shape(data_train))[1]
     d_out = np.max(labels_train)+1
@@ -138,6 +147,7 @@ if __name__ == "__main__":
     learning_rate = 0.1
     num_epoch = 100
     split_factor=0.9
+    d_h = 64
 
     batch_dir = 'data/cifar-10-batches-py/'
     data, labels = read_cifar(batch_dir)
@@ -145,11 +155,13 @@ if __name__ == "__main__":
 
     training_accuracies, final_testing_accuracy = run_mlp_training (data_train, labels_train, data_test, d_h, labels_test, learning_rate, num_epoch)
 
-    k = list(range(num_epoch))
-    k = [x+1 for x in k]
+    print(training_accuracies, final_testing_accuracy)
+
+    #k = list(range(num_epoch))
+    #k = [x+1 for x in k]
 
-    plt.plot(k, training_accuracies)
-    plt.show()
+    #plt.plot(k, training_accuracies)
+    #plt.show()
     
 
 
-- 
GitLab