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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IA et Machine Learning
TP-Clustering
Commits
d33ecc31
Commit
d33ecc31
authored
1 year ago
by
thibault.capt
Browse files
Options
Downloads
Patches
Plain Diff
tp3 perceptron couche cachee
parent
dc179203
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
perceptron-tp3.py
+35
-40
35 additions, 40 deletions
perceptron-tp3.py
with
35 additions
and
40 deletions
perceptron-tp3.py
+
35
−
40
View file @
d33ecc31
# Author : Capt Thibault , Souza Luz Juliano
# Date : 31.10.2023
# Project : Perceptron
# Description : Ce fichier représente notre travail pour le tp du perceptron
import
numpy
as
np
import
pandas
as
pd
from
matplotlib
import
pyplot
as
plt
def
upd_weights
(
wi_old
,
learning
,
t
,
y
,
xi
):
"""
Mettre à jour les poids
:param wi_old: Les anciens poids
:param learning: Le taux d
'
apprentissage ( qui contrôle la vitesse de convergence)
:param t: La valeur cible
:param y: La sortie actuelle du modèle pour l
'
exemple donné
:param xi: Les caractéristiques de l
'
exemple d
'
apprentissage
:return: Les poids mis à jour
"""
def
upd_weights_hidden
(
wi_old
,
learning
,
t
,
y
,
xi
):
return
wi_old
+
learning
*
(
t
-
y
)
*
y
*
(
1
-
y
)
*
xi
def
upd_weights_output
(
wi_old
,
learning
,
delta
,
y
):
return
wi_old
+
learning
*
delta
*
y
def
sigmoide
(
x
):
"""
Calcule la fonction sigmoïde (fonction d
'
activation) pour une valeur x
:param x: La valeur d
'
entrée.
:return: La sigmoide en fonction de x.
"""
return
1
/
(
1
+
np
.
exp
(
-
x
))
def
predict
(
poids_input_hidden
,
poids_hidden_output
,
input_data
):
hidden_input
=
np
.
dot
(
poids_input_hidden
,
input_data
)
hidden_output
=
sigmoide
(
hidden_input
)
output
=
sigmoide
(
np
.
dot
(
poids_hidden_output
,
np
.
insert
(
hidden_output
,
0
,
1
)))
return
output
,
hidden_output
if
__name__
==
'
__main__
'
:
dataset
=
pd
.
read_csv
(
"
Data/student-data-train.csv
"
,
header
=
0
)
# Normalisation des colonnes
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
()
# Extraction des données
X
=
dataset
[[
'
norm_grade_1
'
,
'
norm_grade_2
'
]].
values
y
=
dataset
.
iloc
[:,
0
].
values
num_features
=
X
.
shape
[
1
]
# ------------------- Paramètres ------------------
learning_rate
=
1e-2
# Taux d'apprentissage
max_iterations
=
5
# Initialisation aléatoire des poids (+1 pour le biais)
poids
=
[
np
.
random
.
uniform
(
-
0.5
,
0.5
)
for
_
in
range
(
num_features
+
1
)]
print
(
"
Poids initiaux:
"
,
poids
)
# Boucle d'apprentissage
learning_rate
=
1e-2
max_iterations
=
2000
# Initialize weights for input to hidden layer and hidden to output layer
weights_input_hidden
=
np
.
random
.
rand
(
num_features
+
1
,
num_hidden
)
-
0.5
weights_hidden_output
=
np
.
random
.
rand
(
num_hidden
+
1
)
-
0.5
print
(
"
Weights input to hidden:
"
,
weights_input_hidden
)
print
(
"
Weights hidden to output:
"
,
weights_hidden_output
)
for
iteration
in
range
(
max_iterations
):
total_error
=
0
for
i
in
range
(
len
(
X
)):
# Ajout du biais (X0 = 1)
input_data
=
np
.
insert
(
X
[
i
],
0
,
1
)
cible
=
y
[
i
]
# Calcul de la sortie du réseau
sortie
=
sigmoide
(
np
.
dot
(
poids
,
input_data
))
# Mise à jour des poids
poids
=
upd_weights
(
poids
,
learning_rate
,
cible
,
sortie
,
input_data
)
# Calcul de l'erreur quadratique
total_error
+=
(
cible
-
sortie
)
**
2
target
=
y
[
i
]
# Forward pass
hidden_output
,
output
=
predict
(
weights_input_hidden
,
weights_hidden_output
,
input_data
)
# Backpropagation
delta_output
=
(
target
-
output
)
*
output
*
(
1
-
output
)
weights_hidden_output
=
upd_weights_output
(
weights_hidden_output
,
learning_rate
,
delta_output
,
np
.
insert
(
hidden_output
,
0
,
1
))
delta_hidden
=
hidden_output
*
(
1
-
hidden_output
)
*
delta_output
*
weights_hidden_output
[
1
:]
weights_input_hidden
=
upd_weights_hidden
(
weights_input_hidden
,
learning_rate
,
delta_hidden
,
input_data
)
total_error
+=
(
target
-
output
)
**
2
/
2
# Affichage de l'erreur à chaque itération (à enlever pour de meilleures performances)
print
(
f
"
Iteration
{
iteration
+
1
}
: Erreur =
{
total_error
}
"
)
print
(
f
"
Iteration
{
iteration
+
1
}
: Error =
{
total_error
}
"
)
# Calcul du taux de classification correcte
correct_classifications
=
0
...
...
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