A variational autoencoder (VAE) is a generative neural model with an encoder and a decoder. Unlike a traditional autoencoder, which maps an image to a fixed latent vector, its encoder describes a probability distribution in a compact latent space. Sampling from that distribution lets the decoder produce reconstructions with variation. section 11.2.2
Follow an image through the model:
- Encode: turn the image into parameters of a latent distribution, typically a normal distribution described by a mean and standard deviation. section 11.2.2
- Sample and decode: draw a latent representation, then reconstruct an image from it. A smooth latent space supports transitions between representations. section 9.1.1
- Train: combine reconstruction loss, which measures image differences, with KL divergence, which measures departure from a prior distribution. Both losses guide parameter updates. section 11.2.2
Compress and check the reconstruction
Center-crop and resize a beaver image and a dolphin image to 3 × 512 × 512. A pretrained encoder samples a 4 × 64 × 64 latent for each; the decoder returns 3 × 512 × 512 reconstructions. Compare each reconstruction with its original rather than treating the output shape alone as proof that detail was preserved. This workflow uses pretrained weights, not training a VAE from scratch. section 9.2.1 section 9.2.2 section 11.2
N=torch.distributions.Normal(0, 1)
noise=N.sample(torch.Size([1,4,64,64])) #1
latent1=encoder(image1.unsqueeze(0),
noise)[0]
latent2=encoder(image2.unsqueeze(0),
noise)[0] #2
print(latent1.shape,latent2.shape) #3
The encoder call pairs an image tensor with sampled normal noise and produces a compact latent tensor. section 9.2.2
A latent diffusion model uses this compression to do costly denoising in a smaller space, then passes the generated latent to a VAE decoder for the final high-resolution image. For example, reducing 3 × 512 × 512 to 4 × 64 × 64 makes the diffusion input 48 times smaller. The trade-off is potential lost detail: conventional VAEs can also produce blurry reconstructions, a limitation addressed differently by vector-quantized VAEs. section 9.0 section 11.6
Figure 11.4 The architecture of a variational autoencoder (VAE) and the training steps involved in generating high-resolution color images via the VAE’s two primary components: an encoder (upper-middle left) and a decoder (lower-middle right)
Trace the encoder, latent sampling, decoder and training losses together before using the components separately. section 11.2.2












