An autoencoder consists of two parts (left side of the figure at the top of the article).
- Encoder f: Converts the input x into a code (latent representation) h=f(x).
- Decoder g: Produces a reconstruction r=g(h) with the same shape as the input from the code.
The training procedure is as follows.
- Take an input x from the training data.
- Convert it into the code h with the encoder and into the reconstruction r with the decoder.
- Compute the difference between the input and the reconstruction (the reconstruction error) L(x,r).
- Use backpropagation to compute gradients through the decoder and then the encoder, and update the weights of both together.
The quantity minimized during training is:
f,gmint∑L(x(t),g(f(x(t))))
The encoder and decoder are ordinary neural networks. The most basic form is a single fully connected layer each:
h=sf(Wx+b),r=sg(W′h+d)
sf,sg are activation functions, W,W′ are weight matrices, and b,d are biases. The encoder and decoder sometimes share weights, as in W′=W⊤ (tied weights). For images, convolutions are often used in the encoder and transposed convolutions in the decoder.
Choosing the reconstruction error
The reconstruction error is chosen to match the type of input values.
| Input values | Decoder output | Reconstruction error |
|---|
| Unbounded real numbers | Linear (no activation) | Mean squared error ∥x−r∥2 |
| Values in 0–1 (pixel intensities, etc.) | Sigmoid function | Binary cross-entropy −∑i[xilogri+(1−xi)log(1−ri)] |
Preventing the network from copying its input
If the code dimension is equal to or larger than the input and the encoder and decoder are expressive enough, the network can drive the reconstruction error to 0 by simply copying the input. It then learns nothing about the features of the data. To prevent this, constraints such as the following are imposed (right side of the figure at the top of the article).
- Undercomplete (bottleneck): Make the code dimension smaller than the input. This is the most basic approach.
- Sparse: Add to the reconstruction error a penalty Ω(h) that pushes many components of the code toward 0. The loss is L(x,g(f(x)))+Ω(h).
- Denoising: Feed the encoder a corrupted input x~, for example with some entries set to 0, and have it reconstruct the original x. The loss is L(x,g(f(x~))) (Vincent et al. 2008).
- Contractive: Penalize the magnitude of the encoder's derivative with respect to the input, λ∥∂f(x)/∂x∥F2, so that the code changes little when the input changes slightly (Rifai et al. 2011).
The sparse, denoising, and contractive approaches can be used even when the code dimension is larger than the input.
An autoencoder reconstructs well only inputs similar to its training data. For inputs of a different kind, the reconstruction error becomes large. This property can also be exploited by treating inputs with large reconstruction error as "unusual data."
Training deep autoencoders (the 2006 method)
Stacked autoencoders were known to be hard to train directly. According to Hinton and Salakhutdinov, large initial weights lead to poor local minima, while small ones make the gradients in layers near the input very small (vanishing gradients). The paper solved this with the following procedure.
- Pretraining: Train two-layer models called restricted Boltzmann machines (RBMs) one layer at a time, starting from the bottom. The features of the first RBM are used as the input to the next RBM.
- Unrolling: Stack the trained RBMs to form the encoder, and arrange the same weights in reverse order to form the decoder.
- Fine-tuning: Treat the whole as a single autoencoder and minimize the reconstruction error with backpropagation.
For MNIST, a 784-1000-500-250-30 encoder and a symmetric decoder were used. Only the 30 code units are linear; the rest are logistic (sigmoid) units.
According to the paper, shallow autoencoders with a single hidden layer in each of the encoder and decoder can be trained without pretraining. However, pretraining greatly reduced the training time. Also, with the same number of parameters, deep autoencoders achieved lower reconstruction error on test data than shallow ones, but this gap disappeared as the number of parameters increased.