Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Special Topics

1. Deep Clustering

Motivation

PCA is a classical linear method for dimensionality reduction. For high-dimensional data with complicated nonlinear structure (images, text, speech), it is often beneficial to first learn a nonlinear feature representation before clustering.

Deep clustering combines deep representation learning with clustering. The main idea is to learn a representation in which clustering becomes easier.

Two-Stage Deep Clustering

Approach:

  1. Learn a low-dimensional representation with an autoencoder:

    z(n)=fθ(x(n))z^{(n)} = f_\theta(x^{(n)})
  2. Apply a classical clustering algorithm (e.g., K-means) to the learned features.

The K-means objective in the embedded space:

min⁡{rk(n)},{μk}∑n=1N∑k=1Krk(n)∥z(n)−μk∥2\min_{\{r^{(n)}_k\}, \{\mu_k\}} \sum_{n=1}^N \sum_{k=1}^K r^{(n)}_k \|z^{(n)} - \mu_k\|^2

subject to rk(n)∈{0,1}r^{(n)}_k \in \{0, 1\} and ∑krk(n)=1\sum_k r^{(n)}_k = 1.

Advantages:

Limitation: The learned representation is not directly optimized for clustering.

Joint Optimization: Deep Embedded Clustering (DEC)

DEC learns the feature extractor and cluster assignments simultaneously.

Soft assignment. DEC first maps each sample into a latent feature z(n)=fθ(x(n))z^{(n)} = f_\theta(x^{(n)}) and maintains KK cluster centers {μ1,…,μK}\{\mu_1, \dots, \mu_K\}. The soft assignment of sample nn to cluster kk is:

qk(n)=(1+∥z(n)−μk∥2/α)−α+12∑j=1K(1+∥z(n)−μj∥2/α)−α+12q^{(n)}_k = \frac{\left(1 + \|z^{(n)} - \mu_k\|^2/\alpha\right)^{-\frac{\alpha+1}{2}}}{\sum_{j=1}^K \left(1 + \|z^{(n)} - \mu_j\|^2/\alpha\right)^{-\frac{\alpha+1}{2}}}

where α>0\alpha > 0 is a parameter (often set to 1). This is a Student’s t-distribution kernel measuring similarity.

Target distribution. DEC does not directly optimize qk(n)q^{(n)}_k. Instead it constructs an auxiliary target distribution pk(n)p^{(n)}_k from qk(n)q^{(n)}_k:

pk(n)=(qk(n))2/fk∑j=1K(qj(n))2/fj,where fk=∑n=1Nqk(n)p^{(n)}_k = \frac{(q^{(n)}_k)^2 / f_k}{\sum_{j=1}^K (q^{(n)}_j)^2 / f_j}, \quad \text{where } f_k = \sum_{n=1}^N q^{(n)}_k

This target distribution:

Objective. DEC minimizes the KL divergence between the target distribution PP and the soft assignments QQ:

LDEC=KL(P∥Q)=∑n=1N∑k=1Kpk(n)log⁡pk(n)qk(n)\mathcal{L}_{\text{DEC}} = \text{KL}(P \parallel Q) = \sum_{n=1}^N \sum_{k=1}^K p^{(n)}_k \log \frac{p^{(n)}_k}{q^{(n)}_k}

Minimizing this with respect to θ\theta and {μk}\{\mu_k\} refines both the representation and the clustering structure simultaneously.

DEC Algorithm:

  1. Initialization: Train an autoencoder to obtain an initial encoder fθf_\theta. Compute latent features and initialize cluster centers by K-means.

  2. Iterative refinement: Repeat until convergence:

    • Compute soft assignments qk(n)q^{(n)}_k

    • Construct target distribution pk(n)p^{(n)}_k

    • Update θ\theta and {μk}\{\mu_k\} by minimizing KL(P∥Q)\text{KL}(P \parallel Q)

  3. Final clustering: Assign each sample to arg⁡max⁡kqk(n)\arg\max_k q^{(n)}_k


2. Contrastive Learning

Motivation

Supervised learning requires massive amounts of labeled data, which is expensive to collect. Unlabeled data is often vast. The goal of self-supervised learning is to learn a general-purpose feature extractor fθ(x)f_\theta(x) from unlabeled data without human annotations. These learned features can then be used for downstream tasks with only a small number of labeled examples.

Key Idea

The main principle of contrastive learning is:

Similar examples should have similar representations.

To achieve this:

Constructing Positive and Negative Pairs

In unsupervised contrastive learning, we do not have labels. Instead, we construct positive pairs from the same image using data augmentation:

x and x~x \text{ and } \tilde{x}

where x~\tilde{x} is an augmented view of xx. All other images in the batch are treated as negative examples.

Common augmentations include random cropping, color distortion, Gaussian blur, and horizontal flipping.

Triplet Loss

A basic contrastive loss is the triplet loss. For each anchor xx, we have:

Ltriplet=max⁡(0,∥fθ(x)−fθ(x+)∥2−∥fθ(x)−fθ(x−)∥2+ϵ)\mathcal{L}_{\text{triplet}} = \max\left(0, \|f_\theta(x) - f_\theta(x^+)\|^2 - \|f_\theta(x) - f_\theta(x^-)\|^2 + \epsilon\right)

where ϵ>0\epsilon > 0 is a margin parameter. The loss encourages the anchor to be closer to the positive than to the negative by at least ϵ\epsilon.

Compared with an autoencoder, contrastive learning wants similar/dissimilar data to have similar/dissimilar representations, rather than pixel-wise reconstruction.

N-way Contrastive Loss

Triplet loss considers one positive and one negative at a time. A more general approach treats contrastive learning as an NN-way classification problem: for an anchor representation z=fθ(x)z = f_\theta(x), identify its positive pair z+z^+ among many negatives {zi}\{z_i\}.

LN-way(θ)=−log⁡exp⁡(−d(z,z+))∑iexp⁡(−d(z,zi))+exp⁡(−d(z,z+))\mathcal{L}_{N\text{-way}}(\theta) = -\log \frac{\exp(-d(z, z^+))}{\sum_i \exp(-d(z, z_i)) + \exp(-d(z, z^+))}

where d(⋅,⋅)d(\cdot, \cdot) is a distance function (e.g., cosine distance).

SimCLR Algorithm

SimCLR is a simple framework for contrastive learning of visual representations.

Algorithm:

  1. Sample a minibatch of NN examples x1,…,xNx_1, \dots, x_N

  2. Augment each example twice to get x~1,…,x~N,x~N+1,…,x~2N\tilde{x}_1, \dots, \tilde{x}_N, \tilde{x}_{N+1}, \dots, \tilde{x}_{2N}

  3. Embed examples with fθf_\theta to get z~1,…,z~2N\tilde{z}_1, \dots, \tilde{z}_{2N}

  4. Compute pairwise cosine distances:

    d(zi,zj)=−zi⊤zj∥zi∥∥zj∥d(z_i, z_j) = -\frac{z_i^\top z_j}{\|z_i\| \|z_j\|}
  5. Update θ\theta with respect to the NN-way loss:

    LN-way(θ)=∑i=1N−log⁡exp⁡(−d(z~i,z~N+i))∑j≠iexp⁡(−d(z~i,z~j))\mathcal{L}_{N\text{-way}}(\theta) = \sum_{i=1}^N -\log \frac{\exp(-d(\tilde{z}_i, \tilde{z}_{N+i}))}{\sum_{j \neq i} \exp(-d(\tilde{z}_i, \tilde{z}_j))}

The key idea is to compare augmentations of the data itself, treating the two augmented views of the same image as a positive pair and all other pairs as negatives.