diff --git a/02-intro_rna/02-keras-classificateur_img.py b/02-intro_rna/02-keras-classificateur_img.py index 808a67f..9f4dd38 100644 --- a/02-intro_rna/02-keras-classificateur_img.py +++ b/02-intro_rna/02-keras-classificateur_img.py @@ -108,6 +108,7 @@ apts = model.fit(X_train, y_train, epochs=n, batch_size=lot, validation_data=(X_ # Phase d'inférence ############################################################################### +# FIXME : prendre 8 images aléatoirement # X_new=[] # y_new=[] # for i in range(8): diff --git a/02-intro_rna/04-keras-tf_playground-xor.py b/02-intro_rna/04-keras-tf_playground-xor.py index 65189a9..0bca55b 100644 --- a/02-intro_rna/04-keras-tf_playground-xor.py +++ b/02-intro_rna/04-keras-tf_playground-xor.py @@ -74,50 +74,60 @@ donnees_ax = fig.add_subplot(133) # Observations : x1,x2 et cibles : y # Observations ############################################################################### -# Observations d'apprentisage +# Observations m = 1000 # Nombre d'observations bg = 1 # Quantité du bruit gaussien -x1 = 10*np.random.rand(m, 1) # Liste des observations x1 -x2 = 10*np.random.rand(m, 1) # Liste des observations x1 -# y = 4 + 3*x1 + bg * np.random.rand(m, 1) # Liste des cibles y -# X = np.c_[np.ones((m, 1)), x1] # Matrice des observations, avec x0=1 -plt.plot(x1, x2, 'b.', label="Observations") +x1 = np.random.uniform(-5,5, m) # Liste des observations x1 +x2 = np.random.uniform(-5,5, m) # Liste des observations x2 +y = np.empty(m) # Liste des observations cible (XOR) +marge = 0.15 +for i in range (m): + if x1[i]>= 0 and x1[i] < marge: x1[i] += marge + if x1[i]<0 and x1[i] > -marge: x1[i] -= marge + if x2[i]>= 0 and x2[i] < marge: x2[i] += marge + if x2[i]<0 and x2[i] > -marge: x2[i] -= marge + if x1[i]*x2[i]<0: + y[i]=0 + else: + y[i]=1 -# Nouvelles observations -# x1_new=np.array([[0], [2]]) -# X_new = np.c_[np.ones((2, 1)), x1_new] # Matrice des observations, avec x0=1 +# Split en observations d'entrainement et observations de test +test_size=0.1 # Ratio du lot de test +m_train = int(np.round(m*(1-test_size))) +x1_train, x2_train, y_train = x1[:m_train], x2[:m_train], y[:m_train] # Jeu d'entrainement +x1_test, x2_test, y_test = x1[m_train:], x2[m_train:], y[m_train:] # Jeu de test +X_train = np.c_[x1_train, x2_train] +X_test = np.c_[x1_test, x2_test] -# # Observations d'apprentissage, de validation et de test -# vetement = keras.datasets.fashion_mnist # Jeu de données Fashion MNIST -# (X, y), (X_test, y_test) = vetement.load_data() -# X_train, y_train = X[5000:]/255.0 , y[5000:] -# X_valid, y_valid = X[:5000]/255.0 , y[:5000] -# classes = ["Tshirt", "Pantalon", "Pull", "Robe", "Manteau", "Sandale", "Chemise", "Basket", "Sac", "Bottine"] +# Plots +donnees_ax.plot(x1_train[y_train==1], x2_train[y_train==1], ".", color="tab:blue") +donnees_ax.plot(x1_train[y_train==0], x2_train[y_train==0], "." , color="tab:orange") +donnees_ax.plot(x1_test[y_test==1], x2_test[y_test==1], "o", markerfacecolor='tab:blue', color="black") +donnees_ax.plot(x1_test[y_test==0], x2_test[y_test==0], "o", markerfacecolor='tab:orange', color="black") ############################################################################### # Phase d'apprentissage ############################################################################### -n = 30 # Nombre d'itérations (valeur par défaut : 30 , hyperparamètre) +n = 1000 # Nombre d'itérations (valeur par défaut : 30 , hyperparamètre) eta = 0.01 # Taux d'appentissage (valeur par défaut dans Keras : 0.01, hyperparamètre) lot=32 # Taille de lot (valeur par défaut dans Keras: 32 , hyperparamètre) -perte="sparse_categorical_crossentropy" # Type de perte (hyperparamètre) -#perte="mse" # Type de perte (hyperparamètre) + +# perte="sparse_categorical_crossentropy" # Type de perte (hyperparamètre) +# perte="mse" # Type de perte (hyperparamètre) +perte='mean_absolute_error' keras.backend.clear_session() -# model = keras.models.Sequential() # Modèle de reseau de neurones -# model.add(keras.layers.Flatten(input_shape=[28, 28])) # Couche d'entrée : mise à plat des données d'entrée -> 1 node / pixel soit 784 (28x28) -# model.add(keras.layers.Dense(300, activation="relu")) # Couche 1 : 300 nodes -# # model.add(keras.layers.Dense(300, activation="relu")) # Couche 2 : 300 nodes -> passage de 100 à 300 -# # model.add(keras.layers.Dense(300, activation="relu")) # Couche 3 : 300 nodes -> ajout -# model.add(keras.layers.Dense(100, activation="relu")) # Couche 4 : 100 nodes -> ajout -# model.add(keras.layers.Dense(10, activation="softmax")) # Couche de sortie : 1 node par classe soit 10 +model = keras.models.Sequential() # Modèle de reseau de neurones +# model.add(keras.layers.Flatten(input_shape=[1, 1])) # Couche d'entrée +model.add(keras.layers.Dense(4, input_dim=2, activation="relu")) # Couche 1 : 4 nodes +model.add(keras.layers.Dense(4, activation="relu")) # Couche 2 : 4 nodes +model.add(keras.layers.Dense(1, activation="relu")) # Couche de sortie : 1 node par classe -# # model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"]) -# optimiseur=keras.optimizers.SGD(learning_rate= eta) -# model.compile(loss=perte, optimizer=optimiseur, metrics=["accuracy"]) # Compilation du modèle +optimiseur=keras.optimizers.SGD(learning_rate= eta) +model.compile(loss=perte, optimizer=optimiseur, metrics=["accuracy"]) # Compilation du modèle -# apts = model.fit(X_train, y_train, epochs=n, batch_size=lot, validation_data=(X_valid, y_valid)) # Entrainement +apts = model.fit(X_train, y_train, epochs=n, batch_size=lot) # Entrainement ############################################################################### # Phase d'inférence @@ -143,22 +153,22 @@ keras.backend.clear_session() ############################################################################### # Modèle -# model_ax.set_title("Modèle") -# keras.utils.plot_model(model, "model.png", show_shapes=True) -# model_img=plt.imread("model.png") -# model_ax.imshow(model_img) -# model_ax.set_axis_off() -# os.remove("model.png") # Supression du fichier temporaire +model_ax.set_title("Modèle") +keras.utils.plot_model(model, "model.png", show_shapes=True) +model_img=plt.imread("model.png") +model_ax.imshow(model_img) +model_ax.set_axis_off() +os.remove("model.png") # Supression du fichier temporaire # Courbes d'apprentissage -# apts_ax.set_title("Courbes d'apprentissage") -# apts_ax.plot(apts.epoch, apts.history['loss'], 'b-', label="Perte - entrainement") +apts_ax.set_title("Courbes d'apprentissage") +apts_ax.plot(apts.epoch, apts.history['loss'], 'r-', label="Perte - entrainement") # apts_ax.plot(apts.epoch, apts.history['val_loss'], 'r-', label="Perte - validation") -# apts_ax.plot(apts.epoch, apts.history['accuracy'], 'b:', label="Précision - entrainement") +apts_ax.plot(apts.epoch, apts.history['accuracy'], 'b-', label="Précision - entrainement") # apts_ax.plot(apts.epoch, apts.history['val_accuracy'], 'r:', label="Précision - validation") -# apts_ax.set(ylim=(0, 1)) -# apts_ax.set_xlabel("Époque") -# apts_ax.legend() +apts_ax.set(ylim=(0, 1)) +apts_ax.set_xlabel("Époque") +apts_ax.legend() # Plot des données donnees_ax.set_title("Données")