-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtf2_unevar.py
More file actions
79 lines (58 loc) · 2.38 KB
/
tf2_unevar.py
File metadata and controls
79 lines (58 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
from tensorflow import keras
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense
import matplotlib.pyplot as plt
# COPIER-COLLER A PARTIR D'ICI
# Partie A. Données
# Fonctions à approcher
def f(x):
return np.cos(2*x) + x*np.sin(3*x) + x**0.5 - 2
a, b = 0, 5 # intervalle [a,b]
N = 100 # taille des données
X = np.linspace(a, b, N) # abscisses
Y = f(X) # ordonnées
X_train = X.reshape(-1,1)
Y_train = Y.reshape(-1,1)
# Partie B. Réseau
modele = Sequential()
p = 10
modele.add(Input(shape=(1,))) # Entrée de dimension 1
modele.add(Dense(p, activation='tanh'))
modele.add(Dense(p, activation='tanh'))
modele.add(Dense(p, activation='tanh'))
modele.add(Dense(p, activation='tanh'))
modele.add(Dense(1, activation='linear'))
# Partie C. Apprentissage
# Méthode de gradient : descente de gradient classique
weights = modele.get_weights() # Sauvegarder les poids
mysgd = optimizers.SGD(learning_rate=0.001)
modele.compile(loss='mean_squared_error', optimizer=mysgd)
history_sgd = modele.fit(X_train, Y_train, epochs=4000, batch_size=N)
# Méthode de gradient : descente de gradient classique améliorée
modele.set_weights(weights)
mysgd = optimizers.SGD(learning_rate=0.001, decay=1e-7, momentum=0.9, nesterov=True)
modele.compile(loss='mean_squared_error', optimizer=mysgd)
history_nesterov = modele.fit(X_train, Y_train, epochs=4000, batch_size=N)
# Méthode de gradient : 'adam'
modele.set_weights(weights)
modele.compile(loss='mean_squared_error', optimizer='adam')
history_adam = modele.fit(X_train, Y_train, epochs=4000, batch_size=N)
# Partie D. Visualisation
# Affichage de la fonction et de son approximation par la dernière méthode
Y_predict = modele.predict(X_train)
plt.plot(X_train, Y_train, color='blue')
plt.plot(X_train, Y_predict, color='red')
plt.show()
# Affichage de l'erreur Nesterov
plt.plot(history_nesterov.history['loss'], color='blue')
# plt.savefig('unevar-fonction-erreur.png')
plt.show()
# Affichage des erreurs
plt.plot(history_sgd.history['loss'], label='gradient classique', color='green')
plt.plot(history_nesterov.history['loss'], label='gradient améliorée', color='blue')
plt.plot(history_adam.history['loss'], label='adam', color='red')
plt.legend()
# plt.savefig('unevar-fonction-leserreurs.png')
plt.show()