Tutorial em
Tutorial em
and EM algorithm
Radek Danecek
Gaussian Mixture Model
• Unsupervised method
• Fit multimodal Gaussian distributions
Formal Definition
• The model is described as:
• Apply MLE:
• Maximize:
• Algorithm:
• Initialize model parameters (randomly):
• Iterate until convergence:
• E-step
• Assign cluster probabilities (“soft labels”) to each sample
• M-step
• Solve the MLE using the soft labels
Initialization
• Initialize model parameters (randomly)
• Covariances:
• Spherical, according to empirical variance
E-step
K
• For each data point and each
cluster , compute the probability
that belongs to Probabilities of point n
belonging to clusters 1…K
(given current model parameters) (sum up to 1)
N
“soft labels”
E-step
• For each data point and each
cluster , compute the probability
that belongs to
(given current model parameters)
“soft labels”
M-step
• Now we have “soft labels” for the data -> fall back to supervised MLE
• Differentiate w.r.t.
M-step
K
• Update model parameters:
Probabilities of point n
belonging to clusters 1…K
(sum up to 1)
• Update prior for each cluster: N
Input Image
Practical Example – Color segmentation
3 clusters
Practical Example – Color segmentation
4 clusters
Practical Example – Color segmentation
5 clusters
Practical Example – Color segmentation
7 clusters
Practical Example – Color segmentation
8 clusters
Practical Example – Color segmentation
9 clusters
Practical Example – Color segmentation
10 clusters
Practical Example – Color segmentation
15 clusters
Practical Example – Color segmentation
20 clusters
Practical Example – Color segmentation
Input Image
EM Algorithm for GMMs
• Idea:
• Objective function:
• Split optimization of the objective into to parts
• Algorithm:
• Initialize model parameters (randomly):
• Iterate until convergence:
• E-step
• Assign cluster probabilities (“soft labels”) to each sample
• M-step
• Find optimal parameters given the soft labels
Generalized EM
• Idea:
• Objective function:
• Split optimization of the objective into to parts
• Algorithm:
• Initialize model parameters (randomly):
• Iterate until convergence:
• E-step
• Assign cluster probabilities (“soft labels”) to each sample
• M-step
• Find optimal parameters given the soft labels
Generalized EM
• Idea:
• Objective function:
• Split optimization of the objective into to parts
• Algorithm:
• Initialize model parameters (randomly):
• Iterate until convergence:
• E-step
• Assign cluster probabilities (“soft labels”) to each sample
• M-step
• Find optimal parameters given the soft labels
Generalized EM
• Idea:
• Objective function:
• Split optimization of the objective into to parts
• Algorithm:
• Initialize model parameters (randomly):
• Iterate until convergence:
• E-step
• Assign cluster probabilities (“soft labels”) to each sample
• M-step
• Find optimal parameters given the soft labels
Generalized EM
• Idea:
• Objective function:
• Split optimization of the objective into to parts
• Algorithm:
• Initialize model parameters (randomly):
• Iterate until convergence:
• E-step
• Assign cluster probabilities (“soft labels”) to each sample
• M-step
• Find optimal parameters given the soft labels
Generalized M-step
• What is the objective function?
• GMM:
• General:
Exercise
• Consider a mixture of K multivariate Bernoulli distributions with
parameters , where
• EM objective:
Exercise
• Multivariate Bernoulli distribution:
• EM objective:
Exercise
• Multivariate Bernoulli distribution:
• EM objective:
Exercise
• Multivariate Bernoulli distribution:
• EM objective:
Exercise
• Question 3: Write down the M-step update
• Differentiate wrt.:
Exercise
• Question 3: Write down the M-step update
• Differentiate wrt.:
Exercise
• Question 3: Write down the M-step update
• Differentiate wrt.:
Summary
• EM algorithm is useful for fitting GMMs (or other mixtures) in an
unsupervised setting
Source: https://scikit-learn.org/stable/modules/clustering.html
Alternative for density estimation
• Kernel density estimation
Source: https://scikit-learn.org/stable/auto_examples/neighbors/plot_species_kde.html
References
• Lecture slides/videos
• https://www.python-
course.eu/expectation_maximization_and_gaussian_mixture_models.
php
• https://scikit-learn.org/stable/modules/clustering.html