Part 17 - Implementing Autoencoders in Python
Machine Learning Algorithms Series - Dimensionality Reduction and Feature Extraction with TensorFlow and Keras
This article explains how to implement Autoencoders in Python using TensorFlow and Keras. Autoencoders are neural networks used for unsupervised learning, specifically for dimensionality reduction and feature extraction. They work by encoding input data into a compressed latent representation and then reconstructing the original input from this representation. Autoencoders are useful for tasks like denoising, anomaly detection, and pre-training for other neural networks. The article covers importing necessary libraries, preparing data, defining the autoencoder model, compiling and training the model, obtaining the encoded compressed representation, and printing the results.
Step-by-Step Implementation
Importing Libraries:
Import the
Model
class fromtensorflow.keras.models
.Import
Input
andDense
layers fromtensorflow.keras.layers
.Import
numpy
for numerical operations.
Preparing Data:
Create a NumPy array
X
representing the data points in a high-dimensional space (e.g., 5D). Each sublist represents a data point with five values, which are features.
Defining the Autoencoder Model:
Set the input dimension (
input_dim
) to the number of features in the datasetX
.Specify the encoding dimension (
encoding_dim
), which is the number of dimensions to which the input data will be compressed (e.g., 2 for compressing 5D data into 2D).Define the input layer of the autoencoder using
Input(shape=(input_dim,))
.Define the encoder layer as a dense (fully connected) layer with
encoding_dim
neurons and a ReLU activation function:Dense(encoding_dim, activation='relu')(input_layer)
. The encoder reduces the input dimension from 5 to 2 by learning a compressed representation of the input data.Define the decoder layer as a dense layer with
input_dim
neurons and a sigmoid activation function:Dense(input_dim, activation='sigmoid')(encoded)
. The decoder expands the compressed 2D encoding back to the original 5D space.Combine the input and output layers to create the full autoencoder model:
Model(input_layer, decoded)
.
Compiling the Model:
Compile the autoencoder model using the Adam optimizer and the mean squared error (MSE) loss function:
autoencoder.compile(optimizer='adam', loss='mse')
.
Training the Model:
Train the autoencoder model on the data
X
, usingX
as both the input and target. Specify the number of epochs and the batch size:autoencoder.fit(X, X, epochs=100, batch_size=2, verbose=0)
.
Obtaining the Encoded Compressed Representation:
Define an encoder model using only the encoding part of the original autoencoder:
encoder = Model(input_layer, encoded)
.Pass the input data
X
through the encoder model to obtain the compressed 2D representation:X_compressed = encoder.predict(X)
.
Printing the Results:
Print the compressed 2D representation of the input data.
Complete Code Example
# Import necessary libraries
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
import numpy as np
# Prepare data
X = np.array([[, , , ,], [, , , ,], [, , , ,], [, , , ,], [, , , ,], [, , , ,]])
# Define the autoencoder model
input_dim = X.shape
encoding_dim = 2
input_layer = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu')(input_layer)
decoded = Dense(input_dim, activation='sigmoid')(encoded)
autoencoder = Model(input_layer, decoded)
# Compile the model
autoencoder.compile(optimizer='adam', loss='mse')
# Train the model
autoencoder.fit(X, X, epochs=100, batch_size=2, verbose=0)
# Get the encoded compressed representation
encoder = Model(input_layer, encoded)
X_compressed = encoder.predict(X)
# Print the results
print("Compressed representation:\n", X_compressed)
Conclusion
This article demonstrates how to train an autoencoder to reduce the dimensionality of data (e.g., from 5D to 2D), which can then be used for tasks like visualization or feature extraction. The autoencoder learns to reconstruct the original data while compressing it into a more compact form in the middle layer. The encoder part of the model can be used separately to generate compressed representations of new data.