liveBook · Manning

Kullback–Leibler divergence

As explained in Learn Generative AI with PyTorch

A divergence measure used as a regularization term to encourage a latent-variable distribution to resemble a normal distribution.

Kullback–Leibler (KL) divergence measures how one probability distribution diverges from an expected distribution. In a variational autoencoder (VAE), it acts as a regularization term: it penalizes deviations of the encoder’s learned distribution from a standard normal prior. section 7.3.1

An ordinary autoencoder minimizes reconstruction loss, aiming to reproduce its inputs. A VAE adds KL divergence to that loss and encodes inputs as distributions rather than fixed points. Reconstruction loss rewards accurate images; the KL term encourages latent representations that generalize rather than simply memorize training examples. section 7.0 section 7.3.1

Training a face-image VAE

An encoder compresses each 3 × 256 × 256 image into a 100-dimensional latent distribution, represented by means and standard deviations. A sampled vector goes to the decoder, which reconstructs the image. Training adds pixel-level reconstruction loss and KL divergence, then propagates the total loss through the network. For the assumed normal distributions, the KL term is summed over the 100 dimensions; it is zero when every mean is 0 and every standard deviation is 1. section 7.3.2 section 7.3.1

Figure 7.6 The architecture of a VAE and the steps to train one to generate human face images. A VAE consists of an encoder (middle upper left) and a decoder (middle bottom right). In each iteration of training, human face images are fed to the encoder (step 1). The encoder compresses the images to probabilistic points in the latent space (step 2; since we assume normal distributions, each probability point is characterized by a vector of means and a vector of standard deviations). We then sample encodings from the distribution and present them to the decoder. The decoder takes sampled encodings (step 3) and reconstructs images (step 4). The VAE adjusts its parameters to minimize the sum of reconstruction loss and the KL divergence. The KL divergence measures the difference between the encoder’s output and a standard normal distribution.

Follow the encoder, sampling, decoder, and combined loss through one training iteration. section 7.3.2

Regularizing the latent space supports more than reconstruction. After training, the decoder can generate new faces from sampled encodings. Encodings can also be averaged, combined, or interpolated to explore changes in image features, such as glasses. section 7.3.2 section 7.4.4

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines divergence and explains its normal-prior minimum across 100 latent dimensions, without deriving the formula.
Examples
Uses face-image reconstruction, sampled face generation, and glasses-related encoding arithmetic and interpolation.
What you can do after
Evaluate and understand how KL regularization contributes to VAE training and latent-space manipulation.
How it connects
Connects variational autoencoders to reconstruction loss, latent space, normal distributions, and overfitting.

Also explained in

Compare how 12 books explain Kullback–Leibler divergence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.