Skip to content
Snippets Groups Projects
Commit 7299f2fd authored by Kevin Bonga's avatar Kevin Bonga
Browse files

editing tp3 encoder main file by adding commentary

parent e7410cfa
No related branches found
No related tags found
No related merge requests found
......@@ -4,24 +4,32 @@ from keras.models import Model
from keras.layers import Input, Dense
from keras.optimizers import Adam
# Charger et préparer les données
data = np.loadtxt('datas/semeion.data')
# Séparer les features et les labels
X = data[:, :-10] # Ignorer les 10 dernières colonnes
y = data[:, -10:]
# Diviser les données en ensembles d'entraînement et de test
X_train, X_test = train_test_split(X, test_size=0.2, random_state=42)
y = data[:, -10:] # Les 10 dernières colonnes sont les étiquettes
X_train, X_test = train_test_split(X, test_size=0.2, random_state=42) # Diviser les données en ensembles d'entraînement et de test
# Fonction pour ajouter du bruit aux données
def add_noise(data, noise_factor=0.5):
"""
Add random noise to the data. The noise factor controls the amount of noise added. The noisy data is clipped to ensure it falls within the valid range [0, 1].
:param data: numpy.ndarray: The input data.
:param noise_factor: float: The noise factor.
:return: numpy.ndarray: The noisy data.
"""
noisy_data = data + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=data.shape)
noisy_data = np.clip(noisy_data, 0., 1.)
return noisy_data
# Auto-encodeur avec une couche cachée
def build_single_layer_autoencoder(input_dim, hidden_dim):
"""
Build an autoencoder with a single hidden layer.
:param input_dim:
:param hidden_dim:
:return: keras.models.Model: The autoencoder model.
"""
input_layer = Input(shape=(input_dim,))
hidden_layer = Dense(hidden_dim, activation='relu')(input_layer)
output_layer = Dense(input_dim, activation='sigmoid')(hidden_layer)
......@@ -31,6 +39,12 @@ def build_single_layer_autoencoder(input_dim, hidden_dim):
# Auto-encodeur avec trois couches cachées
def build_three_layer_autoencoder(input_dim, hidden_dims):
"""
Build an autoencoder with three hidden layers.
:param input_dim:
:param hidden_dims:
:return: keras.models.Model: The autoencoder model.
"""
input_layer = Input(shape=(input_dim,))
hidden_layer1 = Dense(hidden_dims[0], activation='relu')(input_layer)
hidden_layer2 = Dense(hidden_dims[1], activation='relu')(hidden_layer1)
......@@ -42,6 +56,14 @@ def build_three_layer_autoencoder(input_dim, hidden_dims):
# Entraîner et évaluer un auto-encodeur
def train_and_evaluate_autoencoder(autoencoder, X_train, X_test, epochs=50, batch_size=256):
"""
Train and evaluate an autoencoder model on the given data.
:param autoencoder: the autoencoder model to train and evaluate
:param X_train: training data
:param X_test: test data
:param epochs: number of epochs to train the model
:param batch_size: batch size for training
"""
autoencoder.fit(X_train, X_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_data=(X_test, X_test))
reconstructed = autoencoder.predict(X_test)
mse = np.mean(np.power(X_test - reconstructed, 2), axis=1)
......@@ -49,6 +71,16 @@ def train_and_evaluate_autoencoder(autoencoder, X_train, X_test, epochs=50, batc
# Entraîner et évaluer un auto-encodeur avec des données bruitées
def train_and_evaluate_noisy_autoencoder(autoencoder, X_train, X_test, noise_factor=0.5, epochs=50, batch_size=256):
"""
Train and evaluate an autoencoder model on noisy data.
:param autoencoder: the autoencoder model to train and evaluate
:param X_train: training data without noise (clean data)
:param X_test: test data without noise (clean data)
:param noise_factor: the factor controlling the amount of noise to add to the data
:param epochs: number of epochs to train the model
:param batch_size: batch size for training
:return:
"""
X_train_noisy = add_noise(X_train, noise_factor)
X_test_noisy = add_noise(X_test, noise_factor)
autoencoder.fit(X_train_noisy, X_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_data=(X_test_noisy, X_test))
......@@ -56,7 +88,7 @@ def train_and_evaluate_noisy_autoencoder(autoencoder, X_train, X_test, noise_fac
mse = np.mean(np.power(X_test - reconstructed, 2), axis=1)
print("Mean Squared Error with noise: ", np.mean(mse))
# Paramètres
# Paramètres pour les auto-encodeurs à une et trois couches cachées
input_dim = X_train.shape[1]
# Entraîner et évaluer l'auto-encodeur avec une couche cachée
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment