Foundations to Frontier
Tests
E. Deep Learning › E13 Autoencoders

Convolutional autoencoder

A convolutional autoencoder replaces fully connected layers with convolutions. Strided convolutions shrink the feature maps on the way down, and transposed convolutions (or upsampling) grow them back. Weight sharing gives far fewer parameters and translation equivariance: a pattern is handled the same way wherever it appears. This makes convolutional autoencoders the standard choice for images, spectrograms and other grid data, and the backbone of image denoisers and latent-diffusion autoencoders. This page covers encoder–decoder shapes, the output-size formulas and checkerboard artefacts. In the exercises you compute convolution and transposed-convolution sizes, then train dense and convolutional autoencoders on 1-D signals with bumps only in the left half. Tested on bumps in the unseen right half, the 1072-parameter dense autoencoder failed (relative error 4.5). The 12-parameter convolutional one reconstructed them (0.09).

Level 0: Foundation

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

Shrink the picture, then grow it back

A convolutional autoencoder works like a zoom-out and zoom-in. The encoder slides small filters over the image and halves its size several times, ending with a small stack of feature maps: the code. The decoder reverses this, doubling the size at each step until it gets back to a full image. Because the same filters are used everywhere, a pattern learned in one corner works in every corner. The demo shows the sizes along the way.

Autoencoder lab
encoder 64 → 32 → 16 → 8  |  decoder 8 → 16 → 32 → 64
Each stride-2 convolution halves the feature map; transposed convolutions double it back to 64 × 64. The code is 8 × 8 per channel.
Why engineers care

Convolutional autoencoders compress and clean images, spectrograms and sensor grids, detect visual defects, and form the image compressors inside latent diffusion models.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The encoder of a convolutional autoencoder…
  2. 2. The decoder uses…
  3. 3. Weight sharing means a pattern is handled…

Level 1: Beginner

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

By hand

  • Convolution output size: ⌊(n + 2p − k)/s⌋ + 1.
    • kernel 4, stride 2, padding 1 halves the size: 28 → 14;
    • kernel 5, stride 2, padding 2 turns 32 into 16.
  • Transposed convolution output size: (n − 1)s − 2p + k + output padding.
    • 16 with kernel 4, stride 2, padding 1 gives 32;
    • 7 with kernel 3, stride 2, padding 1 and output padding 1 gives 14.
  • Parameters: c_in · c_out · k² + c_out per 2-D convolution (3 → 16 channels with 3 × 3 kernels: 448).
  • Code size: 28 × 28 inputs after two halvings with 32 channels give 7 × 7 × 32 = 1568 numbers. The code shrinks spatially but grows in channels.
  • Exercise 2’s models: dense [32, 16, 32] has 1072 parameters; the convolutional one has a 5-tap kernel and a bias each way, 12 parameters.
📝 Quick check (Level 1)
0/3 answered
  1. 1. conv_out(28, k=4, s=2, p=1):
  2. 2. tconv_out(16, k=4, s=2, p=1):
  3. 3. Parameters of a 3 → 16, 3 × 3 convolution:

Level 2: Intermediate

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

Convolutional autoencoders in code

Exercise 1.

  • The convolution and transposed-convolution size formulas matched the examples.
  • A 1-D cross-correlation with kernel (1, 0, −1) gave constant −2 on a ramp.

Practice.

  • Use nn.Conv2d(..., stride=2) down and nn.ConvTranspose2d(..., stride=2) up, with batch norm or group norm and ReLU or GELU.
  • To avoid checkerboard artefacts, prefer nn.Upsample followed by a convolution, or choose kernel sizes divisible by the stride.
  • For high-quality reconstructions, add perceptual or adversarial losses, as in the VQGAN and latent-diffusion autoencoders.
enc = nn.Sequential(nn.Conv2d(1, 16, 4, 2, 1), nn.ReLU(), nn.Conv2d(16, 32, 4, 2, 1))     # 28 → 14 → 7
dec = nn.Sequential(nn.ConvTranspose2d(32, 16, 4, 2, 1), nn.ReLU(), nn.ConvTranspose2d(16, 1, 4, 2, 1))
loss = F.mse_loss(dec(enc(x)), x)
🐍 Exercise 1: shapes and convolution
Implement the output-size formulas and a 1-D convolution.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Checkerboard artefacts come from…
  2. 2. A fix for checkerboards:
  3. 3. Latent diffusion's autoencoder adds…

Level 3: Advanced

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

Theory

Equivariance.

  • Convolution commutes with translation: shifting the input shifts the feature map (exactly for stride 1, up to the stride for strided layers).
  • An autoencoder built from convolutions therefore reconstructs a shifted pattern the way it reconstructs the original.
  • What it learns at one position transfers to all positions.

Fewer parameters, better generalisation.

  • A dense layer needs separate weights for every input–output pair. A convolution shares a small kernel.
  • This acts as a strong, correct prior for images and signals, where the same local patterns appear everywhere.
  • The model needs far less data and cannot memorise position-specific quirks.

Limits. Strided layers are only approximately shift-equivariant (aliasing). Pure convolutions see only a local window, so long-range structure needs depth, pooling or attention.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Convolution commutes with…
  2. 2. Strided layers are shift-equivariant…
  3. 3. Long-range structure needs…

Level 4: Expert

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

Does weight sharing let an autoencoder handle patterns in unseen positions?

History.

  • J. Masci, U. Meier, D. Cireşan and J. Schmidhuber, “Stacked convolutional auto-encoders for hierarchical feature extraction” (ICANN 2011).
  • A. Odena, V. Dumoulin and C. Olah, “Deconvolution and checkerboard artifacts” (Distill 2016).
  • Convolutional autoencoders are the compression stage of latent diffusion models (Rombach and colleagues, 2022).

Exercise 2.

  • Data: 1-D signals of length 32 with two Gaussian bumps. Training bumps lie only in the left half; the test uses bumps in the left half and in the unseen right half.
  • Models, both 32 → 16 (tanh) → 32, trained for 1500 Adam steps:
    • a dense autoencoder;
    • a convolutional one with a shared 5-tap kernel, stride 2 down and a transposed convolution up.
  • Measurement: relative reconstruction error (squared error divided by signal energy).
model parameters error, left half (seen) error, right half (unseen)
dense 1072 0.0004 4.458
convolutional 12 0.030 0.088
  • The dense autoencoder memorises positions: it fits the training region almost perfectly but fails completely in the right half, worse than outputting zeros (error 1). It never learned weights for those positions.
  • The convolutional autoencoder generalises with 89 times fewer parameters: the same kernel handles bumps wherever they are. Its right-half error is close to its left-half error.
  • The price is a less exact fit on the seen region (0.030 against 0.0004): 12 parameters cannot capture every detail.
  • Seeds: with seeds 1 and 2, the unseen-half errors were 4.63 and 3.41 for dense against 0.038 and 0.028 for convolutional.
🐍 Exercise 2 (research): does weight sharing let an autoencoder handle patterns in unseen positions?
Train dense and convolutional autoencoders on bumps in the left half and test both halves.

Convolutional autoencoders in AI, robotics and aerospace

  • Visual inspection: reconstruction-based defect detection on production lines and aircraft surfaces.
  • Imaging and spectra: denoising satellite images, radar and spectrograms; compressing images for downlink.
  • Generative pipelines: latent diffusion models encode images with a convolutional autoencoder before diffusion.
  • Design lesson: build the symmetries of your data into the architecture. It saves parameters and makes behaviour on unseen cases predictable.

References

  1. J. Masci, U. Meier, D. Cireşan and J. Schmidhuber (2011). “Stacked convolutional auto-encoders for hierarchical feature extraction.” ICANN.
  2. A. Odena, V. Dumoulin and C. Olah (2016). “Deconvolution and checkerboard artifacts.” Distill.
  3. R. Rombach et al. (2022). “High-resolution image synthesis with latent diffusion models.” Proc. CVPR.