8 Estimation II
8.1 Multimodality
To build generative AI, we rely on Maximum Likelihood Estimation
(MLE). However, MLE has a prerequisite: you must first define the
mathematical shape of the probability distribution before you can find
its parameters.
The problem is that real-world data is very complex to model. For
example, in image generation there are many ways to arrange the pixels,
and in text generation, there isn’t just one correct next word.
There are multiple, highly probable correct answers. In statistics, this
means the data has multiple peaks (modes). Standard statistical models
(like Gaussian distribution) are too simple for real-world data (one
have one peak).
Hence, we need a way to represent complex, multimodal datasets.
8.2 Latent Variable Models
8.2.1 Definition and Efficient Representation
Because real-world data cannot be effectively modelled with simple
probability distributions, we use latent variables. We assume that the
observable data is generated by a smaller set of underlying,
unobservable factors.
Notations:
Observation, : The raw, complex data (like pixels of an image or words in a sentence).
Latent variable, : hidden, unobserved factor that acts as an essential, compressed representation of the observation .
Joint distribution, .
The prior : he distribution of the latent factors themselves. Before generating an image, the model randomly selects a set of traits from this distribution. For example, it might sample a -vector that mathematically translates to "highly tilted" and "very thick".
Conditional distribution : Once the hidden traits () are chosen, this distribution determines the actual probability of the raw pixels () given those specific traits.
Data generation: To generate a new data point , the model performs a two-step sampling process: first, sample , and then sample the image .
For example, each image (one digit) in the MNIST database is
pixels. The raw observation
exists in 784-dimensional space, which is computationally massive and
inefficient.
Out of all combinations of 784 pixels, actual handwritten digits occupy
a very small fraction. The latent variable
captures just the necessary semantic features needed to draw the digit
in a much lower dimension.
Some
factors might be "which digit is it?"; "How slanted is it?"; "How thick
is the stroke?".
Arithmetic operations in the latent space can admit meaningful semantic
interpretations, in contrast to pixel space, where direct image addition
lacks semantic meaning.
Here, serves as the decoder of the encoder-decoder model. We feed a low-dimensional vector of numbers (), and the network outputs a high-dimensional object (). In contrast, the encoder would be represented by the posterior distribution .
8.2.2 The Manifold Hypothesis
The Manifold Hypothesis says that high-dimensional, real-world data
(like images or text) often lies on or near a lower-dimensional,
structured manifold embedded within that high-dimensional space.
In the MNIST example, in the 84-dimensional space of a 28x28 pixel
image, useful, structured data (like actual digits) tend to cluster
along some lower-dimensional manifold within that space. Everywhere else
in the higher-dimensional space, it is usually noise.
The implication is that we don’t need to waste computing power trying to
learn the rules for the entire 784-dimensional space. Instead, we only
need to learn the latent variable coordinates that map onto that
manifold.
8.2.3 Gaussian Mixture Models (GMM)
A Gaussian Mixture Model (GMM) is a probabilistic model assuming all
data points are generated from a mixture of a finite number of Gaussian
distributions with unknown parameters. It helps to solve our problem of
modelling multiple peaks.
To create a GMM with
different Gaussian distributions on a
-dimensional
space
(),
the overall PDF is written as:
(
refers to the shape of the data, in the MNIST example,
)
The parameters:
: The mean (center) of the -th Gaussian.
: The covariance matrix (shape/spread) of the -th Gaussian.
: The mixing proportion (or weight) of the -th Gaussian. This dictates how much "influence" that specific Gaussian has on the total mixture. The sum of all mixing proportions must equal 1 ().
The hyperparameter: The number of mixes, . We have to decide how many clusters to look for before training the model.
The GMM is a specific type of latent variable model. We can translate
the GMM equation into the
framework.
Here, the latent variable,
,
answers the question: "Which specific Gaussian cluster did this data
point come from?". Mathematically,
This means
is a one-hot vector of
numbers where exactly one number is a ’1’ and the rest are ’0’s. For
example, if we have
clusters, and a data point was generated by the second Gaussian, then
.
We can represent the generation process using this :
The Prior : This is the probability of picking a specific cluster before we even look at the data. Note that this is just its mixing weight . We write this as: If and , we get , which collapses down to just output the weight of the chosen cluster.
The Conditional : This is the probability of generating data , once we have chosen cluster . We write this as: Using the same example, we get , which again isolates the PDF of the single chosen Gaussian.
Marginal distribution of : Note that summing out reconstructs the original GMM equation. Using does not change the shape of the model.
8.3 Expectation-Maximization (EM) Algorithm
We want to run MLE on the latent variable model to get the best values for the parameters (means), (covariances) and (mixing weights), so we have a decoder.
8.3.1 Intractability of Direct MLE
8.3.1.1 Why do we need to use the latent variable ?
Recall the MLE equation:
If we apply MLE on the GMM equation by only looking at
,
we get
Taking the derivative is algebraically intractable. In other words, it
is hard to simplify this as the summation is in the log.
What if know
for every data point?
If we consider the joint probability
,
we have
This equation is easier to
simplify as the product is in the log, so by properties of logarithms,
the summation will be outside of the log.
8.3.2 The EM Framework (E-step and M-step)
However, to use
(to simplify the MLE equation above), we need to know the value of
(and by extension,
).
The EM algorithm is a way to find a MLE for problems where we have
latent variables, i.e.
.
The overarching flow is to iteratively improve our guess of the
parameters
.
We start with a completely random guess
,
then use EM algorithm to find a better guess.
We have i.i.d. observations
,
and note that
.
Recall that we wanted to maximise the true log-likelihood of our data:
But the problem is that there is a summation inside the log, making this
algebraically intractable.
8.3.2.1 The E-step
We cannot directly optimize the likelihood because we do not know the
hidden variables
.
In this step, we use our current, locked-in parameter guesses
to evaluate the probabilities of the hidden variables. Mathematically,
this involves constructing a surrogate lower-bound function (which will
become the Q-function) that approximates the true log-likelihood.
Jensen’s Inequality: For random variable
and concave function
(
frown shape function),
Note that we defined a
surrogate function
.
Part 2 is the complete-data joint probability. This is the algebraically easier to solve equation, and we want to optimise this, but we need to know the hidden .
Part 1 acts as the weight, such that Part 1 Part 2 gives expected value.
Part 3 is the same term as Part 1, and acts as a normalisation constant to ensure the lower bound perfectly touches our current position.
2 properties of :
. is guaranteed to be a lower bound that always sits underneath the true log-likelihood.
. When , perfectly equals the true log-likelihood.
Derivation:
Getting the Q-function:
8.3.2.2 The M-step
Now we have a surrogate function built using our expectations of the
hidden variables. The goal of the M-step is to find the brand new
parameters
()
that maximize the Q function. We take the derivative of the Q-function
with respect to our parameters
and solve for zero to find the brand new, updated parameters
.
Note that Term 2 does not contain
,
it only contains
(our old, locked-in guesses), so we can discard it from the
Q-function.
A more tractable form of the Q-function is:
is a for loop over the dataset
is a for loop over the clusters
is the current guess for that single data point
is the complete-data log-likelihood for that single data point.
8.3.3 Application to Gaussian Mixtures
The Q-function is For a GMM, the parameters consist of the mixing weights (), the means (), and the covariance matrices () for all clusters. Therefore, and for a specific cluster , .
8.3.3.1 The E-step
In this step, the goal is to use our current best guess of the
parameters, denoted as
,
to compute the probability that a specific data point
came from a specific cluster
.
This probability is called the responsibility, denoted as
.
We calculate it using Bayes’ Theorem:
Numerator: The probability of picking cluster () multiplied by the Gaussian probability density of point under cluster ’s current mean and covariance.
Denominator: The sum of that exact same calculation across all clusters. This normalizes the values so that the responsibilities for a single point add up to exactly 1.
8.3.3.2 The M-step
We plug these responsibilities into a surrogate function called the
Q-function. The Q-function acts as a lower bound to our true,
intractable log-likelihood. By maximizing the Q-function, we push the
true likelihood up.
The Q-function for a GMM looks like this:
We now take the derivatives of the Q-function with respect to our
parameters
(),
set them to zero, and solve. This gives us the exact formulas to update
our clusters to fit the data better, treating the responsibilities
as fixed weights.
First, we calculate the "effective number of points" assigned to cluster
by summing up all the responsibilities for that cluster:
Then, we use
to calculate our brand new parameters,
:
Update the Mixing Weights: The new weight is simply the effective number of points in cluster divided by the total number of points .
Update the Means: The new center is a weighted average of all data points. Points with a high responsibility for this cluster pull the mean closer to themselves.
Update the Covariance Matrices: The new shape is a weighted covariance calculation. Notice the use of the outer product which gives us the required matrix structure, scaled by how much each point belongs to the cluster.
We now have a new set of parameters
.
These are fed back into the E-step to calculate fresh responsibilities,
which are then fed again into the M-step to get even better
parameters.
This is repeated until the parameters stop changing significantly, at
which point the algorithm has converged and the best fitting Gaussian
distribution has been found.