Part 12 - Unsupervised Learning I

Introduction

In this and final part(s), we will explore unsupervised learning methods in probabilistic machine learning. Unsupervised learning involves learning patterns from unlabeled data, which is a common scenario in real-world applications.

The Unsupervised Learning Problem

Intuition (The Unsupervised Learning Problem)

A branch of machine learning that operates over unlabeled data sets D{x1,,xN}\mathcal{D} \coloneqq \{\mathbf{x}_1, \ldots, \mathbf{x}_N\}. The goal is to find meaningful structure in the data, characterized by the presence of hidden (latent) variables that help us explain the structure of the data.

We can (somewhat) formally define this as, given the data set D{xn}n=1Ni.i.dp(x)\mathcal{D} \coloneqq \{\mathbf{x}_n\}_{n = 1}^N \sim_\text{i.i.d} p(\mathbf{x}), learn some useful properties of p(x)p(\mathbf{x}). Thus, the key difference to supervised learning is that we want to model p(x)p(\mathbf{x}) (unsupervised) instead of p(yx)p(y \mid \mathbf{x}) (supervised).

Example 1

Some common unsupervised learning tasks include:

  • Discovering clusters: Clustering data into groups:
    • Assume presence of an unobserved label yny_n (hidden, latent variable) associated to each xn\mathbf{x}_n.
    • Goal: Recovering labels yny_n for all xnD\mathbf{x}_n \in \mathcal{D}.
  • Dimensionality reduction: Reduce dimensionality of data by projecting it to lower dimensional subspace which captures its essence.
    • Data may appear high-dimensional, but there may be a few of degrees in variability (latent factors).
    • Thus, it might be possible to highlight independent explanatory factors; easier to visualize, analyze, and use for downstream tasks.
  • Generation of new samples: Learn a model that produces samples approximately distributed according to p(x)p(\mathbf{x}).
    • Examples: Computer graphics for gaming; Software that can produce artificial scenes based on a given description;

Clustering

We will discuss clustering in more detail, as it is one of the most fundamental unsupervised learning tasks. We will firstly go through KK-means clustering, which is a simple and popular clustering algorithm. Then, we will discuss Gaussian Mixture Models (GMMs), which are a more powerful probabilistic clustering method.

KK-Means Clustering

Intuition (KK-Means Clustering)

Given a data set D{xn}n=1N\mathcal{D} \coloneqq \{\mathbf{x}_n\}_{n = 1}^N, we want to partition it into KK clusters.

  • Here, cluster indices are encoded by categorical variables yn\mathbf{y}_n via one-hot encoding.
  • ynky_{nk} is the kk-th component of yn\mathbf{y}_n.
  • ynk=1y_{nk} = 1 if xn\mathbf{x}_n assigned to cluster kk, ynk=0y_{nk} = 0 otherwise.

But how should we perform the clustering?

The “clusters” should comprise points closer in distance → cluster together points that are mutually close in Euclidean distance (could be other distance metrics as well). KK-means assigns all points in same cluster to a prototype representative μk{μk}\boldsymbol{\mu}_k \rightarrow \{\boldsymbol{\mu}_k\} represent centers of clusters.

Thus, the goal is to find assignment of data points to clusters and {μk}\{\boldsymbol{\mu}_k\} such that all points within a given cluster can be quantized to μk\boldsymbol{\mu}_k with minimum quadratic loss,

{yn},{μk}=argmin{yn},{μk} n=1Nk=1Kynkd(xn,μk)=argmin{yn},{μk} n=1Nk=1Kynkxnμk2,\begin{align*} \{\mathbf{y}_n^\star\}, \{\boldsymbol{\mu}_k^\star\} & = \underset{\{\mathbf{y}_n\}, \{\boldsymbol{\mu}_k\}}{\arg\min} \ \sum_{n = 1}^N \sum_{k = 1}^K y_{nk} d(\mathbf{x}_n, \boldsymbol{\mu}_k) \newline & = \underset{\{\mathbf{y}_n\}, \{\boldsymbol{\mu}_k\}}{\arg\min} \ \sum_{n = 1}^N \sum_{k = 1}^K y_{nk} \Vert \mathbf{x}_n - \boldsymbol{\mu}_k \Vert^2, \end{align*}

where d(,)d(\cdot, \cdot) is a general distance metric, then we call it KK-medoids.

Thus, KK-means solves the minimization problem by alternately optimizing yn\mathbf{y}_n and μk\boldsymbol{\mu}_k.

Algorithm ( K\ K-Means Clustering)
    1. Initialization: Initialize {μk(0)}\{\boldsymbol{\mu}_k^{(0)}\} (e.g., randomly select KK data points as initial cluster centers).
  • For =1,\ell = 1, \ldots:
    1. Assignment step: For fixed {μk(1)}\{\boldsymbol{\mu}_k^{(\ell - 1)}\}, solve the optimization problem for each data point xn\mathbf{x}_n,
yn()={1,for k=argminjxnμj(1)20,otherwise\mathbf{y}_n^{(\ell)} = \begin{cases} 1, & \text{for } k = \arg \min_j \Vert \mathbf{x}_n - \boldsymbol{\mu}_j^{(\ell - 1)} \Vert^2 \newline 0, & \text{otherwise} \newline \end{cases}
    1. Refitting step: For fixed {yn()}\{\mathbf{y}_n^{(\ell)}\}, solve the optimization problem for each cluster center μk\boldsymbol{\mu}_k,
μk()=n=1Nynk()xnn=1Nynk()\boldsymbol{\mu}_k^{(\ell)} = \frac{\sum_{n = 1}^N y_{nk}^{(\ell)} \mathbf{x}_n}{\sum_{n = 1}^N y_{nk}^{(\ell)}}
  • Repeat until convergence (i.e., cluster assignments do not change or change in loss function is below a threshold).

Gaussian Mixture Models (GMMs)

Intuition (Gaussian Mixture Models (GMMs))

In the KK-means case we (try to) place a hyper-sphere at the center of each cluster. But, if the clusters are far from circular, KK-means will not perform well.

The Gaussian Mixture Model (GMM) is a probabilistic model that assumes that the data is generated from a mixture of several Gaussian distributions. Formally, we have,

p(x)=k=1KπkN(xμk,Σk),k=1Kπk=1,p(\mathbf{x}) = \sum_{k = 1}^K \pi_k \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k), \quad \sum_{k = 1}^K \pi_k = 1,

where πk\pi_k are the mixing coefficients and N(xμk,Σk)\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k) are the proportion of Gaussian in the data.

Thus, the goal is to assign each data point to a cluster based on the likelihood of belonging to one of the Gaussian components.

Derivation (Gaussian Mixture Models (GMMs))

Firstly, we have KK clusters, we introduce z[1,,K]\mathbf{\mathsf{z}} \in [1, \ldots, K] which is the hidden (categorical) random variables, representing which Gaussian generated our observation x\mathbf{x}, with some probability. We can write p(x)p(\mathbf{x}) as,

p(x)=k=1Kp(x,z=k)=k=1Kp(z=k)p(xz=k)=k=1KπkN(xμk,Σk).\begin{align*} p(\mathbf{x}) & = \sum_{k = 1}^K p(\mathbf{x}, \mathbf{\mathsf{z}} = k) \newline & = \sum_{k = 1}^K p(\mathbf{\mathsf{z}} = k) p(\mathbf{x} \mid \mathbf{\mathsf{z}} = k) \newline & = \sum_{k = 1}^K \pi_k \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k). \end{align*}

z\mathbf{\mathsf{z}} can be represented by a one-hot vector which means,

p(z=k)=p(zk=1)=πk,p(xz=k)=p(xzk=1)=N(xμk,Σk).\begin{align*} p(\mathbf{\mathsf{z}} = k) & = p(z_k = 1) = \pi_k, \newline p(\mathbf{x} \mid \mathbf{\mathsf{z}} = k) & = p(\mathbf{x} \mid z_k = 1) = \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k). \end{align*}

Thus, we can write,

p(z)=k=1Kπkzk,p(xz)=k=1KN(xμk,Σk)zk.\begin{align*} p(\mathbf{\mathsf{z}}) & = \prod_{k = 1}^K \pi_k^{z_k}, \newline p(\mathbf{x} \mid \mathbf{\mathsf{z}}) & = \prod_{k = 1}^K \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)^{z_k}. \end{align*}

Since we are interested in the posterior,

p(zk=1x)=p(zk=1)p(xzk=1)p(x)=πkN(xμk,Σk)j=1KπjN(xμj,Σj)\begin{align*} p(z_k = 1 \mid \mathbf{x}) & = \frac{p(z_k = 1) p(\mathbf{x} \mid z_k = 1)}{p(\mathbf{x})} \newline & = \frac{\pi_k \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)}{\sum_{j = 1}^K \pi_j \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_j, \boldsymbol{\Sigma}_j)} \newline \end{align*}

p(zk=1x)p(z_k = 1 \mid \mathbf{x}) is the posterior probability that the data point x\mathbf{x} belongs to cluster kk.

Note

In the KK-means case, we hard assign each data point to a cluster. In GMMs, we soft assign each data point to clusters based on the posterior probabilities.

Intuition (Learning Model Parameters in GMMs)

We want to learn θ={π,μ,Σ}\boldsymbol{\theta} = \{\boldsymbol{\pi}, \boldsymbol{\mu}, \boldsymbol{\Sigma}\} from data set D{xn}n=1N\mathcal{D} \coloneqq \{\mathbf{x}_n\}_{n = 1}^N. Using MLE we find that,

θ=argmaxθ logp(Dθ)=argmaxθ log(n=1Np(xnθ))=argmaxθ n=1Nlogp(xnθ)=argmaxθ n=1Nlog(k=1KπkN(xnμk,Σk)).\begin{align*} \boldsymbol{\theta}^{\star} & = \underset{\boldsymbol{\theta}}{\arg\max} \ \log p(\mathcal{D} \mid \boldsymbol{\theta}) \newline & = \underset{\boldsymbol{\theta}}{\arg\max} \ \log \left(\prod_{n = 1}^N p(\mathbf{x}_n \mid \boldsymbol{\theta})\right) \newline & = \underset{\boldsymbol{\theta}}{\arg\max} \ \sum_{n = 1}^N \log p(\mathbf{x}_n \mid \boldsymbol{\theta}) \newline & = \underset{\boldsymbol{\theta}}{\arg\max} \ \sum_{n = 1}^N \log \left(\sum_{k = 1}^K \pi_k \mathcal{N}(\mathbf{x}_n \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\right). \end{align*}

However, this optimization problem does not have a closed-form solution. We can use the Expectation-Maximization (EM) algorithm to iteratively find an approximate solution.

Derivation (Expectation-Maximization (EM) for GMMs)

The EM algorithm gets around our problem by using a common trick in machine learning. Introduce a new distribution qn(zn)q_n(\mathbf{z}_n) over the hidden variables zn\mathbf{\mathsf{z}}_n,

(θ)n=1Nlogznqn(zn)p(xn,znθ)qn(zn)=n=1NlogEqn(zn)[p(xn,znθ)qn(zn)]\begin{align*} \ell(\boldsymbol{\theta}) & \coloneqq \sum_{n = 1}^N \log \sum_{\mathbf{z}_n} q_n(\mathbf{z}_n) \frac{p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})}{q_n(\mathbf{z}_n)} \newline & = \sum_{n = 1}^N \log \mathbb{E}_{q_n(\mathbf{z}_n)} \left[\frac{p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})}{q_n(\mathbf{z}_n)}\right] \newline \end{align*}

Using Jensen’s inequality (i.e., f(E[X])E[f(X)]f(\mathbb{E}[X]) \geq \mathbb{E}[f(X)] for concave ff), we have,

(θ)n=1NEqn[logp(xn,znθ)qn(zn)]=n=1N(Eqn[logp(xn,znθ)]znqn(zn)logqn(zn))=n=1N(Eqn[logp(xn,znθ)]+H(qn))=L(q,θ).\begin{align*} \ell(\boldsymbol{\theta}) & \geq \sum_{n = 1}^N \mathbb{E}_{q_n} \left[\log \frac{p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})}{q_n(\mathbf{z}_n)}\right] \newline & = \sum_{n = 1}^N \left(\mathbb{E}_{q_n} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})] - \sum_{\mathbf{z}_n} q_n(\mathbf{z}_n) \log q_n(\mathbf{z}_n)\right) \newline & = \sum_{n = 1}^N \left(\mathbb{E}_{q_n} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})] + \mathsf{H}(q_n)\right) \newline & = \mathcal{L}(\boldsymbol{q}, \boldsymbol{\theta}). \end{align*}

L(q,θ)\mathcal{L}(\boldsymbol{q}, \boldsymbol{\theta}) is the lower bound on logp(Dθ)\log p(\mathcal{D} \mid \boldsymbol{\theta}) (i.e., evidence lower bound, ELBO).

Thus, we can maximize L(q,θ)\mathcal{L}(\boldsymbol{q}, \boldsymbol{\theta}) instead of (θ)\ell(\boldsymbol{\theta}). Maximizing the ELBO will force the log likelihood (θ)\ell(\boldsymbol{\theta}) to increase as well. The EM algorithm alternates between two steps. Firstly, the E-step.

L(q,θ(1))=Eqn[logp(xn,znθ(1))qn(zn)]=znqn(zn)logp(xn,znθ(1))qn(zn)=znqn(zn)logp(znxn,θ(1))p(xnθ(1))qn(zn)=znqn(zn)logp(znxn,θ(1))qn(zn)+znqn(zn)logp(xnθ(1))= KL(qn(zn)p(znxn,θ(1)))+logp(xnθ(1)).\begin{align*} \mathcal{L}(\boldsymbol{q}, \boldsymbol{\theta}^{(\ell - 1)}) & = \mathbb{E}_{q_n} \left[\log \frac{p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta}^{(\ell - 1)})}{q_n(\mathbf{z}_n)}\right] \newline & = \sum_{\mathbf{z}_n} q_n(\mathbf{z}_n) \log \frac{{p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta}^{(\ell - 1)})}}{q_n(\mathbf{z}_n)} \newline & = \sum_{\mathbf{z}_n} q_n(\mathbf{z}_n) \log \frac{p(\mathbf{z}_n \mid \mathbf{x}_n, \boldsymbol{\theta}^{(\ell - 1)}) p(\mathbf{x}_n \mid \boldsymbol{\theta}^{(\ell - 1)})}{q_n(\mathbf{z}_n)} \newline & = \sum_{\mathbf{z}_n} q_n(\mathbf{z}_n) \log \frac{p(\mathbf{z}_n \mid \mathbf{x}_n, \boldsymbol{\theta}^{(\ell - 1)})}{q_n(\mathbf{z}_n)} + \sum_{\mathbf{z}_n} q_n(\mathbf{z}_n) \log p(\mathbf{x}_n \mid \boldsymbol{\theta}^{(\ell - 1)}) \newline & = - \ \mathrm{KL}(q_n(\mathbf{z}_n) \mid \mid p(\mathbf{z}_n \mid \mathbf{x}_n, \boldsymbol{\theta}^{(\ell - 1)})) + \log p(\mathbf{x}_n \mid \boldsymbol{\theta}^{(\ell - 1)}). \end{align*}

Thus, the E-step fixes θ\boldsymbol{\theta} and optimizes qnq_n → For a fixed θ\boldsymbol{\theta} (θ(1))(\boldsymbol{\theta}^{(\ell - 1)}), we can maximize L(q,θ)\mathcal{L}(\boldsymbol{q}, \boldsymbol{\theta}) by setting each term to qn(zn)=p(znxn,θ)q_n^{\star}(\mathbf{z}_n) = p(\mathbf{z}_n \mid \mathbf{x}_n, \boldsymbol{\theta}).

Secondly, the M-step. Here, we fix qn(zn)q_n(\mathbf{z}_n) and optimize θ\boldsymbol{\theta}.

L(θ)=n=1NEqn[logp(xn,znθ)]+H(qn)n=1NEqn[logp(xn,znθ)],\begin{align*} \mathcal{L}(\boldsymbol{\theta})^{\ell} & = \sum_{n = 1}^N \mathbb{E}_{q_n^{\ell}} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})] + \mathsf{H}(q_n^{\ell}) \newline & \propto \sum_{n = 1}^N \mathbb{E}_{q_n^{\ell}} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})], \end{align*}

where qn(zn)=p(znxn,θ(1))q_n^{\ell}(\mathbf{z}_n) = p(\mathbf{z}_n \mid \mathbf{x}_n, \boldsymbol{\theta}^{(\ell - 1)}) from the E-step. We see that this is the expected complete data log-likelihood. Thus,

θ(+1)=argmaxθ n=1NEqn[logp(xn,znθ)]\boldsymbol{\theta}^{(\ell + 1)} = \underset{\boldsymbol{\theta}}{\arg\max} \ \sum_{n = 1}^N \mathbb{E}_{q_n^{\ell}} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})]

Let’s observe what we have derived. EM based on observation that solving θ=argmaxθ logp(Dθ)\boldsymbol{\theta}^{\star} = \underset{\boldsymbol{\theta}}{\arg\max} \ \log p(\mathcal{D} \mid \boldsymbol{\theta}) would be easy to solve given the latent variables z1,,zn\mathbf{z}_1, \ldots, \mathbf{z}_n having been used implicitly to generate the data samples x1,,xn\mathbf{x}_1, \ldots, \mathbf{x}_n. Thus, we can consider the maximization of the complete data log-likelihood,

c(θ)logp(D,Zθ).\ell_c(\boldsymbol{\theta}) \coloneqq \log p(\mathcal{D}, \mathbf{Z} \mid \boldsymbol{\theta}).

However, as we do not have values for Z\mathbf{Z}, we consider the expectation,

EZp(ZD,θ(1))[c(θ)]=EZp(ZD,θ(1))[logp(D,Zθ)]=EZp(ZD,θ(1))[n=1Nlogp(xn,znθ)]=n=1NEznp(znxn,θ(1))[logp(xn,znθ)]\begin{align*} \mathbb{E}_{\mathbf{\mathsf{Z}} \sim p(\mathbf{Z} \mid \mathcal{D}, \boldsymbol{\theta}^{(\ell - 1)})} [\ell_c(\boldsymbol{\theta})] & = \mathbb{E}_{\mathbf{\mathsf{Z}} \sim p(\mathbf{Z} \mid \mathcal{D}, \boldsymbol{\theta}^{(\ell - 1)})} [\log p(\mathcal{D}, \mathbf{Z} \mid \boldsymbol{\theta})] \newline & = \mathbb{E}_{\mathbf{\mathsf{Z}} \sim p(\mathbf{Z} \mid \mathcal{D}, \boldsymbol{\theta}^{(\ell - 1)})} \left[\sum_{n = 1}^N \log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})\right] \newline & = \sum_{n = 1}^N \mathbb{E}_{\mathbf{\mathsf{z}}_n \sim p(\mathbf{z}_n \mid \mathbf{x}_n, \boldsymbol{\theta}^{(\ell - 1)})} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})] \newline \end{align*}

We don’t know Z()\mathbf{\mathsf{Z}}^{(\ell)}, so we average them out given the current model θ(1)\boldsymbol{\theta}^{(\ell - 1)}. In practice, we can define a function,

Q(θ,θ(1))Eqn[logp(xn,znθ)]Q(\boldsymbol{\theta}, \boldsymbol{\theta}^{(\ell - 1)}) \coloneqq \mathbb{E}_{q_n^{\ell}} [\log p(\mathbf{x}_n, \mathbf{z}_n \mid \boldsymbol{\theta})]

that lower bounds the desired function.

If we now optimize for θ\boldsymbol{\theta}, we will get a better lower bound,

L(θ(1))L(θ())(θ())logp(Dθ())\mathcal{L}^{\star}(\boldsymbol{\theta}^{(\ell - 1)}) \leq \mathcal{L}(\boldsymbol{\theta}^{(\ell)}) \leq \ell(\boldsymbol{\theta}^{(\ell)}) \eqqcolon \log p(\mathcal{D} \mid \boldsymbol{\theta}^{(\ell)})

Finally, we can iterate between the E-step and the M-step and our lower bound will always improve until convergence.

Algorithm (Expectation-Maximization (EM) for GMMs)
    1. Initialization: Initialize θ(0)\boldsymbol{\theta}^{(0)}.
  • For =1,\ell = 1, \ldots:
    1. E-step: Evaluate p(ZD,θ(1))p(\mathbf{Z} \mid \mathcal{D}, \boldsymbol{\theta}^{(\ell - 1)}) and compute,
Q(θ,θ(1))=Zp(ZD,θ(1))logp(D,Zθ)Q(\boldsymbol{\theta}, \boldsymbol{\theta}^{(\ell - 1)}) = \sum_{\mathbf{\mathsf{Z}}} p(\mathbf{Z} \mid \mathcal{D}, \boldsymbol{\theta}^{(\ell - 1)}) \log p(\mathcal{D}, \mathbf{Z} \mid \boldsymbol{\theta})
    1. M-step: Solve the optimization problem,
θ()=argmaxθ Q(θ,θ(1))\boldsymbol{\theta}^{(\ell)} = \underset{\boldsymbol{\theta}}{\arg\max} \ Q(\boldsymbol{\theta}, \boldsymbol{\theta}^{(\ell - 1)})
    1. If convergence criterion not satisfied, return to step 2.