2024-02-04

Diving into Kullback-Leibler Divergence

The Kullback-Leibler Divergence (KLD) measures how one probability distribution differs from another. In some sense, the KLD can be considered as a statistical distance, however it is not actually a metric/distance function primarily due to its asymmetry and other factors.

KLD can be applied to both discrete and continuous distributions with the corresponding formulas:


Discrete: \(D_{KL}(P \parallel Q) = \sum_{i}^{}p(x_i)\log(\frac{p(x_i)}{q(x_i)})\)

Continuous: \(D_{KL}(P \parallel Q) = \int_{- \infty}^{\infty}p(x)\log(\frac{P(dx)}{Q(dx)})dx\)

Calculating & Interpreting KLD Values

We will be using the library philentropy and KL function for calculating KLD values, however, here are naive examples of how to calculate KLD yourself.

kld_discrete <- function(p, q) sum(p * log(p / q))
kld_continuous <- function(p_density, q_density, lower, upper) {
  integrate(function(x) {
    p_x <- p_density(x)
    q_x <- q_density(x)
    ifelse(p_x > 0, p_x * log(p_x / q_x), 0)
  }, lower, upper)$value
}

The smaller a KDL value is the more similar it is with the theoretical distribution. The larger it is the more dissimilar it is. A KDL value ranges from 0 to infinity, inclusive.

Calculating KLD for Similar Dists

We will be calculating the KLD for two similar dists: Logistic & Gumbel

\(D_{KL}(rlogis(X) \parallel rgumbel(X)) =\) 0.1360312 Similar!

Calculating KLD for Dissimilar Dists

We will be calculating the KLD for two dissimilar dists: T & Normal

\(D_{KL}(rt(X) \parallel rnorm(X)) =\) 1.2266746 Not Similar!

Application: Variational Autoencoder

A Variational Autoencoder (VAE) is a type of artificial neural network (ANN) architecture classified under generative ANNs. VAEs are used in unsupervised machine learning and probabilistic modeling specifically for dimensionality reduction and feature learning. A common component for VAE’s loss function is the Kullback-Leibler divergence term conjoined with binary cross entropy.

VAE’s use a multivariate normal distribution as a latent space for embeddings. During training, we will use a special case of the KDL to compare multivariate normal distributions across epochs. The special case of the KDL looks like this.


\(D_{KL}[N(\mu,\sigma) \parallel N(0, 1)] = -\frac{1}{2}\sum(1 + \log(\sigma^2) -\mu^2 - \sigma^2)\)

Multivariate Normal Distribution

A multivariate normal distribution deals with multiple random variables simultaneously, unlike a univariate normal distribution. The distribution describes the joint behavior of a set of random variables.

The three characteristics that describe a multivariate normal distribution are:

  • Random Variables - continuous/discrete input data
  • Mean Vector - n-dimensional vector where \(\mu_1\) is for \(X_1\)
  • Covariance Matrix - covariances between pairs of random variables

PDF of multivariate normal distibution in k dimentions \(f(x_1, ..., x_k) = \frac{exp(-\frac{1}{2}(x-\mu)^{T}\sum{}^{-1}(x - \mu))}{\sqrt{(2\pi)^k|\sum|}}\)

What does a MND look like?

x = observed/input data
y = reconstructed data
z = \(\mu + \sigma\epsilon\)

Bringing It All Togther

The KL divergence term imposes a penalty on the network when it encodes observations into the z_mean and z_log_var variables, and these encoded values deviate substantially from the expected values of a standard normal distribution, where z_mean should ideally be 0, and z_log_var should ideally be 0, where:

z_mean - the mean point of the distribution
z_log_var - the logarithm of the varaiance of each dimension

kl_loss <- -0.5 * sum(1 + z_log_var - z_mean^2 - exp(z_log_var))