Generating an Autoencoder with Python: Exploring Encoder and Decoder Architectures

Rate this post

πŸ’‘ Problem Formulation: Autoencoders are a type of artificial neural network used to learn efficient representations of unlabeled data, typically for the purpose of dimensionality reduction or feature learning. The challenge is to create an autoencoder in Python using separate encoder and decoder components that can compress and reconstruct data with minimal loss. For instance, given an input image, an autoencoder should produce an encoded (compressed) representation and subsequently reconstruct an image as close as possible to the original.

Method 1: Building an Autoencoder with Keras Sequential API

Using the Keras Sequential API, you can easily stack layers to build both the encoder and decoder parts of an autoencoder. This method allows you to rapidly prototype and test different architectures by simply adding or removing layers.

Here’s an example:

from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model, Sequential

# Encoder
encoder = Sequential([
    Dense(128, activation='relu', input_shape=(784,)),
    Dense(64, activation='relu'),
    Dense(32, activation='relu')

# Decoder
decoder = Sequential([
    Dense(64, activation='relu', input_shape=(32,)),
    Dense(128, activation='relu'),
    Dense(784, activation='sigmoid')

# Autoencoder
autoencoder = Sequential([encoder, decoder])
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

The code creates an autoencoder for image data that has been flattened into vectors of length 784 (e.g., MNIST dataset images). The encoder compresses the data to a vector of length 32, and the decoder attempts to reverse this process. Using ‘sigmoid’ activation in the final layer ensures the output values are between 0 and 1, matching the normalized input data.

Method 2: Utilizing Keras Functional API for a Custom Autoencoder

The Keras Functional API provides more flexibility in designing complex models, allowing multiple inputs and outputs, and constructing models that have shared layers or layers with non-sequential connectivity graphs.

Here’s an example:

from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model

# This returns a tensor
inputs = Input(shape=(784,))

# A layer instance is callable on a tensor, and returns a tensor
encoded = Dense(64, activation='relu')(inputs)
encoded = Dense(32, activation='relu')(encoded)

decoded = Dense(64, activation='relu')(encoded)
decoded = Dense(784, activation='sigmoid')(decoded)

# This creates a model that includes the Input layer and three Dense layers
autoencoder_model = Model(inputs=inputs, outputs=decoded)
autoencoder_model.compile(optimizer='adam', loss='binary_crossentropy')

This code snippet illustrates how to build a custom autoencoder using the Keras Functional API. It starts by defining the input and then constructing the encoder and decoder as a series of functional layers. The model is then compiled, specifying the optimizer and loss function to be used during training.

Method 3: Using PyTorch to Define an Autoencoder

PyTorch provides a dynamic computation graph that is especially useful for building autoencoders with custom behavior, and it encourages a more detailed approach to defining the architecture.

Here’s an example:

import torch
from torch import nn

class Autoencoder(nn.Module):
    def __init__(self):
        super(Autoencoder, self).__init__()
        # Encoder
        self.encoder = nn.Sequential(
            nn.Linear(784, 128),
            nn.Linear(128, 64),
            nn.Linear(64, 32),
        # Decoder
        self.decoder = nn.Sequential(
            nn.Linear(32, 64),
            nn.Linear(64, 128),
            nn.Linear(128, 784),

    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x

autoencoder = Autoencoder()

This is a simple implementation of an autoencoder using PyTorch. The class `Autoencoder` is defined as a subclass of `nn.Module`, and it includes two neural network modules: one for encoding and one for decoding. The `forward` method defines how the input data flows through both networks. PyTorch takes care of the gradients’ computations automatically during training.

Method 4: Utilizing TensorFlow’s GradientTape for a Custom Training Loop

TensorFlow’s GradientTape API provides an explicit way to record operations for automatic differentiation. A custom training loop allows for more control over the training process and can be beneficial when building complex models like autoencoders.

Here’s an example:

import tensorflow as tf

# Encoder and Decoder defined similarly to previous examples
# ...

# Custom training step
def train_step(model, input, loss_fn, optimizer):
    with tf.GradientTape() as tape:
        reconstruction = model(input)
        loss = loss_fn(input, reconstruction)
    gradients = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))
    return loss

# Use the above function in a training loop
# ...

This snippet shows a custom training loop for an autoencoder using TensorFlow’s GradientTape. First, it involves passing data through the model (forward pass), computing the loss (how different the reconstruction is from the input), and then using automatic differentiation to compute gradients. These gradients are then used to update the model’s weights during the backward pass.

Bonus One-Liner Method 5: Using Keras Layers inside Sci-kit Learn Pipeline

For simplicity and in cases where you might want to use autoencoders in conjunction with other pre-processing methods, the Keras layers can be included within a scikit-learn Pipeline.

Here’s an example:

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler

def autoencoder_model():
    # Model defined similarly to previous examples
    # ...
    return model

pipeline = Pipeline([
    ('scaler', MinMaxScaler()),
    ('autoencoder', KerasClassifier(build_fn=autoencoder_model))

The code creates a scikit-learn Pipeline that first scales the data and then runs it through the autoencoder. The `KerasClassifier` is a wrapper to be able to use Keras models in a scikit-learn context, such as pipelines or grid search for hyperparameters tuning.


  • Method 1: Keras Sequential API. Strengths include ease of use and rapid prototyping. Weaknesses include less flexibility for model architectures that are not sequential.
  • Method 2: Keras Functional API. Offers more customization for complex models with configurations like shared layers or non-sequential data flows. Somewhat more complicated than Sequential API.
  • Method 3: PyTorch nn.Module. Highly customizable and encourages a detailed definition of model architectures. It requires more code and a deeper understanding of model construction.
  • Method 4: TensorFlow’s GradientTape. Provides explicit control over the training process and can be useful for research and debugging. Its detailed nature may be a steeper learning curve for beginners.
  • Method 5: Scikit-learn Pipeline with Keras. Simplifies the processes by allowing autoencoders and other pre-processing steps to be included in a single pipeline. Limited by scikit-learn’s interface and model compatibility.