With our Python environment ready and a grasp of the dataset (like MNIST, which we'll assume for our examples), we can now define the structure of our simple autoencoder. We'll use Keras, a high-level API within TensorFlow, which makes building neural networks quite accessible.
An autoencoder, at its heart, is a neural network. We'll construct it using layers. For this basic model, we'll primarily use Dense
layers. A Dense
layer is a fully connected layer, meaning each neuron in it receives input from all neurons in the previous layer.
Our autoencoder will have two main parts:
We'll use the Keras Sequential
model, which is ideal for creating models as a linear stack of layers.
The encoder's job is to reduce the dimensionality of the input. If our input data is, for example, a flattened MNIST image with 28×28=784 pixels, the encoder will map these 784 dimensions to a much smaller number.
input_shape
argument in the first Dense
layer.Dense
layers that progressively reduce the number of neurons. For example, from 784 inputs, we might go to 128 neurons.encoding_dim
, is a critical parameter.The decoder's task is the reverse of the encoder. It takes the compressed representation from the bottleneck and expands it back to the original input's dimensions.
Let's translate this into Keras code. First, ensure you have TensorFlow installed and imported.
from tensorflow import keras
from tensorflow.keras import layers
# Define the dimensionality of the input.
# For MNIST, each image is 28x28 pixels, so flattened it's 784.
input_dim = 784
# Define the size of the encoded representation (the bottleneck).
# This is a hyperparameter you can tune. Let's choose 64.
encoding_dim = 64
# Define the autoencoder model using the Keras Sequential API
autoencoder = keras.Sequential([
# Encoder part
layers.Dense(128, activation='relu', input_shape=(input_dim,)), # Input layer connected to first hidden layer
layers.Dense(encoding_dim, activation='relu'), # Bottleneck layer
# Decoder part
layers.Dense(128, activation='relu'), # First hidden layer of decoder
layers.Dense(input_dim, activation='sigmoid') # Output layer, reconstructing the input
])
# Display the model's architecture
autoencoder.summary()
When you run autoencoder.summary()
, Keras will print a table that looks something like this (the exact number of parameters might vary slightly based on TensorFlow/Keras versions but the structure will be consistent):
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 128) 100480
dense_1 (Dense) (None, 64) 8256
dense_2 (Dense) (None, 128) 8320
dense_3 (Dense) (None, 784) 101136
=================================================================
Total params: 218,192
Trainable params: 218,192
Non-trainable params: 0
_________________________________________________________________
Understanding the Summary:
(None, 128)
means this layer outputs a tensor where the first dimension (None
) is the batch size (which can vary), and the second dimension is the number of neurons (128 in this case).Dense
layer connects 784 inputs to 128 neurons. This involves 784×128 weights plus 128 biases, totaling 100352+128=100480 parameters.A diagram can help visualize the flow of data through our autoencoder:
This diagram shows the data flowing from the input layer, through the encoder (compressing to 64 units at the bottleneck), and then through the decoder to reconstruct the original 784 units at the output.
In this structure:
Dense
layers because they are versatile and a good starting point for many neural network tasks, including basic autoencoders.activation='relu'
) is used in the hidden layers. It's a popular choice that helps models learn effectively.activation='sigmoid'
) is used in the output layer. If your input pixel values are scaled between 0 and 1 (a common preprocessing step for images), sigmoid ensures the output values are also in this range, which is helpful for comparing the reconstruction to the original.With the model's architecture defined, the next step is to configure it for training. This involves choosing a loss function (to measure how well the autoencoder is doing) and an optimizer (to tell the model how to get better). We'll cover that in the next section.
Was this section helpful?
© 2025 ApX Machine Learning