liveBook · Manning

Kullback–Leibler divergence

As explained in Evolutionary Deep Learning: Genetic algorithms and neural networks

A divergence-based measure comparing probability distributions through differences in their information content.

Kullback–Leibler divergence measures distributional loss between an input and a learned representation. In a variational autoencoder (VAE), it complements reconstruction loss so training captures input-distribution structure rather than only pixel differences. section 8.5.2

A practical VAE loss combines:

  • A reconstruction term using mean squared error or binary cross entropy. section 8.5.2
  • A KL term computed from the latent mean and log variance, summed across latent dimensions and scaled by −0.5. section 8.5.2
  • The mean of both terms, registered as the model loss with Keras add_loss. section 8.5.2
Listing 8.27 EDL_8_5_VAE.ipynb: Building the VAE model
use_mse = False
if use_mse:
  reconstruction_loss = mse(
      K.flatten(inputs), K.flatten(outputs))      #1
else:
  reconstruction_loss = binary_crossentropy(
      K.flatten(inputs), K.flatten(outputs))      #1
 
reconstruction_loss *= image_size * image_size    #2
kl_loss = 1 + z_log_var - K.square(z_mean) – 
 K.exp(z_log_var)                               #3
kl_loss = K.sum(kl_loss, axis=-1)                 #3
kl_loss *= -0.5                                   #3
 
vae_loss = K.mean(tf.math.add(reconstruction_loss, 
 kl_loss))                                      #4
vae.add_loss(vae_loss)                            #5
 
vae.compile(optimizer='adam')
vae.summary()

Inspect the executable Keras implementation of reconstruction loss, KL divergence, combined loss, and model compilation. section 8.5.2

Comparing loss choices

Normalize the observed loss, then switch reconstruction_loss between MSE and binary cross entropy to compare training behavior despite different output scales. section 8.5.2

How Evolutionary Deep Learning: Genetic algorithms and neural networks covers it

How deep it goes
Defines divergence as statistical distance and shows its implementation formula, while deferring deeper treatment of statistical distance and loss calculations.
Examples
Uses a Keras VAE implementation and compares normalized training losses produced by MSE and binary cross entropy.
What it assumes
Builds from autoencoder reconstruction loss and introduces probability-distribution comparison through a VAE example.
What you can do after
Apply it via Keras by adding a KL term to a VAE’s reconstruction loss.
How it connects
Connects probability distributions, variational autoencoders, latent representations, reconstruction loss, MSE, and binary cross entropy.

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 Evolutionary Deep Learning: Genetic algorithms and neural networks. The section links point to the parts of the book it draws on.