18 min total

Machine Learning

Variational inference

Variational inference

suppose we have some observed/learned data x
and a hidden variable z

we calculate a posterior distribution P(z|x)

given this image x what latent vectors could it have generated

Bayes’ rule

In a generative model, x is observed data, p(z) is the prior over latent variables, and p(x|z) describes how a latent variable generates an observation. The evidence p(x) requires summing or integrating over z; this can make the posterior p(z|x) difficult to compute.

one generative model's output x can be derived from multiple z

P(x) means total possible ways of generating x

P(x) = summation_allz[p(x,z)] for discrete variables


for continuous variables
P(x) = integral[p(x,z) dz]

where P(x, z) is a joint probability distribution (probability of x and z occurring together)
P(x, z) = P(x|z) P(z) (chain rule)

P(x) = integral[P(x|z) P(z) dz]

during integral we are calculating for every possible latent vector z, add up how much it contributes to producing x

in Neural nets, z is huge continuous vector space

if our latent space is 128 dims

With 10 candidate values along each of 128 dimensions, a full grid would require 10^128 evaluations.

Another problem is wasted compute over calculating z's that don't matter in our calculation

We approximate the posterior p(z|x) with a tractable distribution q(z|x). An expectation is a weighted average with respect to a probability distribution.

p(x) = integral[p(x|z) p(z) dz]

p(x) = integral[p(x|z) p(z) q(z|x)/q(z|x) dz] (introduces a term that cancels itself out)

so P(x) = E_q(z|x) [p(x, z)/q(z|x)]

it means take multiple values from supscript and take average of the computed values within [...some compute] function(s)


we also add log( ) terms to ensure numerical stability

log(p(x)) = log E_q(z|x) [p(x, z)/q(z|x)]

Expectations and marginal likelihood

Taking a logarithm outside an expectation is not the same as taking the expected logarithm. Jensen’s inequality gives a lower bound that can be optimized instead.

Logarithms and expectations

Jensen’s inequality

jensen tells us:

log E[X] >= E[log X] (like compute kark daikhly)

therefore

log(p(x)) >= E_q(z|x)[log p(x, z)/q(z|x)]

The evidence lower bound (ELBO)

our goal is to maximize ELBO term

since p(x, z) = p(x|z)p(z) (chain rule)

ELBO = E_q(z|x)[log p(x|z) + log p(z) - log q(z|x)]

KL divergence

ELBO = E_q(z|x)[log p(x|z)] - D_KL(q(z|x)||p(z))

Variational Bayesian Gaussian mixtures

normal GMMs require you to set the number of clusters manually.

A Bayesian mixture starts with an upper bound on the number of components. Priors and variational inference can shrink unused component weights toward zero; they do not guarantee recovery of a uniquely optimal number of clusters.

process:

1. Assume max clusters K = 10 or 50

a. each cluster has a mean, variance, weight

2. initialize random cluster metadata

a. random means

b. random variances

c. random weights

d. initialize responsibilities

3. for each data point, compute probability that point n belongs to cluster k

4. now recompute cluster params using responsibility variable values

5. apply a prior on weights, shrink unnecessary clusters towards zero

6. compute ELBO