Correction de l’exercice : Ajustement des biais et de Why pour
un RNN
Enoncé du problème
On souhaite ajuster les poids Why et les biais by et bz dans un RNN. Les valeurs données sont :
• Matrice des poids :
0.1 0.2 0.5 0.6
Wxh = , Whh = ,
0.3 0.4 0.7 0.8
Why = 0.9 1.0 .
0.1
• Biais : bz = , by = 0.3.
0.2
0.1974 0.5890
• Etats cachés : h0 ≈ , h1 ≈ .
0.4621 0.7526
• Entrées : x(t1 ) = [1, 0], x(t2 ) = [0, 1].
• Sorties cibles : y (t0 ) = 1, y (t1 ) = 0.
• Prédictions : ŷ0 = 0.7022, ŷ1 = 0.8389.
• Fonctions d’activation :
– Fonction d’activation de sortie : fa (x) = σ(x) = 1
1+e−x , dérivée fa′ (x) = σ(x)(1 − σ(x)).
– Fonction d’activation cachée : fh (x) = tanh(x), dérivée fh′ (x) = 1 − tanh2 (x).
Correction
1. Calcul des erreurs et des gradients
Erreur de sortie : La perte au temps t est donnée par :
1
Lt = (ŷt − yt )2 .
2
Dérivée de la perte par rapport à ŷt :
∂Lt
= ŷt − yt .
∂ ŷt
Pour t0 : ŷ0 = 0.7022, y0 = 1 :
∂L0
= 0.7022 − 1 = −0.2978.
∂ ŷ0
Pour t1 : ŷ1 = 0.8389, y1 = 0 :
∂L1
= 0.8389 − 0 = 0.8389.
∂ ŷ1
1
Dérivée par rapport aux biais by :
∂Lt ∂Lt ′
= · f (ot ),
∂by ∂ ŷt a
avec fa′ (ot ) = σ(ot )(1 − σ(ot )).
Pour t0 : ŷ0 = 0.7022, donc :
fa′ (o0 ) = 0.7022(1 − 0.7022) ≈ 0.2089,
∂L0
= −0.2978 · 0.2089 ≈ −0.0622.
∂by
Pour t1 : ŷ1 = 0.8389, donc :
fa′ (o1 ) = 0.8389(1 − 0.8389) ≈ 0.1352,
∂L1
= 0.8389 · 0.1352 ≈ 0.1134.
∂by
2. Mise à jour des poids Why
Le gradient par rapport à Why est :
∂Lt ∂Lt ′
= · f (ot ) · hTt .
∂Why ∂ ŷt a
0.1974
Pour t0 : h0 ≈ :
0.4621
∂L0
= −0.2978 · 0.2089 · 0.1974 0.4621
∂Why
≈ −0.0123 −0.0287 .
0.5890
Pour t1 : h1 ≈ :
0.7526
∂L1
= 0.8389 · 0.1352 · 0.5890 0.7526
∂Why
≈ 0.0669 0.0854 .
3. Mise à jour des paramètres
La mise à jour s’effectue par :
θ ← θ − η · ∇θ ,
avec η le taux d’apprentissage.
Application numérique : En prenant η = 0.01, on met à jour les paramètres :
• Mise à jour de Why :
(t ) (t )
Why ← Why − η · ∇W0hy + ∇W1hy ,
Why = 0.9 1.0 − 0.01 · −0.0123 −0.0287 + 0.0669 0.0854 ,
Why = 0.9 1.0 − 0.01 · 0.0546 0.0567 ,
Why ≈ 0.8995 0.9994 .
• Mise à jour de by :
(t ) (t )
by ← by − η · ∇by0 + ∇by1 ,
by = 0.3 − 0.01 · (−0.0622 + 0.1134) ,
by = 0.3 − 0.01 · 0.0512,
by ≈ 0.2995.