Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP-Clustering
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IA et Machine Learning
TP-Clustering
Commits
1f6f9abe
Commit
1f6f9abe
authored
1 year ago
by
thibault.capt
Browse files
Options
Downloads
Patches
Plain Diff
update
parent
b994a3f5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
perceptron-tp3.py
+29
-19
29 additions, 19 deletions
perceptron-tp3.py
with
29 additions
and
19 deletions
perceptron-tp3.py
+
29
−
19
View file @
1f6f9abe
# Author : Capt Thibault, Souza Luz Juliano
# Author : Capt Thibault, Souza Luz Juliano
# Date : 31.10.2023
# Date : 31.10.2023
# Project : Perceptron multicouche
# Project : Perceptron multicouche
# Description : Ce fichier représente notre travail pour le tp du perceptron multicouche
# Description : Ce fichier représente notre
# travail pour le tp du perceptron multicouche
#
##############################################
#
# Réponse à la question :
# Comme notre modèle d'apprentissage est assez simple, moins il y a de neurones dans la couche cachée, plus c'est
import
numpy
as
np
import
numpy
as
np
import
pandas
as
pd
import
pandas
as
pd
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
# Fonction pour mettre à jour les poids de la couche cachée
def
upd_weights_hidden
(
wi_old
,
learning
,
delta
,
xi
):
def
upd_weights_hidden
(
wi_old
,
learning
,
delta
,
xi
):
return
wi_old
+
learning
*
np
.
outer
(
xi
,
delta
)
return
wi_old
+
learning
*
np
.
outer
(
xi
,
delta
)
# Fonction pour mettre à jour les poids de la couche de sortie
def
upd_weights_output
(
wi_old
,
learning
,
delta
,
y
):
def
upd_weights_output
(
wi_old
,
learning
,
delta
,
y
):
return
wi_old
+
learning
*
delta
*
y
return
wi_old
+
learning
*
delta
*
y
# Fonction d'activation sigmoïde
def
sigmoide
(
x
):
def
sigmoide
(
x
):
return
1
/
(
1
+
np
.
exp
(
-
x
))
return
1
/
(
1
+
np
.
exp
(
-
x
))
# Fonction de prédiction
def
predict
(
poids_input_hidden
,
poids_hidden_output
,
input_data
):
def
predict
(
poids_input_hidden
,
poids_hidden_output
,
input_data
):
hidden_input
=
np
.
dot
(
input_data
,
poids_input_hidden
)
hidden_input
=
np
.
dot
(
input_data
,
poids_input_hidden
)
hidden_output
=
sigmoide
(
hidden_input
)
hidden_output
=
sigmoide
(
hidden_input
)
...
@@ -26,25 +34,31 @@ def predict(poids_input_hidden, poids_hidden_output, input_data):
...
@@ -26,25 +34,31 @@ def predict(poids_input_hidden, poids_hidden_output, input_data):
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
# Chargement des données depuis le fichier CSV
dataset
=
pd
.
read_csv
(
"
Data/student-data-train.csv
"
,
header
=
0
)
dataset
=
pd
.
read_csv
(
"
Data/student-data-train.csv
"
,
header
=
0
)
dataset
[
'
norm_grade_1
'
]
=
(
dataset
[
'
grade_1
'
]
-
dataset
[
'
grade_1
'
].
mean
())
/
dataset
[
'
grade_1
'
].
std
()
dataset
[
'
norm_grade_1
'
]
=
(
dataset
[
'
grade_1
'
]
-
dataset
[
'
grade_1
'
].
mean
())
/
dataset
[
'
grade_1
'
].
std
()
dataset
[
'
norm_grade_2
'
]
=
(
dataset
[
'
grade_2
'
]
-
dataset
[
'
grade_2
'
].
mean
())
/
dataset
[
'
grade_2
'
].
std
()
dataset
[
'
norm_grade_2
'
]
=
(
dataset
[
'
grade_2
'
]
-
dataset
[
'
grade_2
'
].
mean
())
/
dataset
[
'
grade_2
'
].
std
()
# Normalisation des grades
X
=
dataset
[[
'
norm_grade_1
'
,
'
norm_grade_2
'
]].
values
X
=
dataset
[[
'
norm_grade_1
'
,
'
norm_grade_2
'
]].
values
y
=
dataset
.
iloc
[:,
0
].
values
y
=
dataset
.
iloc
[:,
0
].
values
num_features
=
X
.
shape
[
1
]
num_features
=
X
.
shape
[
1
]
learning_rate
=
1e-3
# Paramètres d'apprentissage
learning_rate
=
0.034
max_iterations
=
2000
max_iterations
=
2000
num_hidden
=
1
0
num_hidden
=
20
0
# Initialiser les poids pour l'entrée dans la couche cachée et masqués dans la couche de sortie
# Initialiser les poids pour l'entrée dans la couche cachée et masqués dans la couche de sortie
weights_input_hidden
=
np
.
random
.
rand
(
num_features
+
1
,
num_hidden
)
-
0.5
weights_input_hidden
=
np
.
random
.
rand
(
num_features
+
1
,
num_hidden
)
-
0.5
weights_hidden_output
=
np
.
random
.
rand
(
num_hidden
+
1
)
-
0.5
weights_hidden_output
=
np
.
random
.
rand
(
num_hidden
+
1
)
-
0.5
errors_x
=
[]
errors_y
=
[]
print
(
"
Weights input to hidden:
"
,
weights_input_hidden
)
print
(
"
Weights input to hidden:
"
,
weights_input_hidden
)
print
(
"
Weights hidden to output:
"
,
weights_hidden_output
)
print
(
"
Weights hidden to output:
"
,
weights_hidden_output
)
# Entraînement du modèle
for
iteration
in
range
(
max_iterations
):
for
iteration
in
range
(
max_iterations
):
total_error
=
0
total_error
=
0
...
@@ -65,6 +79,12 @@ if __name__ == '__main__':
...
@@ -65,6 +79,12 @@ if __name__ == '__main__':
total_error
+=
(
target
-
output
)
**
2
/
2
total_error
+=
(
target
-
output
)
**
2
/
2
# Ajouter les coordonnées des erreurs à la liste
if
target
!=
round
(
output
):
errors_x
.
append
(
X
[
i
,
0
])
errors_y
.
append
(
X
[
i
,
1
])
if
(
iteration
%
10
==
0
)
or
iteration
==
0
:
# print tous les 5 iterations
if
(
iteration
%
10
==
0
)
or
iteration
==
0
:
# print tous les 5 iterations
print
(
f
"
Iteration
{
iteration
+
1
}
: Error =
{
total_error
}
"
)
print
(
f
"
Iteration
{
iteration
+
1
}
: Error =
{
total_error
}
"
)
...
@@ -81,23 +101,13 @@ if __name__ == '__main__':
...
@@ -81,23 +101,13 @@ if __name__ == '__main__':
accuracy
=
correct_classifications
/
len
(
X
)
accuracy
=
correct_classifications
/
len
(
X
)
print
(
f
"
Taux de classifications correctes:
{
accuracy
*
100
}
%
"
)
print
(
f
"
Taux de classifications correctes:
{
accuracy
*
100
}
%
"
)
# Extraction des poids de la couche cachée vers la couche d'entrée
w1
,
w2
,
b
=
weights_input_hidden
[
1
,
1
],
weights_input_hidden
[
2
,
1
],
weights_input_hidden
[
0
,
1
]
# Calcul de la pente et de l'ordonnée à l'origine de la droite de séparation
pente
=
-
w1
/
w2
intercept
=
-
b
/
w2
print
(
f
"
Droite de séparation: y =
{
pente
}
x +
{
intercept
}
"
)
# Tracer la droite de séparation
# Tracer la droite de séparation
plt
.
figure
()
plt
.
figure
()
plt
.
scatter
(
X
[
y
==
0
][:,
0
],
X
[
y
==
0
][:,
1
],
color
=
'
re
d
'
,
label
=
'
Classe 0
'
)
plt
.
scatter
(
X
[
y
==
0
][:,
0
],
X
[
y
==
0
][:,
1
],
color
=
'
g
re
en
'
,
label
=
'
Classe 0
'
)
plt
.
scatter
(
X
[
y
==
1
][:,
0
],
X
[
y
==
1
][:,
1
],
color
=
'
blue
'
,
label
=
'
Classe 1
'
)
plt
.
scatter
(
X
[
y
==
1
][:,
0
],
X
[
y
==
1
][:,
1
],
color
=
'
blue
'
,
label
=
'
Classe 1
'
)
plt
.
plot
([
-
2
,
2
],
[
-
2
*
pente
+
intercept
,
2
*
pente
+
intercept
]
,
color
=
'
g
re
en
'
,
label
=
'
Droite de séparation
'
)
plt
.
scatter
(
errors_x
,
errors_y
,
color
=
'
re
d
'
,
marker
=
'
x
'
,
label
=
'
Erreurs
'
)
plt
.
title
(
'
Données
avec la droite de séparation
'
)
plt
.
title
(
'
Données
'
)
plt
.
xlabel
(
'
Norm_Grade_1
'
)
plt
.
xlabel
(
'
Norm_Grade_1
'
)
plt
.
ylabel
(
'
Norm_Grade_2
'
)
plt
.
ylabel
(
'
Norm_Grade_2
'
)
plt
.
legend
()
plt
.
legend
()
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