Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Image classification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dubray Chloe
Image classification
Commits
017fb23c
Commit
017fb23c
authored
Nov 7, 2023
by
Dubray Chloe
Browse files
Options
Downloads
Patches
Plain Diff
Update mlp.py
parent
2068610f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mlp.py
+21
-9
21 additions, 9 deletions
mlp.py
with
21 additions
and
9 deletions
mlp.py
+
21
−
9
View file @
017fb23c
...
@@ -2,13 +2,13 @@ import numpy as np
...
@@ -2,13 +2,13 @@ import numpy as np
from
read_cifar
import
*
from
read_cifar
import
*
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
d_h
=
64
#Définition de la fonction sigmoïde
epsilon
=
0.00001
def
sigma
(
z
)
:
def
sigma
(
z
)
:
return
1
/
(
1
+
np
.
exp
(
-
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
)
:
def
learn_one_mse
(
w1
,
b1
,
w2
,
b2
,
data
,
targets
,
learning_rate
)
:
#Forward pass
a0
=
data
a0
=
data
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
a1
=
sigma
(
z1
)
a1
=
sigma
(
z1
)
...
@@ -18,6 +18,7 @@ def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
...
@@ -18,6 +18,7 @@ def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
d_out
=
np
.
shape
(
targets
)[
1
]
d_out
=
np
.
shape
(
targets
)[
1
]
#Backpropagation
dC_da2
=
2
*
(
a2
-
targets
)
/
d_out
dC_da2
=
2
*
(
a2
-
targets
)
/
d_out
dC_dz2
=
dC_da2
*
a2
*
(
1
-
a2
)
dC_dz2
=
dC_da2
*
a2
*
(
1
-
a2
)
dC_dw2
=
np
.
matmul
(
a1
.
T
,
dC_dz2
)
dC_dw2
=
np
.
matmul
(
a1
.
T
,
dC_dz2
)
...
@@ -37,6 +38,7 @@ def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
...
@@ -37,6 +38,7 @@ def learn_one_mse (w1, b1, w2, b2, data, targets, learning_rate) :
return
(
w1
,
b1
,
w2
,
b2
,
loss
)
return
(
w1
,
b1
,
w2
,
b2
,
loss
)
#Définition de la fonction d'encodage one-hot
def
one_hot
(
labels
,
num_classes
=
None
):
def
one_hot
(
labels
,
num_classes
=
None
):
if
num_classes
is
None
:
if
num_classes
is
None
:
num_classes
=
np
.
max
(
labels
)
+
1
num_classes
=
np
.
max
(
labels
)
+
1
...
@@ -45,6 +47,7 @@ def one_hot(labels, num_classes=None):
...
@@ -45,6 +47,7 @@ def one_hot(labels, num_classes=None):
one_hot_matrix
[
i
,
labels
[
i
]]
=
1
one_hot_matrix
[
i
,
labels
[
i
]]
=
1
return
one_hot_matrix
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
)
:
def
learn_one_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data
,
labels_train
,
learning_rate
)
:
a0
=
data
a0
=
data
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
...
@@ -72,20 +75,25 @@ def learn_one_cross_entropy (w1, b1, w2, b2, data, labels_train, learning_rate)
...
@@ -72,20 +75,25 @@ def learn_one_cross_entropy (w1, b1, w2, b2, data, labels_train, learning_rate)
w2
-=
learning_rate
*
dC_dw2
w2
-=
learning_rate
*
dC_dw2
b2
-=
learning_rate
*
dC_db2
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
loss
=
-
np
.
sum
(
y
*
np
.
log2
(
predictions
+
epsilon
)
+
(
1
-
y
)
*
np
.
log2
(
1
-
predictions
+
epsilon
))
/
N
return
(
w1
,
b1
,
w2
,
b2
,
loss
)
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
):
def
predict_class
(
predictions
):
return
np
.
argmax
(
predictions
,
axis
=
1
)
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
):
def
accuracy
(
y_true
,
y_pred
):
return
np
.
mean
(
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
)
:
def
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
)
:
training_accuracies
=
[]
training_accuracies
=
[]
for
k
in
range
(
num_epoch
)
:
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
)
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
...
@@ -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
)
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
)
:
def
test_mlp
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
)
:
a0
=
data_test
a0
=
data_test
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
...
@@ -116,7 +125,7 @@ def test_mlp (w1, b1, w2, b2, data_test, labels_test) :
...
@@ -116,7 +125,7 @@ def test_mlp (w1, b1, w2, b2, data_test, labels_test) :
return
(
test_accuracy
)
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
)
:
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_in
=
(
np
.
shape
(
data_train
))[
1
]
d_out
=
np
.
max
(
labels_train
)
+
1
d_out
=
np
.
max
(
labels_train
)
+
1
...
@@ -138,6 +147,7 @@ if __name__ == "__main__":
...
@@ -138,6 +147,7 @@ if __name__ == "__main__":
learning_rate
=
0.1
learning_rate
=
0.1
num_epoch
=
100
num_epoch
=
100
split_factor
=
0.9
split_factor
=
0.9
d_h
=
64
batch_dir
=
'
data/cifar-10-batches-py/
'
batch_dir
=
'
data/cifar-10-batches-py/
'
data
,
labels
=
read_cifar
(
batch_dir
)
data
,
labels
=
read_cifar
(
batch_dir
)
...
@@ -145,11 +155,13 @@ if __name__ == "__main__":
...
@@ -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
)
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
))
print
(
training_accuracies
,
final_testing_accuracy
)
k
=
[
x
+
1
for
x
in
k
]
#k = list(range(num_epoch))
#k = [x+1 for x in k]
plt
.
plot
(
k
,
training_accuracies
)
#
plt.plot(k, training_accuracies)
plt
.
show
()
#
plt.show()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment