Ex No 7
Ex No 7
No:7 Applying the Autoencoder algorithms for encoding the real- world data
Aim:
To apply the Autoencoder algorithms for encoding the real-world data on MNIST dataset
Algorithm:
Program:
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
# Load the MNIST dataset
(X_train, _), (X_test, _) = tf.keras.datasets.mnist.load_data()
# Normalize the images to [0, 1] and flatten them
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0
X_train = X_train.reshape((X_train.shape[0], -1)) # Flatten to 784
X_test = X_test.reshape((X_test.shape[0], -1)) # Flatten to 784
# Define the autoencoder model
input_dim = X_train.shape[1] # 784 for MNIST
encoding_dim = 32 # Dimension of the encoded representation
# Input layer
input_data = layers.Input(shape=(input_dim,))
# Encoder
encoded = layers.Dense(encoding_dim, activation='relu')(input_data)
# Decoder
decoded = layers.Dense(input_dim, activation='sigmoid')(encoded)
# Autoencoder model
autoencoder = models.Model(input_data, decoded)
# Compile the model
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
Output:
Result:
Thus the program to apply the Autoencoder algorithms for encoding the real-world data on MNIST
dataset