Imagine a folder holding a million photos that nobody has labeled. Nobody has written "dog" or "beach" next to any of them. In the supervised learning of chapter 02, every training example came with an answer to learn from, called a label, and the model's job was to predict it. Here there are no answers at all, so it's fair to ask whether a model can learn anything useful from the folder.
It can, in two different ways. The first is to look for structure that's already in the data: photos that look alike can be grouped together, and the thousands of numbers that describe each photo can often be summarized with far fewer. That's unsupervised learning, and there's no target anywhere in it. The second way is to invent a question out of the data itself, one whose answer you already know. Cover a patch of a photo and ask the model to fill it in, and the answer is the patch you covered. That's self-supervised learning, one strategy inside the larger world of learning from unlabeled data.
The second way matters a lot, because the invented question gives you a real loss and a real gradient (a loss is a number that says how wrong the model currently is, and training nudges the model to shrink it) without a person ever writing a label. Almost every foundation model trained since 2018 begins with a self-supervised phase like this.
This chapter follows our folder of photos through both ideas. We'll start with the older methods that look for structure, move through autoencoders, which are a bridge between the two ideas, and then build up to the methods that invent their own tasks. After that we'll see the loss they train with, why they won, and what goes wrong when there's no label to grade the result against.
01Two ways to learn without labels
Finding structure that's already there
Take the unsupervised idea first. Suppose we run a method called k-means on a set of customer purchase records, or on our folder of photos. It splits the records into groups, and that's the whole output. There's no correct grouping that it checks its answer against, and no loss that compares a prediction with a ground truth, because nobody ever supplied one. What it hands back is structure that was sitting in the data all along, such as clusters, a description of the data with fewer numbers, or an estimate of where the data is dense. A method like that is what we call unsupervised.
To see how little it needs, here are six numbers with no labels at all. Looking at them, you can probably see two clumps, one near 1.5 and one near 9. The code below finds the same two clumps without being told. It starts with a deliberately poor guess for where the two group centers are, [1.0, 2.0], which puts both of them in the small clump. Each pass then does two things. It assigns every number to its nearest center (that's the argmin line), and then it moves each center to the mean of the numbers assigned to it.
Try it Find two groups in six unlabeled numbers with k-means
import numpy as np
x = np.array([1.0, 1.5, 2.0, 8.0, 9.0, 9.5]) # no labels, just numbers
centers = np.array([1.0, 2.0]) # a poor starting guess
for step in range(4):
dist = np.abs(x[:, None] - centers[None, :]) # distances
group = dist.argmin(axis=1) # nearest center wins
centers = np.array([x[group == k].mean() for k in (0, 1)])
print(f"step {step}: groups {group} centers {np.round(centers, 2)}")
2.0 into the large group and puts the centers at 1.25 and 7.12. The second moves it back, and the centers land at 1.5 and 8.83. Nothing changes after that. Nobody told the algorithm which numbers belong together; it found the two clumps from distances alone.Look at what the first pass did. The poor starting centers sit at 1 and 2, so the number 2.0 is closer to the second center than to the first, and it joined the three large numbers. That group's mean is pulled down to 7.12, while the first group's mean is 1.25. Now 2.0 is closer to 1.25 than to 7.12, so on the second pass it moves back, and the centers land at 1.5 and 8.83. After that, nothing moves. Our algorithm never saw a label. It found the two clumps from distances alone, and that's the kind of result the unsupervised family is after.
Inventing the label
The second idea sounds like a cheat the first time you hear it. Self-supervised learning still uses zero human-provided labels, but it does have a target. Here the model makes that target itself, out of the data, and then trains with an ordinary supervised loss against it.
Here's where the label comes from when nobody wrote one. Take a sentence and hide one word, then ask a model to predict it from the rest. Its label is the word you already had and chose to hide. Or take one photo from our folder, produce two randomly cropped, colour-jittered versions of it, and ask a model to recognise that both came from the same photo. Its label is the fact that you know which crops came from which photo, because you made them. Both of these are most likely familiar territory: ordinary gradient descent on an ordinary loss, such as cross-entropy (the usual loss for picking the right answer out of many options). What's unusual is only where the label came from. A made-up task like this, whose only purpose is to force the model to learn something useful, is called a pretext task.
Which one should I reach for?
It depends on what you want out of the folder. If you want to explore it or compress it, you're asking an unsupervised question, so PCA or clustering is the place to start. If you want a representation (a vector for each photo) that will transfer to a task you haven't seen yet, you're asking a self-supervised question, because contrastive and masked-prediction pretext tasks are the objectives that have been shown to produce features that generalize, as far as anyone can tell from benchmarks so far. Mixing the two up is probably the most common stumble in this area.
There's a single idea behind everything we'll cover, and chapter 01 approached it from the information-theory side. A good learned representation, roughly, keeps high mutual information with whatever you care about downstream, meaning it keeps what's informative about it, and throws away nuisance variation such as sensor noise, the exact pixel values of a background, or the particular synonym a sentence happened to use. Clustering, PCA, contrastive learning and masked prediction all chase that same goal, and none of them is ever told what "what you care about" is.
A short history explains why the order of this chapter looks the way it does. For decades, "no labels" meant k-means, PCA and their relatives. They were useful for exploration and compression, but they relied on hand-designed features and couldn't produce representations that a downstream network could reuse. Then BERT (2018) masked words and predicted them, and SimCLR and MoCo (2020) trained an encoder to place two augmentations of the same image close together, so both turned unlabeled data into a training signal. By the mid-2020s that had become the standard pipeline, with GPT-style next-token prediction, BERT-style masked prediction and CLIP's image-text contrast doing the pretraining, and labeled fine-tuning as a small last step. So we'll start with the older family and work our way toward that.
02From clusters to learned representations
"Unsupervised and self-supervised" covers a wide family of methods that share no single architecture. It helps to group them by what they do. Clustering partitions the data directly. Dimensionality reduction finds a lower-dimensional description of it. Autoencoders compress the data and then reconstruct it. And self-supervised methods invent a prediction task and solve it with an encoder. That last group is where most representation-learning research of the 2020s lives, so it gets the most space here, and the diagram. We'll go in order, starting with the simplest.
Clustering with k-means and its relatives
We just watched k-means work on six numbers. In general it starts by picking k initial cluster centers, called centroids, and alternates two steps until nothing changes: assign every point to its nearest centroid, then recompute each centroid as the mean of the points now assigned to it. Each round can only lower the total distance from points to their centers, or leave it flat, so the process always converges. But it doesn't necessarily converge to the best possible partition. A bad random start can trap it in a poor local optimum, a solution that no small change improves even though a better one exists elsewhere. That's why, in practice, you would probably run k-means from several starting points and keep the best result, or you use k-means++, a smarter initialization that spreads the starting centroids out.
A natural complaint about k-means is that a centroid is a computed average, and one extreme point can drag an average a long way. k-medoids solves the same problem but forces each cluster's center to be an actual data point, called a medoid. An extreme point can't become the medoid unless it's central, so the method is far less sensitive to outliers.
Another complaint is that you have to pick k before you start. Hierarchical (agglomerative) clustering avoids that. It begins with every point as its own cluster and repeatedly merges the two closest clusters until everything is one cluster. What "closest" means is set by a linkage rule: the closest pair of points, the farthest pair, or the average distance across all pairs. You end up with a dendrogram, a tree that records every merge, and you choose k afterwards by cutting the tree at whatever height gives you the number of groups you want. What it costs you is scale, because computing and updating all the pairwise distances grows with the square of the number of points in the naive version.
A third complaint is that k-means only finds roughly round blobs. DBSCAN drops the idea of a center altogether and defines clusters by density. A point is a core point if at least minPts other points sit within a small radius eps of it, and a cluster grows by chaining together core points that are close to each other. Points that belong to no dense region are labeled noise, so they aren't forced into a cluster they don't belong in. That gets you two things k-means structurally can't do, which are clusters of arbitrary shape and automatic outlier detection, with no k to specify. The catch is that eps and minPts aren't always easy to choose, and DBSCAN struggles badly when different real clusters in the same dataset have very different densities, because one eps can't be right for both a tight cluster and a sparse one.
Gaussian mixture models and EM
All of these give each point a hard answer: it belongs to exactly this cluster. What if a point sits between two groups? A Gaussian Mixture Model (GMM) is what you get when you make k-means probabilistic. Instead of assuming every cluster is a hard, spherical blob, it assumes the data was generated by a mixture of k Gaussians (bell-shaped distributions). Each has its own mean, its own covariance (so clusters can be elliptical as well as round) and its own mixing weight. Every point then gets a soft, probabilistic membership across all k of them, where k-means gives a hard assignment to one.
Fitting a GMM is trickier than k-means because there's a circularity in it. You don't know which Gaussian generated which point, and you don't know the Gaussians' parameters, and each of those depends on the other, so there's no closed-form formula. Expectation-Maximization (EM) breaks the circle the same way k-means does, by alternating, but with soft numbers in place of hard ones.
- E-step. It fixes the current parameters and, for every point, computes the responsibility: the posterior probability that the point came from each of the k Gaussians, given where the Gaussians currently sit.
- M-step. It fixes those responsibilities and re-fits each Gaussian's mean, covariance and mixing weight, using the responsibilities as per-point soft weights in place of hard 0/1 membership.
Repeat the two steps and the data's likelihood under the model provably increases every round until it converges. As with k-means, that only guarantees a local optimum of the likelihood, so you probably want to run it from several starting points (often seeded with a quick k-means pass) and keep the best result by final log-likelihood. Still, the two methods are closely related. Force every Gaussian to be spherical, tiny and equally weighted, and the soft responsibilities collapse into hard 0/1 assignments while the M-step's weighted mean becomes an ordinary mean. That's the same alternating logic, so k-means is a limit of EM.
Dimensionality reduction
Clustering describes the folder by groups of photos. A different question is whether each photo, described by thousands of numbers, can be described with far fewer. PCA, ICA and NMF all answer that, and they differ in what counts as a good small description. The eigenvector and SVD machinery behind PCA is covered in chapter 01, and the Essence of Linear Algebra series is the best visual build-up of it if eigenvectors still feel abstract.
- PCA finds the orthogonal directions of maximum variance and projects onto the top few. It decorrelates the data, and it's the optimal linear projection if the only goal is preserving variance, and that's the same as minimizing squared reconstruction error.
- ICA (Independent Component Analysis) asks for something strictly stronger: statistically independent components, a stronger condition than uncorrelated.
- NMF (Non-negative Matrix Factorization) adds a different constraint. Every factor and every coefficient has to be non-negative.
The ICA distinction matters because decorrelated is a much weaker condition than independent. Two variables can have zero correlation and still be tightly, nonlinearly dependent. ICA needs the underlying sources to be non-Gaussian to be identifiable at all. It's the tool behind the classic cocktail-party problem: given several microphone recordings, each a different mixture of the same underlying voices, recover the original independent voice signals.
NMF's constraint sounds small but has a big effect. Because components can only add and never cancel, NMF tends to discover parts-based, additive representations, such as facial features that combine into a face, or topics that combine into a document. Those are often far more interpretable than PCA's components, which mix positive and negative contributions and are hard to read one at a time.
Spectral methods start from a different place. You build a graph in which nearby data points are connected, weighted by similarity, compute that graph's Laplacian (a matrix that describes how the graph is connected), and use its eigenvectors as a new coordinate system for the data. Spectral clustering then runs ordinary k-means in that new coordinate system instead of in raw feature space. This lets it separate clusters that are non-convex or intertwined, such as two concentric rings, which k-means can't do directly because it only draws straight-line, Euclidean boundaries.
Every method so far is a fixed recipe. PCA is linear, and clustering uses a fixed notion of distance. To get something that can learn nonlinear descriptions, we can bring in a neural network.
Autoencoders, denoising autoencoders and VAEs
A plain autoencoder is a pair of networks trained end to end to reconstruct their own input through a bottleneck. The first, the encoder, compresses an input x down to a low-dimensional code z. The second, the decoder, turns z back into something close to x, and training minimizes the reconstruction error. It's unsupervised in the sense we defined above, since there are no labels and x is predicting x. The code z lives in what's called the latent space, "latent" because it holds hidden factors that nobody observed directly.
Nothing in that training asks the latent space to be well behaved. It only has to take whatever shape makes reconstruction loss small. So a plain autoencoder isn't a generative model: sample a random point in its latent space and, unless it happens to land near where a real training example landed, the decoder produces garbage. Nothing during training ever asked the latent space to be smooth or densely packed.
Before fixing that, there's a second problem to deal with. An autoencoder with enough capacity can get a low error by learning something close to the identity function, copying its input through. A denoising autoencoder makes one small, effective change. It corrupts the input before it reaches the encoder (by masking patches or adding noise) but still asks the decoder to reconstruct the clean original. Now the network can't get away with copying. It has to learn the real structure and correlations in the data, because the corrupted parts can only be inferred from everything else. Keep that idea in mind, because it comes back later in this chapter as BERT-style masked language modeling and MAE-style masked image modeling, which use the same mechanism in a different modality.
The latent-space problem needs a different fix. A Variational Autoencoder (VAE) changes what the encoder outputs. Instead of encoding x to a single point z, the encoder outputs the parameters of a distribution over z, a mean and a variance, and a sample is drawn from that distribution. A KL-divergence term (a measure of how different two distributions are) in the loss then pulls every input's latent distribution toward a fixed, simple prior, almost always a standard Gaussian. That regularization term forces the latent space to be smooth and densely packed, with no gaps, so sampling from the prior, and not only encoding a real example, reliably decodes into something plausible. The full latent-variable setup and the derivation behind it get their complete treatment in chapter 12, and the equation itself is recapped in section 03 below.
So far the training signal has been "reconstruct the input". The self-supervised methods we turn to next ask for something smarter.
Self-supervised learning: inventing a task, then solving it with an encoder
Everything from here on is self-supervised in the specific sense from section 01: no human label, but a real, invented target. There are two broad families, and the distinction between them (joint-embedding versus generative objectives) matters more than the list of model names attached to each.
Joint-embedding methods are the ones the diagram below covers, and contrastive learning is the clearest version of the idea. In our photo folder it runs in four moves.
- Take one unlabeled image. Apply two different random augmentations, such as a random crop, a colour jitter or a horizontal flip. You get two different-looking images that are still, semantically, the same underlying thing.
- Run both through the same encoder. Literally the same weights, applied twice.
- Those two embeddings are a positive pair. The objective pulls them together.
- Every other image in the same batch gives embeddings that are negatives. The objective pushes those apart.
An embedding here is just the vector the encoder outputs for an image. No human ever labeled anything. The label is "these two crops came from the same source image and these others didn't," a fact you know for free because you generated the crops yourself. In the diagram, follow one image on the left through its two augmentations and the shared encoder, and watch where the two resulting embeddings end up relative to the others.
This setup, with two or more branches that share weights and are trained so that distance in embedding space means something, is a Siamese network. It predates SimCLR by decades, and it's the same architecture behind face-verification systems (are these two photos the same person?) and one-shot signature or fingerprint matching. Metric learning is the general name for training an embedding space where distance is meaningful this way. It usually uses a contrastive loss (pull positives together, push negatives apart by at least some margin) or a triplet loss (take an anchor, a positive and a negative, and push the anchor-negative distance to exceed the anchor-positive distance by a margin).
Three joint-embedding methods follow, in order of how much machinery they need. SimCLR (2020) is close to the diagram in its purest form: large batches supply the negatives directly, and in the original paper's own ablations, augmentation strategy turned out to be a bigger lever than architecture. Large batches are expensive, so MoCo (2020) gets a large number of negatives without needing enormous batches. It keeps a running queue of embeddings from recent batches, computed by a slowly updated momentum encoder, as a large, cheap pool of negatives. And BYOL and SimSiam, both 2020, asked a surprising question: do you need negative pairs at all? Both train with only positive pairs and still avoid collapsing to a trivial solution. How that works, and why it's a nontrivial claim, is the subject of the section on representation collapse below.
The other broad family generates its supervision by masking and then reconstructing, in input space and not in embedding space. BERT-style masked language modeling (2018) hides about 15% of the tokens in a sentence and trains the model to predict them from the surrounding context. That prediction is bidirectional, since the model can look both left and right of the masked token, which a left-to-right autoregressive model can't do. MAE-style masked image modeling (2021) is the visual analogue. It masks out a large majority of an image's patches (MAE's own default is a striking 75%) and trains a decoder to reconstruct the missing pixels from the small number of visible patches an encoder was allowed to see. That very high masking ratio is deliberate. Images have far more spatial redundancy than text, so a much more aggressive mask is needed to make the reconstruction hard enough to force useful learning. Otherwise the model could solve it by copying nearby pixels.
Both families are self-supervised, and they differ in where the prediction target lives. Joint-embedding methods (SimCLR, MoCo, BYOL, SimSiam) never reconstruct anything in input space, and the entire objective lives in embedding space, comparing representations to each other. Generative or reconstruction methods (denoising autoencoders, BERT, MAE) predict directly in the original input space, with actual tokens and actual pixels as the target.
Why would anyone choose the harder-looking joint-embedding route?
Because the tradeoff cuts both ways. Reconstruction objectives are simple and stable to train, since they're ordinary regression or classification against a well-defined target, and a trivial constant-output encoder fails at reconstructing real detail, so collapse isn't a live risk. But they can spend a lot of model capacity getting low-level detail right that may not matter downstream. Take the exact texture of grass in the background doesn't matter for classifying "dog." Joint-embedding objectives sidestep that waste, since they only ever have to make the right high-level things end up close together. But that freedom is exactly what opens the door to a failure mode we'll meet in section 05, representation collapse. First, though, we need the loss that turns the pull and push in the diagram into a number.
03The equations
InfoNCE
InfoNCE turns the pull/push picture from the diagram into one loss function. It was introduced for representation learning by van den Oord et al. (2018), and it's the loss SimCLR uses directly, where it's sometimes called NT-Xent (normalized temperature-scaled cross-entropy). It's a softmax over the batch in which the right answer is the positive pair. For a positive pair (i, j) drawn from a batch of 2N augmented examples, meaning N images with two augmentations each:
- z_i, z_j the two embeddings of the positive pair, from the same source image under two different augmentations, both L2-normalized so their dot product is a cosine similarity.
- sim(·,·) cosine similarity between two embeddings, ranging from -1 to 1, which measures how aligned the two vectors are.
- τ temperature, a scalar that rescales similarities before the softmax. Small τ sharpens the distribution so the loss reacts much more strongly to hard, nearby negatives, and large τ flattens it so every negative contributes more evenly. The project in section 07 has you sweep τ and watch the embedding clusters tighten or smear.
- numerator the positive pair's similarity, exponentiated. It's the one term gradient descent is trying to make as large as possible relative to everything else.
- denominator the sum over every other embedding in the batch except i itself. Normalizing against the whole batch is what pushes every negative's similarity down, since increasing the numerator's share of that sum requires decreasing everyone else's relative share.
The ELBO, recapped
The other loss we met was the VAE's. It's built from the evidence lower bound, or ELBO, the standard variational-inference objective from Kingma & Welling (2013). It makes an otherwise-intractable log p(x) practical to train by gradient descent. The full derivation, and the reparameterization trick that makes sampling z differentiable, live in chapter 12. Here's the equation and what each term does:
- q_φ(z|x) the encoder's output distribution over latents given the input, typically a Gaussian whose mean and variance are predicted by the encoder network.
- E[log p_θ(x|z)] expected log-likelihood of reconstructing x from a sampled z. In practice, this is the reconstruction loss.
- p(z) the fixed prior being regularized toward, almost always a standard Gaussian.
- D_KL(·‖·) the penalty for how far the encoder's per-input distribution strays from that prior. A plain autoencoder doesn't have this term, and it's what makes the latent space smooth and samplable.
With both losses in hand, we can ask why this whole approach displaced the obvious alternative.
04Why self-supervised pretraining won
The obvious alternative to inventing a pretext task is to label more data. That doesn't scale the way self-supervision does, mostly for economic reasons and not algorithmic ones. Raw text, images, audio and video are abundant, and the internet produces them continuously at a cost close to zero per example. Labels aren't cheap, since every one requires a human decision, at a rate and cost that grows with dataset size. Doubling an unlabeled dataset is usually a storage and compute problem, while doubling a labeled dataset is a headcount and time problem, and headcount doesn't scale the way GPUs do.
Self-supervised pretraining sidesteps that bottleneck. Pretraining probably does its most useful work early, extracting structure from the raw, abundant data (what a well-formed sentence looks like, what a real natural image looks like, what tends to co-occur with what) before a single labeled example is involved. Fine-tuning on labels then only has to teach the specific task on top of representations that already understand the domain, so it typically needs orders of magnitude fewer labeled examples than training the same architecture from a random initialization would. It also means label-quality issues matter less than they used to. Inconsistent annotators, ambiguous categories and ordinary noise in any large human-labeled dataset all have less effect, because the representation doing most of the heavy lifting was never trained on those noisy labels in the first place.
None of this makes labels obsolete. You still need them to point the model at the specific task you want, and to check whether pretraining worked at all. What changed is the ratio. A small, carefully curated labeled set on top of a good self-supervised backbone now reliably beats a much larger labeled set feeding a model trained from scratch, and that shift is the practical reason this happened as fast as it did. But a method that never sees a label has a cost of its own: it's much harder to tell when it has gone wrong.
05Failure modes, evaluation and what breaks
This is where "no labels" becomes a problem. Without a ground-truth answer to check against, most of what can silently go wrong here doesn't throw an error. It quietly produces a plausible-looking, wrong result. Picking a clustering algorithm, in particular, is mostly a matter of picking which assumption you're comfortable making. The table lines up the four we met in section 02.
| k-means | GMM (EM) | Hierarchical | DBSCAN | |
|---|---|---|---|---|
| Cluster shape assumed | Roughly spherical, similar-sized | Gaussian — can be elliptical | None explicit — driven by linkage rule | Arbitrary — defined by density |
| Need to pick k upfront | Yes, fixed in advance | Yes, number of components | No — cut the dendrogram after the fact | No, but needs eps/minPts |
| Handles outliers | Poorly — every point forced into a cluster | Soft assignment helps some, still assumes Gaussian shape | Sensitive — outliers can distort early merges | Explicitly labels outliers as noise |
| Rough cost | Fast, scales well | Heavier — fits full covariances | Quadratic in n, naively — doesn't scale to huge n | Near-linear with a spatial index; struggles with mixed densities |
Representation collapse
The worst failure belongs to the joint-embedding methods. Suppose the encoder ignores its input and outputs the exact same constant vector for every image. The positive pairs are then trivially identical, which gives perfect similarity and a minimized loss. A naive loss function can't tell that apart from a good representation, so it's a shortcut that gradient descent will probably take unless something forbids it. That failure is called representation collapse, and avoiding it is the central design problem in this whole family of methods.
Contrastive methods like SimCLR and MoCo avoid it directly. Negative pairs are explicitly pushed apart, so the collapsed solution scores as badly as possible on every negative term in the loss. Collapsing destroys the part of the loss that pushes negatives apart just as much as it satisfies the part that pulls positives together, so there's no net incentive to take the shortcut.
BYOL and SimSiam remove negative pairs entirely and still, empirically, don't collapse, and that's surprising. Both rely on an asymmetry between the two branches instead.
- BYOL. One branch, the online network, trains normally by gradient descent and ends with an extra predictor layer that tries to predict the other branch's output. The other branch, the target network, never receives a gradient at all. Its weights are only ever updated as a slow exponential moving average (a running average that weights recent values most) of the online network's weights, with gradients explicitly stopped from flowing into it.
- SimSiam. It simplifies further, dropping the momentum-averaged target network altogether and showing that a predictor plus stop-gradient alone is enough.
The stop-gradient, which blocks gradients from flowing into one branch, is the load-bearing piece in both. Without it, both branches could collapse to the same constant together and call it solved. With it, one branch is deliberately prevented from copying or trivially chasing the other. Why stop-gradient plus a predictor reliably prevents collapse is still an open research question. The SimSiam paper offers an EM-like interpretation, but "guaranteed by a clean proof" would be an overclaim. What's solid is the empirical result, replicated many times over, that this specific asymmetric architecture reliably avoids collapse without ever needing a negative pair.
Reading t-SNE and UMAP plots
A related trap shows up when you try to look at what an encoder learned. To see high-dimensional embeddings you project them down to two dimensions with an algorithm such as t-SNE or UMAP, and it's tempting to read the plot literally: clusters far apart in the plot are far apart in the real data, and clusters close together are similar. That's only reliable for which points fall into which local neighborhood, because both algorithms are designed to preserve local structure and make no promise about global distances. Cluster size, the distance between two separate clusters, and even the density of points within a cluster can all be artifacts of the algorithm's hyperparameters (perplexity for t-SNE, n_neighbors for UMAP) and not anything real in the underlying data. Treat these plots as evidence that points are each other's neighbors, and never as evidence for exactly how different two well-separated blobs are. van der Maaten & Hinton flag this limitation directly in the original t-SNE paper, and citing it is a strong answer if it comes up in an interview.
Curse of dimensionality, for distance-based methods
k-means, DBSCAN and anything else built on Euclidean distance share a specific, well-documented failure as dimensionality grows. In high dimensions, the ratio between the distance to the nearest point and the distance to the farthest point tends toward 1. Everything ends up roughly equidistant from everything else, and "nearest neighbor" stops being a meaningful concept. This is why clustering raw pixel-space or raw high-dimensional feature vectors directly is usually a bad idea. A standard fix is to reduce dimensionality first, with PCA or with a learned embedding from an autoencoder or a self-supervised encoder, and then cluster in that lower-dimensional, more meaningful space.
Why unsupervised evaluation is hard
How do you know a clustering or an embedding is any good?
Supervised learning has a built-in success metric: does the prediction match the label? Unsupervised and self-supervised methods have no such thing by construction. There's no ground truth to check a clustering or a learned embedding against, which makes "is this good?" a harder question here than almost anywhere else in ML. In practice you fall back on proxy metrics, and each has specific limits.
- Silhouette score measures, for each point, how much closer it is to its own cluster than to the next-nearest one, averaged over all points and ranging from -1 to 1. It's useful for comparing different values of k on the same algorithm. But it still assumes roughly convex, well-separated clusters, so it can penalize a correct DBSCAN-style non-convex clustering for the wrong reasons.
- Linear probe is the standard proxy for self-supervised representations. Freeze the pretrained encoder entirely, train only a small linear classifier on top using a labeled dataset, and report that accuracy.
The linear probe is the evaluation protocol SimCLR, and nearly every contrastive-learning paper since, uses to report results. There's no other honest way to say "these representations are good" without eventually testing them against a real task.
06Summary
- Unsupervised learning has no target. Clustering, dimensionality reduction and density estimation expose structure that's already in the data, and nothing is checked against a ground truth.
- Self-supervised learning invents its target from the data. Hide a word, or make two crops of one photo, and the answer is something you already know, so ordinary gradient descent applies with no human labels.
- k-means alternates assigning and averaging. It always converges but only to a local optimum, so you restart it from several starting points or use k-means++. k-medoids, hierarchical clustering and DBSCAN each remove one of its assumptions.
- A Gaussian mixture model is k-means made soft. EM alternates computing responsibilities and re-fitting the Gaussians, and k-means is the hard-assignment limit of it.
- PCA, ICA and NMF differ in what counts as a good small description. PCA keeps variance, ICA asks for independence, and NMF forces non-negative parts.
- Autoencoders learn by reconstructing. Denoising versions have to learn real structure instead of copying, and a VAE adds a KL term so that the latent space can be sampled from.
- Contrastive learning pulls positive pairs together and pushes negatives apart. InfoNCE does it with one softmax over the batch, and the temperature sets how hard the loss focuses on near negatives.
- Joint-embedding and reconstruction methods put the target in different places. Joint-embedding methods compare embeddings, and masked methods like BERT and MAE predict tokens or pixels.
- Representation collapse is the central failure. Negatives prevent it in SimCLR and MoCo, and an asymmetry with a stop-gradient prevents it in BYOL and SimSiam, though why that works is still an open question.
- Pretraining won on economics. Unlabeled data is nearly free, so a small labeled set on a good self-supervised backbone beats a large labeled set on a model trained from scratch.
- Evaluation needs proxies. Use silhouette score for clusters and a linear probe for representations, and read t-SNE and UMAP plots only for local neighborhoods.
07Build this
Representation collapse is the failure this whole family of methods is designed around. Reading about it is one thing. Watching every embedding in your batch slide onto the same point is another.
Train a SimCLR-style encoder on a toy image set where you already know what the right answer looks like. Then delete the negatives and watch the representation die. The embedding is two-dimensional, so it plots directly. It avoids t-SNE on purpose, since section 05 warns that distances in a t-SNE plot are not distances in your data, and this project needs a picture you can trust.
- Generate the data: a few hundred 32×32 images, each one solid shape on a black background, in one of four colors. Color is the only semantic content. Keep the color labels aside, for plotting and nothing else.
- Define two augmentations that preserve color: random resized crop and horizontal flip. Two views per image, one shared encoder, exactly the diagram in section 02.
- Encoder: three small conv layers down to a 2-D output, L2-normalized. Two dimensions means every embedding is plottable with no projection step in between.
- Write InfoNCE by hand from the equation in section 03. Cosine similarity matrix over the 2N views, mask the diagonal, divide by , cross-entropy against the index of the matching view. Train at , then again at .
- Plot the trained embeddings on the unit circle, colored by the labels you set aside. Log the standard deviation of the embeddings across each batch as training runs.
- Now break it by dropping the negatives entirely: replace the loss with the negative cosine similarity of the positive pair alone, and retrain from scratch.
Where this runs in production
This is a common real pipeline. You have a large pool of unlabeled images from your own domain (product photos, medical scans, satellite imagery, whatever is abundant) and only a small, expensive-to-produce labeled set for the task you care about, say defect classification. Three steps:
- Pretrain a vision encoder on the full unlabeled pool, using a contrastive objective (SimCLR/MoCo-style) or a masked-reconstruction objective (MAE-style). No labels are used yet.
- Attach a small task-specific head to that pretrained encoder and fine-tune on the small labeled set.
- Or, cheaper still, train only a linear probe on the frozen features.
In practice this consistently beats training the same architecture from a random initialization directly on the small labeled set alone. The encoder already learned general visual structure (edges, textures, object parts, shape) from the much larger unlabeled pool before ever seeing a single label. Fine-tuning only has to adapt that structure to the specific task, and does not have to learn vision from scratch on too little data.
08Interview questions
BeginnerWhat's the actual difference between unsupervised and self-supervised learning?
Both use unlabeled data, but only one produces a real training target. Unsupervised learning has no target at all — clustering and PCA just expose structure already in the data, with no loss comparing a prediction to a ground truth. Self-supervised learning manufactures a target directly from the data itself — mask a word and predict it, compare two augmented views of an image — and then trains with an ordinary supervised loss against that manufactured target.
BeginnerWalk me through the k-means algorithm.
Pick k initial centroids. Alternate two steps until nothing changes: assign every point to its nearest centroid, then recompute each centroid as the mean of the points now assigned to it. It's guaranteed to converge but only to a local optimum, so in practice you run it from several random initializations (or use k-means++ for a smarter start) and keep the best result.
IntermediateExplain what happens in the E-step and M-step of EM for a Gaussian mixture model.
The E-step fixes the current Gaussian parameters and computes, for every point, the responsibility — the posterior probability it came from each of the k Gaussians. The M-step fixes those responsibilities and re-fits each Gaussian's mean, covariance, and mixing weight, using the responsibilities as soft, per-point weights. Repeating this alternation increases the data's likelihood under the model monotonically, but only to a local optimum — k-means is the hard-assignment special case of exactly this same alternation.
IntermediateWhy does temperature matter in the InfoNCE loss?
Temperature rescales similarities before the softmax inside the loss. A small temperature sharpens the distribution, so the loss and gradient become dominated by whichever negative is currently closest to the positive — forcing the encoder to focus on separating confusable, hard negatives. A large temperature flattens the distribution, treating every negative roughly equally regardless of how close it actually is, which is a much weaker training signal.
IntermediateWhat's the practical difference between a plain autoencoder and a VAE?
A plain autoencoder's latent space has no guaranteed structure beyond whatever shape minimizes reconstruction loss, so sampling a random point from it and decoding usually produces garbage — it's not a generative model. A VAE's encoder outputs a distribution over the latent instead of a single point, and a KL-divergence term pulls that distribution toward a fixed prior (usually a standard Gaussian), which forces the latent space to be smooth and densely packed — that's what makes sampling from the prior actually generate plausible outputs. Full ELBO derivation is on 12.
DeepWhat is representation collapse, and how do BYOL/SimSiam avoid it without negative pairs?
Representation collapse is the trivial failure mode where the encoder outputs the same constant vector for every input, which vacuously satisfies "positive pairs are close" without learning anything useful. Contrastive methods prevent it with negative pairs — collapsing would also destroy the part of the loss pushing negatives apart. BYOL and SimSiam use no negatives at all; instead they rely on an asymmetry between two branches — a predictor network on one branch plus a stop-gradient (and, for BYOL, a momentum-averaged target network) on the other — that empirically prevents collapse. The exact theoretical reason this specific asymmetry works is still an active research question; what's well-established is the empirical result, replicated widely, that it reliably works.
DeepWhy is evaluating an unsupervised or self-supervised model harder than evaluating a supervised one, and what do you actually do about it?
Supervised evaluation just checks predictions against ground-truth labels. There's no equivalent ground truth for a clustering or a learned embedding, so you're reduced to proxy metrics. For clustering, something like silhouette score — but it assumes roughly convex clusters, so it can penalize a correct non-convex (DBSCAN-style) result. For self-supervised representations, the standard proxy is a linear probe: freeze the encoder, train only a small linear classifier on top using labeled data, and report that accuracy — this is the exact protocol SimCLR-style papers use to claim their representations are actually useful.
09Go deeper
●Now write it yourself
Reading the derivation and being able to produce it are different skills. These are Deep-ML problems that exercise what this page covers — each one is checked against real test cases, not multiple choice.
Matched to this page from Deep-ML's catalogue of 1,380 problems. More at deep-ml.com, and Where to practise covers the other platforms and what each one trains.