Foundations to Frontier
Tests
E. Deep Learning › E13 Autoencoders

Stacked autoencoder

A stacked autoencoder is a deep autoencoder built one layer at a time. Train a shallow autoencoder on the data, then another on its codes, and so on. Stack the encoders, stack the decoders in reverse, and fine-tune the whole network. This greedy layer-wise pre-training (Bengio and colleagues, 2006, alongside Hinton's RBM-based method) was how deep networks were first trained successfully, before better initialisation, ReLUs and adaptive optimisers made it unnecessary. This page covers greedy stacking, fine-tuning and why pre-training helped. In the exercises you build the stacking order, then train a 7-layer sigmoid autoencoder on a curved surface. With plain SGD, end-to-end training stalled. At the same step budget, pre-training reached error 0.14 against 0.34 (better in 2 of 3 seeds). With Adam, end-to-end training matched pre-training (0.0005 against 0.0006).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Build deep networks one floor at a time

Training a deep network all at once used to fail: the signal for learning faded on its way through many layers. Stacked autoencoders got around this by building the network floor by floor. First train a small autoencoder on the data. Then train another one on the codes of the first, and so on. Finally put all the pieces together and fine-tune. Today better optimisers usually make this unnecessary, but the idea of pre-training lives on. The demo shows a single layer’s bottleneck; a stack repeats it.

Autoencoder lab
1-D code. Linear autoencoder (= PCA, red line): error 0.296. Nonlinear code (the angle, green): error 0.003. A linear bottleneck can only flatten curved data onto a subspace.
Why engineers care

Greedy pre-training started the deep-learning revival of 2006. Its descendants, unsupervised pre-training followed by fine-tuning, are how today’s foundation models are built.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A stacked autoencoder is trained…
  2. 2. The second autoencoder is trained on…
  3. 3. Today greedy pre-training is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Stacking:
    1. For encoder widths [20, 16, 8, 2], train autoencoders 20 → 16 → 20, then 16 → 8 → 16 on the 16-unit codes, then 8 → 2 → 8.
    2. Stack the encoders 20 → 16 → 8 → 2 and the decoders in reverse, 2 → 8 → 16 → 20.
    3. The full network has sizes [20, 16, 8, 2, 8, 16, 20] and 998 parameters.
  • Budget: 3 pre-training stages plus fine-tuning, each 600 steps, is 2400 steps; compare with end-to-end training for the same 2400.
  • Why deep sigmoid nets were hard: the sigmoid’s derivative is at most 0.25, so through 6 layers gradients can shrink by 0.25⁶ = 1/4096 from the derivatives alone.
  • Hinton and Salakhutdinov’s MNIST autoencoder (2006): 784–1000–500–250–30, pre-trained with RBMs.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Pre-training stages for widths [20, 16, 8, 2]:
  2. 2. Maximum sigmoid derivative:
  3. 3. Decoders are stacked…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Stacked autoencoders in code

Exercise 1.

  • The mirrored sizes and stacking order were built: encoders in order, decoders reversed.
  • Codes were passed to the next level through a sigmoid.

Practice.

  • Modern deep autoencoders train end to end with good initialisation (He or Xavier), normalisation, ReLU or GELU and Adam. No greedy pre-training is needed.
  • Layer-wise pre-training can still help with very little data or unusual architectures.
  • Its spirit survives as self-supervised pre-training followed by fine-tuning.
codes, levels = x, []
for a, b in zip(widths[:-1], widths[1:]):             # greedy: one shallow autoencoder per level
    ae = train_shallow_ae(codes, a, b); levels.append(ae)
    codes = ae.encode(codes).detach()
deep = nn.Sequential(*[ae.enc for ae in levels], *[ae.dec for ae in reversed(levels)])
fine_tune(deep, x)
🐍 Exercise 1: stacking
Build the mirrored layer sizes, the stacking order and the codes passed between levels.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Modern deep autoencoders are trained…
  2. 2. Greedy pre-training may still help with…
  3. 3. Its modern descendant:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why pre-training helped.

  • Deep sigmoid networks trained with plain gradient descent from small random weights start in a poor region: gradients are tiny in the early layers, and the network often settles on predicting the mean.
  • Each shallow autoencoder is easy to train and leaves its layer computing a useful representation.
  • The stacked network therefore starts near a good solution, and fine-tuning only refines it.

Regulariser or optimiser? Erhan and colleagues (2010) found pre-training acts both as a better initialisation and as a regulariser: it steers networks toward solutions that generalise better, especially with little labelled data.

Why it faded. ReLUs avoid saturating derivatives. Careful initialisation (Glorot 2010, He 2015) keeps signals at the right scale, normalisation layers stabilise training, and adaptive optimisers such as Adam rescale small gradients. Together they let deep networks train well end to end.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Deep sigmoid nets with plain SGD often settle on…
  2. 2. Erhan et al. found pre-training also acts as…
  3. 3. Which helped make pre-training unnecessary?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Is greedy pre-training still needed?

History.

  • G. E. Hinton, S. Osindero and Y.-W. Teh (2006) introduced greedy layer-wise pre-training with RBMs (deep belief networks).
  • Y. Bengio, P. Lamblin, D. Popovici and H. Larochelle, “Greedy layer-wise training of deep networks” (NeurIPS 2006), used autoencoders.
  • D. Erhan et al., “Why does unsupervised pre-training help deep learning?” (JMLR 2010).

Exercise 2.

  • Data: 200 points on a curved 2-D surface in 20 dimensions, scaled to (0, 1).
  • Model: a 7-layer sigmoid autoencoder [20, 16, 8, 2, 8, 16, 20].
  • Training, with plain SGD (learning rate 1) and with Adam:
    • end to end for 600 steps;
    • end to end for 2400 steps;
    • greedy pre-training (3 × 600 steps), then fine-tuning for 600 steps, 2400 in total.
  • Baseline: the data variance, 1.69, is the error of predicting the mean.
optimiser end to end, 600 end to end, 2400 pre-trained (stacked) pre-trained + fine-tuned
SGD 1.694 0.337 0.099 0.136
Adam 0.115 0.0005 0.085 0.0006
  • With plain SGD, end-to-end training is stuck at first: 600 steps leave the error at the variance (predicting the mean), and even 2400 steps reach only 0.337.
  • Pre-training gets SGD out of this: the stacked network starts at 0.099 and ends at 0.136 after fine-tuning (SGD at learning rate 1 slightly undid it). That beats end-to-end at the same budget.
  • Not on every seed: seeds 1 and 2 gave 0.58 against 0.10 and 0.071 against 0.087, so pre-training won on 2 of 3 seeds.
  • With Adam, pre-training brings no benefit: end-to-end training reaches 0.0005, matching pre-training plus fine-tuning (0.0006); seeds 1 and 2 gave 0.009/0.0008 and 0.0005/0.0007. Modern optimisation removed the need, as the history suggests.
🐍 Exercise 2 (research): is greedy pre-training still needed?
Compare end-to-end training with greedy layer-wise pre-training of a deep sigmoid autoencoder, using SGD and Adam.

Stacked autoencoders in AI, robotics and aerospace

  • History that shaped practice: layer-wise pre-training made deep networks usable on small labelled datasets, such as early speech and vision systems.
  • Today: pre-train on abundant unlabelled sensor data, then fine-tune on scarce labelled data. This is the same idea at a larger scale.
  • Design lesson: before adding a training trick, check whether a better optimiser, initialisation or activation already solves the problem.

References

  1. Y. Bengio, P. Lamblin, D. Popovici and H. Larochelle (2006). “Greedy layer-wise training of deep networks.” NeurIPS.
  2. G. E. Hinton and R. R. Salakhutdinov (2006). “Reducing the dimensionality of data with neural networks.” Science 313.
  3. D. Erhan et al. (2010). “Why does unsupervised pre-training help deep learning?” JMLR 11.