ajout rapport final et améliorations/corrections au code

This commit is contained in:
François Pelletier 2019-05-02 22:25:24 -04:00
parent f726946326
commit 65b9a00d02
4 changed files with 421 additions and 9 deletions

View file

@ -71,7 +71,7 @@ class NeuralNet: #nom de la class à changer
all_input_values.append(current_input_values)
return all_input_values
def train(self, train, train_labels, alpha, nb_epoch): #vous pouvez rajouter d'autres attribus au besoin
def train(self, train, train_labels, alpha, nb_epoch, verbose=True): #vous pouvez rajouter d'autres attribus au besoin
"""
c'est la méthode qui va entrainer votre modèle,
train est une matrice de taille nxm, avec
@ -90,12 +90,14 @@ class NeuralNet: #nom de la class à changer
"""
train_labels_enc = self.one_hot_encoder(train_labels)
learning_curve = []
for epoch in range(nb_epoch):
print("epoch"+str(epoch))
# print("epoch"+str(epoch))
for i in range(len(train)):
deltas = []
output = self.feed_forward(train[i])
print("Les valeurs des sorties sont: "+str(output)+"\nLabel:"+str(train_labels_enc[i]))
# print("Les valeurs des sorties sont: "+str(output)+"\nLabel:"+str(train_labels_enc[i]))
deltas.append(train_labels_enc[i]-output[-1])
# On recule dans les couches cachées j
for j in reversed(range(self.n_hidden_layers)):
@ -114,12 +116,13 @@ class NeuralNet: #nom de la class à changer
# Vu qu'on recule, on insère au début au lieu de la fin
deltas.insert(0,layerError)
# Mise à jour des poids
print("Mise à jour des poids. Les deltas sont: "+str(deltas))
# print("Mise à jour des poids. Les deltas sont: "+str(deltas))
for j in range(len(self.layers)):
# Pour chaque neurone k
for k in range(len(self.layers[j])):
self.layers[j][k].update_weights(output[j],alpha,deltas[j][k])
return self.test(train, train_labels)
learning_curve.append(self.test(train, train_labels, verbose)[1])
return learning_curve
def predict(self, exemple, label):
"""
@ -131,6 +134,9 @@ class NeuralNet: #nom de la class à changer
"""
return np.argmax(self.feed_forward(exemple)[-1])
def score(self, test, test_labels):
return [self.predict(exemple,label) for exemple,label in zip(test,test_labels)]
def test(self, test, test_labels, verbose=True):
"""
@ -153,8 +159,7 @@ class NeuralNet: #nom de la class à changer
"""
start_time = time.time()
prediction_test = [self.predict(exemple,label) for exemple,label in zip(test,test_labels)]
print(prediction_test)
prediction_test = self.score(test,test_labels)
cm = metrics.confusion_matrix(test_labels,prediction_test)
accuracy, precision, recall = metrics.prediction_metrics(cm,test_labels,prediction_test)
compute_time = time.time() - start_time

407
Code/Rapport-Final.ipynb Normal file

File diff suppressed because one or more lines are too long

View file

@ -1528,7 +1528,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.7.3"
}
},
"nbformat": 4,

View file

@ -44,7 +44,7 @@ dt5.train(train5, train_labels5)
dt5.predict(test5[0],test_labels5[0])
dt5.test(test5, test_labels5)
nn1 = NeuralNet.NeuralNet(np.array([4,8,3]),range(3))
nn1 = NeuralNet.NeuralNet(np.array([4,4,3]),range(3))
nn1.train(train1, train_labels1, 0.1, 10)
nn1.predict(test1[0],test_labels1[0])
nn1.test(test1,test_labels1)