An autoencoder is a neural network that learns a compact encoding of its input and then tries to reconstruct that input. It is useful when you want features from unlabeled data, such as images, without designing every feature by hand. Common uses include dimensionality reduction, anomaly detection, and denoising. section 9.2 section 9.3 section 9.6
Data passes through three components:
- The encoder reduces the input to a smaller representation. section 9.4
- The bottleneck, or code, limits how much information can pass through. section 9.4
- The decoder uses that code to reconstruct an output with the same dimensions as the input. section 9.4 section 9.5
Figure 9.1 Structure of an autoencoder with an encoder, a bottleneck, and a decoder
The architecture diagram shows where compression occurs and how the decoder turns the code back into a reconstruction. section 9.4
Train the network by comparing each reconstruction with its input and reducing the reconstruction loss. Choose the code size, network depth, nodes per layer, and loss function. A smaller code forces greater compression; additional depth increases complexity and processing time. Binary cross-entropy is suggested for inputs in the [0,1] range, and mean squared error otherwise. Compression is harder when input variables have little relationship to one another. section 9.5
Reconstructing handwritten digits
With Keras, load MNIST digits, scale pixel values by dividing by 255, and flatten each image to 784 values. Map those values through a 32-value encoded layer and a 784-value decoded layer. Fit with each training image serving as both input and target, then encode and decode test images. Plot original and reconstructed digits side by side to inspect what the compact code preserved. section 9.8
Reconstruction error—the difference between the input and its reconstruction—can also help identify anomalies. An undercomplete autoencoder uses a compressed bottleneck for dimensionality reduction. Unlike linear PCA, an autoencoder with nonlinear activations can learn nonlinear relationships; with only linear layers, the undercomplete version is equivalent to PCA. Other named variants include sparse, contractive, denoising, and variational autoencoders. section 9.6 section 9.7
Check the match between training data and intended use before deploying one: reconstruction loses information, and an autoencoder trained on one kind of image may not work on another. For general-purpose data compression, established approaches such as JPEG can be difficult to beat. section 9.4 section 9.6











