Autoencoders are a type of neural network designed to learn efficient representations of data, often used for dimensionality reduction, feature learning, and denoising. Unlike PCA, autoencoders can model non-linear relationships, making them more powerful for complex datasets.
An autoencoder consists of two parts:
Input → [Encoder] → Bottleneck (latent space) → [Decoder] → Output (Reconstruction)Install tensorflow if not already installed
pip install tensorflowimport numpy as np
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.datasets import mnist
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
# 1. Load and normalize MNIST data
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), -1))
x_test = x_test.reshape((len(x_test), -1))
# 2. Define autoencoder architecture
input_dim = x_train.shape[1]
encoding_dim = 32 # Reduced feature size
input_img = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(input_dim, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
# 3. Compile and train
autoencoder.compile(optimizer=Adam(), loss='binary_crossentropy')
autoencoder.fit(x_train, x_train,
epochs=10,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
# 4. Encode and visualize
encoder = Model(input_img, encoded)
encoded_imgs = encoder.predict(x_test)
# 5. Visualize original vs reconstructed images
decoded_imgs = autoencoder.predict(x_test)
n = 5
plt.figure(figsize=(10, 4))
for i in range(n):
# Original
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28), cmap='gray')
plt.axis('off')
# Reconstructed
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray')
plt.axis('off')
plt.suptitle("Original and Reconstructed Images using Autoencoder")
plt.show()Output-
Autoencoders are powerful tools for learning compressed, meaningful representations of data. They outperform linear methods like PCA in capturing complex relationships and are widely used for denoising, anomaly detection, and dimensionality reduction in machine learning workflows.
Sign in to join the discussion and post comments.
Sign in