∇
How a vision model learns from millions of photographs that nobody has labeled. Pretext tasks, contrastive learning, the collapse problem and the methods that escape it, self-distillation from DINO to DINOv3, masked autoencoders, and how to measure what a representation knows, from first principles to the frontier, at interview depth.
Chapter 28 · Section 28.1 · 80 min

Learning without labels

Self-supervised learning trains an image encoder on photographs that nobody has labeled. An encoder is a network that turns an image into a vector of numbers, called its representation. Later tasks read that vector with a small classifier or a little extra training. With no labels, the training target has to come from the image itself. You hide or change part of a photo and ask the network about what you did, and to answer well it has to learn what is in the picture.

This matters because labels are the scarce resource in vision and pixels are not. Chapter 27 measured what that scarcity costs. A Vision Transformer trained on ImageNet's 1.28 million labeled photos alone loses to a convolutional network (§27.3). It pulls ahead with 300 million labeled images, and catches up on ImageNet alone only with a carefully tuned training recipe (§27.4). Self-supervision removes that dependence on labels.

The strongest image encoders trained without labels or text are self-supervised. DINOv3, a 7-billion-parameter encoder from 2025, learned from 1.7 billion unlabeled photos (Siméoni et al. 2025). On ImageNet its frozen features score within a point of the best encoders trained on images paired with captions. Frozen DINOv2 and DINOv3 features are now a default input for segmentation, depth, and robot perception. Masked-autoencoder pretraining, built in §28.5, starts models such as the image encoder of Segment Anything.

The chapter starts from a loss the book already has. The InfoNCE loss of §7.2, built there for text retrieval, scores each matching pair against many non-matching ones with a softmax. In this chapter the matching pair becomes two altered copies of one photo. This section explains why labels run out, how the first self-supervised methods worked, and how they cheated. Then it shows why the modern approach, which asks two such copies to get the same vector, learns the classes, with a theorem that says exactly when. It ends with the three tests that turn "a good representation" into a number, which every later section of the chapter uses.

Why labels are the bottleneck

Start with the sizes. ImageNet, in the version nearly every vision paper reports on (the 2012 challenge set, ILSVRC-2012), has 1.28 million training photographs. A person checked each one against its class, one of 1,000. That is the largest carefully labeled image collection most researchers ever use, and it is tiny next to what cameras produce.

The biggest supervised vision datasets gave up on careful labels long ago. JFT-300M, an internal Google dataset, has more than 375 million noisy labels on 300 million images (Sun et al. 2017). Mahajan et al. (2018) pretrained on up to 3.5 billion Instagram photos labeled only by their hashtags. Their best model reached 85.4 percent top-1 accuracy on ImageNet after fine-tuning. Top-1 accuracy is the fraction of test images whose highest-scoring class is the right one. Fine-tuning means training the whole pretrained network further on ImageNet's own labels.

Labels have three costs. They are expensive, since ImageNet's came from crowd workers who checked candidate images that search engines had returned. They are noisy at scale, since hashtags and automatic tags are mostly right but not always. And their vocabulary is fixed. A model trained on 1,000 classes has been told what matters. Anything those classes do not separate is, as far as the loss is concerned, noise to throw away.

That last cost has been measured. Kornblith et al. (2019) took 16 ImageNet classifiers and reused each one, frozen, on 12 other image datasets. For each dataset they kept the penultimate features, the vector just before the network's final classification layer, and trained only a new linear classifier on top. A linear classifier trained on frozen features is called a linear probe. Across the 16 networks, transfer accuracy tracked ImageNet accuracy closely, with a correlation of r=0.99r = 0.99. But label smoothing and dropout, two regularizers that slightly improve ImageNet accuracy, gave penultimate features that transferred much worse. A representation tuned to separate 1,000 classes as cleanly as possible discards what those classes do not need.

A label can say only a few bits

The second problem is about information rather than cost. Recall from §0.5 that the entropy H(Y)H(Y) of a random variable YY measures, in bits, how uncertain it is. The mutual information I(X;Y)=H(Y)−H(Y∣X)I(X;Y) = H(Y) - H(Y\mid X) is how many of those bits you learn about YY by seeing XX. It is symmetric, so it is also what YY tells you about XX. Let XX be an image and YY its label.

Proof. The conditional entropy of a discrete variable is never negative, so I(X;Y)≤H(Y)I(X;Y) \le H(Y). Among distributions on KK values the uniform one has the largest entropy, log⁡2K\log_2 K. §0.5 proves this from the fact that the KL divergence to the uniform distribution U\mathcal{U} is never negative, DKL(p ∥ U)=log⁡K−H(p)≥0D_{\mathrm{KL}}(p\,\Vert\,\mathcal{U}) = \log K - H(p) \ge 0. ∎

In plain words, however rich the image, its label can pass on no more than log⁡2K\log_2 K bits about it. For ImageNet that is log⁡21000≈9.97\log_2 1000 \approx 9.97, about 10 bits. The label "golden retriever" says nothing about the dog's pose, the light, the grass, or the frisbee.

The bound limits what a target can transmit. It is not a theory of learning. Supervised networks learn far richer features than 1.6 MB would suggest, because the images themselves shape what the network computes on its way to the label. The bound also says nothing about which bits are useful. A popular picture from a 2016 talk by Yann LeCun calls self-supervision the cake and labels the icing. It is a heuristic built on this calculation, not a theorem.

There are two ways around the bottleneck. One keeps labels but makes them free, by using the text that already sits next to images on the web, such as hashtags, captions, and alt-text. That is weak supervision, and it leads to the joint image and text spaces of CLIP and its successors. The other drops labels altogether and makes the images supervise themselves. That is self-supervised learning, and it is this chapter.

What this changes in practice is where the effort goes. When labels are the bottleneck, the work moves to choosing which unlabeled images to train on and which questions to ask of them. An interviewer will often open with "why not just label more data?" The answer names four limits. Labels cost money, they are noisy at scale, their vocabulary is fixed, and each one carries at most log⁡2K\log_2 K bits.

Predicting one part of an image from the rest

Language solved this problem first. word2vec learned a vector for each word by predicting the words around it (§1.5), building on the observation of §1.4 that the company a word keeps carries its meaning. BERT learned by filling in words that had been hidden from it (§7.1). Neither needed a human label, because the text supplied both the question and the answer. Images need a different notion of "part", and the history of self-supervised vision is largely a search for the right one.

Here is the simplest image version. Rotate a photo by a random multiple of 90 degrees, and ask a network which multiple it was. The rotated photo is the question. The answer, 0, 1, 2, or 3, is known because you chose it. Nobody wants a rotation detector. But a network that can tell which way a photo was turned has had to find skies, faces, and the bottoms of objects.

That recipe is called a pretext task. It makes a question and its answer out of an unlabeled image, trains a network to answer, and then keeps only the part of the network that turned the image into a vector. The task is a pretext because nobody wants its answers. What we want is the encoder that solving it forces the network to build.

In plain words, corrupt or cut the image in a way you choose, and ask the network to undo or explain what you did. Every method in this chapter fits the template. In rotation prediction cc is an angle, x~\tilde x the rotated image, and yy the angle's index. In masked modeling x~\tilde x is the visible part of the image and yy the hidden part. In the joint-embedding methods of the later sections, x~\tilde x is one random alteration of xx, and the target is the network's own representation of a second alteration.

The target yy is a label the image made for itself, so the bound above applies to it too. Here is how many bits each classic target carries.

More bits do not mean better features. The jigsaw label carries as much as an ImageNet label, yet it learns much weaker features than the contrastive methods of the next section. What matters is that the target is hard to predict without understanding the image, and, as we will see, that no low-level accident predicts it instead. The cell checks the arithmetic.

Python · cell 1

A tour of the classic pretext tasks

Between 2014 and 2018 researchers proposed dozens of pretext tasks. Five of them shaped the field, and each taught a lesson that the modern methods still rely on.

Context prediction

Context prediction asks where one patch of a photo sits relative to another. Doersch, Gupta and Efros (2015) took word2vec's idea, predicting a word from its neighbors, and applied it to patches. Sample a 96 by 96 patch, then one of its 8 neighbors on a 3 by 3 grid, and ask a network which neighbor it is. To know that a patch of ear sits above a patch of eye, the network has to recognize ears and eyes.

Both patches go through the same convolutional tower, so the two branches share their weights. The tower is AlexNet, the 2012 ImageNet network of §0.8, up to its first fully connected layer. Only the three fully connected layers above it, ending in the 8-way output, see both patches at once. That forces most of the reasoning to happen inside each patch's own representation, which is the part we keep.

The interesting part is what they had to forbid. If neighboring patches touch, the network can match edges and textures across the border and never look at the content. So they left a gap of 48 pixels, half a patch, between cells. Long straight lines could still give the answer away across the gap. So they also shifted each patch at random by up to 7 pixels in each direction, a random shift called jitter. The exercise builds exactly this sampler.

Core

Relative-position pretext pairs

Build the pair sampler of context prediction. Implement relative_position_pair(img, P, gap, jitter, rng) for an image of shape (H, W) or (H, W, C). It returns (center, neighbor, label).

  • Lay out a 3 by 3 grid of square cells of side P, with gap pixels between neighboring cells. Cell (r,c)(r, c) then has the nominal top-left corner (y0+r(P+gap),  x0+c(P+gap))(y_0 + r(P + \text{gap}),\; x_0 + c(P + \text{gap})).
  • Draw the grid origin (y0,x0)(y_0, x_0) uniformly among the integer positions that keep every patch inside the image for any jitter. So y0∈[jitter, H−3P−2 gap−jitter]y_0 \in [\text{jitter},\, H - 3P - 2\,\text{gap} - \text{jitter}], and likewise for x0x_0 with WW.
  • The center patch is cell (1,1)(1, 1). Pick the neighbor uniformly among the other 8 cells.
  • Give each of the two patches its own integer offset, uniform in [−jitter,jitter][-\text{jitter}, \text{jitter}] on each axis.
  • Number the labels 0 to 7 in row-major order with the center skipped, so (0,0)↦0(0,0)\mapsto 0, (0,1)↦1(0,1)\mapsto 1, (0,2)↦2(0,2)\mapsto 2, (1,0)↦3(1,0)\mapsto 3, (1,2)↦4(1,2)\mapsto 4, (2,0)↦5(2,0)\mapsto 5, (2,1)↦6(2,1)\mapsto 6, (2,2)↦7(2,2)\mapsto 7.
  • Return the patches as copies of shape (P, P) or (P, P, C), and use only rng for randomness.
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.

Jigsaw puzzles

A jigsaw task shuffles the pieces of a photo and asks which shuffle was used. Noroozi and Favaro (2016) used it to ask about many spatial relations at once. They crop a 225 by 225 region and split it into a 3 by 3 grid of 75-pixel cells. From a random spot in each cell they take a 64 by 64 tile, which leaves random gaps between tiles. They shuffle the 9 tiles with one permutation from a fixed set, and the network must say which permutation it was.

Each tile goes through the same convolutional tower, and only the last fully connected layers see all nine. Because the tiles are seen together, one ambiguous tile can be placed by elimination. The authors argued that this teaches the network what parts an object is made of.

Which permutations go in the set matters. Two permutations that differ only by swapping two similar tiles, say two patches of sky, are impossible to tell apart, so that label would be noise. So the authors chose permutations that are far apart in Hamming distance, the number of positions at which two permutations differ. They scored the features by fine-tuning a detector on PASCAL VOC 2007, a 20-class object detection benchmark. The score is mean average precision (mAP), the standard detection score from 0 to 100.

Their Table 4 shows the effect of the distance. With 1,000 permutations, an average Hamming distance of 8.00 gave 71 percent puzzle accuracy and 53.2 mAP. Sets with averages of 6.35 and 3.99 gave 62 and 54 percent on the puzzle and only 51.3 and 50.2 mAP. One fact puts 8.00 in perspective.

Proof. At each position ii, the item π′(i)\pi'(i) is uniform over the nn items and independent of π(i)\pi(i), so the two agree there with probability 1/n1/n. By linearity of expectation they agree at n⋅(1/n)=1n \cdot (1/n) = 1 position on average, so they differ at n−1n - 1. ∎

For 9 tiles that is exactly 8, so an average of 8.00 is what a random set already gives. The paper's rule builds the set greedily. It adds, one at a time, the permutation with the largest summed distance to the permutations already chosen. That pushes the average barely above 8, and the exercise shows that it leaves the closest pair as close as two permutations can be.

Core

A maximal-Hamming permutation set

Build the paper's permutation set, and compare it with a rule that protects the closest pair. Implement permutation_set(n_tiles, N, rng, rule="sum"), returning (perms, min_dist, mean_dist).

  • List all permutations of range(n_tiles) in lexicographic order as an int64 array. itertools.permutations gives this order.
  • Make exactly one random draw, j = int(rng.integers(len(all_perms))), for the first permutation.
  • Repeat until N are chosen. Add permutation j to the set, then pick the next j among the permutations not yet chosen.
  • With rule="sum", the paper's Algorithm 1, the next j maximizes the summed Hamming distance to the chosen set. With rule="min", called farthest-point sampling, it maximizes the distance to the nearest chosen permutation. Break ties by the lowest index, which is what np.argmax does.
  • Return the chosen permutations as an (N, n_tiles) array in the order chosen. Also return the minimum and the mean Hamming distance over the N(N−1)/2N(N-1)/2 distinct pairs. Assume N≥2N \ge 2.
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.

Run your solution with 9 tiles and N=100N = 100 and it reproduces a row of the paper's Table 4, an average distance of 8.08 and a minimum of 2, which is a single swap. The farthest-point rule raises the minimum to 6 at almost the same average, about 8.02. A 9-tile run scans all 362,880 permutations 100 times, so it takes a few seconds in plain Python and longer in the browser.

Which mattered more for the features? In the same table, smaller sets with larger minimum distances (3 to 6) gave about the same detection mAP, 52.4 to 52.8, while lowering the average hurt. The tiny sets with the largest minima, 6 to 10 permutations at distance 7 to 9, made the puzzle too easy (97 to 99 percent) and fell to about 49.5 mAP. So for jigsaws the average mattered more than the minimum, and the set must not be so small that the task becomes trivial. When you need a set of well-separated codes in your own work, though, farthest-point sampling is the rule to reach for.

Rotations

Rotation prediction, the example we started from, was proposed by Gidaris, Singh and Komodakis (2018) as the simplest task of all. It turns a whole photo by a multiple of 90 degrees and asks which multiple. To know which way is up, the network must find skies, faces, text, and the bottoms of objects.

Multiples of 90 degrees have a practical advantage. They are flips and transposes of the pixel grid, so no interpolation is needed and no resampling artifacts give the angle away. The authors also found it better to put all four rotated copies of each image in the same batch than to sample one rotation per image. Their AlexNet trained in about two days on one GPU.

The cell builds such a batch and then asks a question that will come back several times. How much of the task can be solved from something other than the objects? In a photo whose sky is brighter than its ground, the mean brightness of the four borders already gives the rotation away.

Python · cell 2

Four numbers solve rotation perfectly when the scene has a bright top, and fall to chance on textures with no up. Real photos sit in between. That is why rotation prediction works at all, and also why it learns less than we would like. Whatever cheap cue solves a pretext task, the network will find it first.

Colorization and inpainting

Two tasks predict pixels rather than a small label. Zhang, Isola and Efros (2016) gave the network only the lightness of each pixel and asked for its color. They worked in the Lab color space, where LL is lightness and aa and bb are two color coordinates, so the input is LL and the target is (a,b)(a, b). A gray shirt could be red or blue, and averaging the possible colors would give a dull brown. So they treated color as classification. They cut the (a,b)(a, b) plane into squares of side 10 and kept the 313 squares that real colors occupy. They also reweighted the loss toward rare colors, so that the network would not settle for a safe brownish gray. Colorization is a cross-channel prediction, one part of each pixel predicting another.

Inpainting removes a region of the image and asks the network to paint it back. Pathak et al. (2016) trained a context encoder to fill the hole from its surroundings. Its loss was a pixel-wise squared error plus, for sharper results, an adversarial loss, the score of a second network trained to tell real fillings from generated ones. Predicting the contents of a hole requires knowing what objects look like. The idea returned five years later, with patches and a much larger fraction removed, as the masked autoencoders of §28.5.

Shortcuts, and why pretext accuracy is not the goal

A pretext task is a stand-in for what we want, and the network optimizes the stand-in. A shortcut is any regularity that predicts the target without understanding the image. The network will learn it, because it is the easiest way down the loss. The most famous example came from the very first context-prediction experiments.

Doersch et al. noticed that for some images the network had found another trivial solution. When they searched feature space for a patch's nearest neighbors, some patches retrieved patches from the same absolute position in other photos, whatever their content. The cause was chromatic aberration. A lens focuses different wavelengths slightly differently. In some cameras one color channel, commonly green, is shrunk slightly toward the image center relative to the others. The offset between green and magenta (red plus blue) then grows with the distance from the center, so a patch's color fringes reveal where it sat in the frame. Once the network knows both patches' absolute positions, their relative position is arithmetic.

The authors removed the cue in two ways. One projects every pixel's color off the green-magenta axis a=(−1,2,−1)\mathbf{a} = (-1, 2, -1), a row vector, by multiplying it by I−a⊤a/(aa⊤)\mathbf{I} - \mathbf{a}^\top\mathbf{a}/(\mathbf{a}\mathbf{a}^\top). The other drops two of the three color channels at random and replaces them with faint noise.

The shift inside a patch can be estimated without any learning. Suppose green at each pixel shows what red shows a small displacement d\mathbf{d} away, G(u)=R(u+d)G(\mathbf{u}) = R(\mathbf{u} + \mathbf{d}). A first-order Taylor expansion gives G(u)≈R(u)+∇R(u)⊤dG(\mathbf{u}) \approx R(\mathbf{u}) + \nabla R(\mathbf{u})^\top\mathbf{d} at every pixel u\mathbf{u}, which is linear in d\mathbf{d}. So the best d\mathbf{d} in the least-squares sense over the patch solves a 2 by 2 linear system.

Python · cell 3

A fringe of 1 percent solves the task perfectly from two numbers. Here that fringe is a difference of only about half a pixel between the shifts of two side-by-side patches, since their centers are 48 pixels apart and 0.01×48=0.480.01\times 48 = 0.48. The texture has no objects, so all of that accuracy came from the lens. The projection removes the cue completely and brings the accuracy back to chance.

∂ Why the projection removes the cue exactly in a gray scene

In a gray scene the red and blue channels hold the same value II, and the green channel holds a displaced value I′I'. So a pixel is p=(I,I′,I)\mathbf{p} = (I, I', I). With a=(−1,2,−1)\mathbf{a} = (-1, 2, -1) we get ap⊤=2(I′−I)\mathbf{a}\mathbf{p}^\top = 2(I' - I) and aa⊤=6\mathbf{a}\mathbf{a}^\top = 6. The projection removes the component 2(I′−I)6 a=13(I′−I) a\tfrac{2(I' - I)}{6}\,\mathbf{a} = \tfrac13(I' - I)\,\mathbf{a}, which leaves

p−13(I′−I) a=13(2I+I′) (1,1,1).\mathbf{p} - \tfrac13(I' - I)\,\mathbf{a} = \tfrac13(2I + I')\,(1, 1, 1).

All three channels now carry the same value, so green and red are identical in every patch and the estimated shift is zero everywhere. ∎

A second, subtler problem is that pretext accuracy does not rank networks. Kolesnikov, Zhai and Beyer (2019) ran a large controlled study of four pretext tasks on three families of networks at several widths. The families were ResNets, which have skip connections (§0.8), RevNets, a reversible variant of ResNets, and VGG, a plain stack of convolutions with no skip connections. Pretext accuracy was somewhat predictive of downstream quality within one family, but not across families. The widest VGG was the best rotation predictor and gave poor features.

They found two more patterns. In networks without skip connections, such as VGG and the AlexNets of the original papers, the representation got worse in the last layers, which specialize to the pretext. In ResNets it did not. And widening the network, which also widens the representation, helped every method. So architecture mattered as much as the task. Context prediction, one of the earliest methods, reached 51.4 percent on an ImageNet linear probe and beat every self-supervised result published before the study once it ran on the right network.

In practice, never pick a pretext model by its pretext accuracy. An interviewer who asks "is pretext accuracy a good measure of quality?" wants the jigsaw ablation and the VGG result as evidence for "no".

Invariance and equivariance

The pretext tasks so far share a structure. They ask the representation to change in a predictable way when the input is transformed, since a network can report the rotation only if the rotation shows up in its features. The opposite request, that the representation not change, is what the rest of the chapter uses. Both need the idea of a random transformation of an image.

In plain words, an invariant feature ignores the transformation. An equivariant one changes with it in a fixed way that does not depend on the image. Rotation prediction needs features that change with rotation, ideally equivariantly, so that a head can read the angle off them. Contrastive learning asks for features that do not change under crops and color jitter.

Misra and van der Maaten (2020) compared the two directly. Their method, PIRL (Pretext-Invariant Representation Learning), used the jigsaw shuffle not as a target to predict but as an augmentation to be invariant to. It pulled the representation of an image toward that of its shuffled version. With a ResNet-50 on ImageNet, the invariant version reached 63.6 percent with a linear probe, against 45.7 for the same network trained to predict the shuffle.

Every augmentation is a claim about what does not matter. Random crops claim that a part identifies the whole. Color jitter claims that hue is irrelevant. Flips claim that left and right are interchangeable. When a claim is wrong for a downstream task, the representation has been trained to throw away exactly what that task needs.

This has been measured too. Ericsson et al. (2021) evaluated 13 self-supervised models on 40 downstream tasks. When they reconstructed images from the features, the self-supervised ones gave back less faithful color than a supervised network, and the authors attribute this to the heavy color distortion in training. That matters for flowers, birds, or medical stains. §28.2 builds the concrete augmentation recipe and measures each part. In practice, list what your downstream task needs before you pick augmentations, and remove any augmentation that destroys it.

Every image is its own class

The idea that turned self-supervised vision from a collection of puzzles into a field was to take classification to its extreme. Dosovitskiy et al. (2014) made each randomly sampled image patch a surrogate class. They generated many transformed copies of each patch and trained a classifier to tell the classes apart. The features then had to survive the transformations and still distinguish the patches.

With thousands of classes this was feasible. With a million it was not. A softmax layer over a million classes has one weight vector per class. Each class is seen only once per epoch, so those weights barely train.

Wu et al. (2018) removed the class weight vectors. If every image is a class, let the image's own stored feature serve as that class's prototype.

In plain words, each image must be recognizable among all the others from its own feature. The features of the other images, read from a slowly updated table, play the competing classes. "Non-parametric" means there are no learned class weights, only stored features. The temperature τ\tau divides the scores before the softmax, so a small τ\tau makes the weights sharper (§7.2 analyzes it in detail).

Wu et al. used d=128d = 128 and τ=0.07\tau = 0.07. With unit vectors the scores then lie in [−1/τ,1/τ]=[−14.3,14.3][-1/\tau, 1/\tau] = [-14.3, 14.3], and a small τ\tau is what lets the softmax pick out one image among a million. They did not compute the full sum over all nn images. They used noise-contrastive estimation (Gutmann and Hyvärinen 2010), which turns the softmax into a yes-or-no task, telling the true entry apart from 4,096 entries drawn at random from the bank.

Why would telling individual photographs apart teach anything about categories? The cheapest way to tell 1.28 million images apart under random crops and color changes is to describe what is in them. Two photos of tabby cats then end up near each other because they share most of their description, not because anyone said "cat".

Wu et al. measured this. On CIFAR-10 their non-parametric features gave 80.8 percent accuracy with a nearest-neighbor classifier, against 63.0 percent for the same network trained with an ordinary softmax that has one weight vector per instance. On ImageNet a ResNet-50 reached 54.0 percent with a linear probe on its frozen features. The memory bank is the engineering trick that makes a million classes affordable.

The bank needs a rule for how entries change. Wu et al. added a proximal term, a penalty on each feature for moving far from its stored entry. The more common form, which MoCo's authors describe as a momentum update on the bank, blends the old entry with the new feature and renormalizes,

vi←normalize⁡(m vi+(1−m) h^i),\mathbf{v}_i \leftarrow \operatorname{normalize}\big(m\,\mathbf{v}_i + (1 - m)\,\hat{\mathbf{h}}_i\big),

with a momentum m∈[0,1)m \in [0, 1). In plain words, the entry moves a fraction 1−m1 - m of the way toward the new feature and is then put back on the unit sphere. The exercise uses this form, with the exact softmax over a small bank.

Interview

Instance discrimination with a memory bank

Implement one training step of instance discrimination, instdisc_step(h, idx, bank, tau, m), returning (loss, dh, new_bank).

  • The inputs are raw features h of shape (B, d), the dataset indices idx of shape (B,) of the images they came from (all distinct), a bank of shape (n, d) with unit rows, a temperature tau, and a momentum m.
  • Normalize the features. Compute the mean loss 1B∑i−log⁡p(idxi∣h^i)\frac1B\sum_i -\log p(\text{idx}_i\mid\hat{\mathbf{h}}_i) with the full non-parametric softmax over the bank as it was before this step.
  • Compute dh, the gradient of the loss with respect to the raw h. Treat the bank as a constant.
  • Build new_bank, a copy in which row idxi\text{idx}_i becomes normalize⁡(m vidxi+(1−m) h^i)\operatorname{normalize}(m\,\mathbf{v}_{\text{idx}_i} + (1 - m)\,\hat{\mathbf{h}}_i) and every other row is unchanged. Leave the input bank untouched.
  • Keep it numerically stable for any temperature.
Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.

Instance discrimination is a contrastive loss in disguise. Recall from §7.2 that the InfoNCE loss scores one positive, the item that truly matches, against many negatives. It applies a softmax cross-entropy with the positive as the correct class. Here the positive for a view of image ii is image ii's bank entry, and the negatives are everyone else's. The step from here to the contrastive methods of §28.2, SimCLR and MoCo, is small. Replace the stored entry by the feature of a second, freshly augmented view of the same image.

An interviewer may ask how you would train a classifier with a million classes and one example each. The answer is this section's trick. Drop the class weights, store one feature per class, and approximate the softmax with sampled negatives.

Augmentations say what should not matter

Take two views of one photograph, and a loss that wants their representations to agree. This is the template behind nearly every method of the next three sections, and it has one trap built in. If agreement is all the loss asks for, an encoder that maps every image to the same vector agrees perfectly. That failure is called collapse. §1.10 watched it happen with a loss that only pulled pairs together, and §7.2 showed that pushing other examples away prevents it. The methods differ mainly in what they use instead of, or in addition to, that push.

The table lists how each family of methods rules out the constant solution, and where the book builds it.

methodwhat rules out the constant solutionwhere
SimCLR, MoConegatives, other images pushed away in a softmax§28.2
spectral contrastive lossa quadratic penalty on agreement between views of different imagesthis section
BYOL, SimSiama predictor on one branch and a stop-gradient on the other (BYOL adds a moving-average target)§28.3
Barlow Twins, VICRegdecorrelation, each embedding dimension kept alive and independent of the others§28.3
SwAVequipartition, cluster assignments forced to use every cluster equally§28.3
DINO, DINOv2, DINOv3a moving-average teacher whose outputs are centered and sharpened§28.4

Notice what is absent from the template. Nothing says which images belong together except the augmentations. If two photos of different dogs end up close, no label put them there. Something in the augmentations, and in the images themselves, did. The rest of this section makes that sentence precise, and it is the theoretical reason the whole approach works.

The augmentation graph

HaoChen, Wei, Gaidon and Ma (2021) found the right object to think about. Picture every possible view of every possible image as a point, a vertex of a graph. Join two views by an edge whose weight is the probability that they come out of the same image.

Crops of one cat photo are joined directly. Two different cat photos are joined indirectly. After cropping, blur, and color jitter, a tight crop of one cat could have come from either of two similar cat photos, so the two photos share views. A chain of such shared views links all the cat photos. A cat and a truck, if the augmentations never make them look alike, sit in different pieces of the graph.

In this part of the section we follow their notation. We write xˉ\bar x for a natural image, xx for a view, and A(x∣xˉ)\mathcal{A}(x\mid\bar x) for the probability that the augmentation turns xˉ\bar x into xx. We take the set of views to be finite with NN elements. That is true of real images, which have finitely many pixel values, and it keeps every step elementary.

In plain words, Ωxx′\Omega_{xx'} is how often the pair (x,x′)(x, x') shows up when you take one random photo and augment it twice. Dividing by the square roots of the degrees stops very common views from dominating.

Two properties come straight from the definition. Ω\boldsymbol\Omega is symmetric. It is also positive semidefinite, meaning u⊤Ωu≥0\mathbf{u}^\top\boldsymbol\Omega\mathbf{u} \ge 0 for every vector u\mathbf{u}, because u⊤Ωu=Exˉ[(∑xA(x∣xˉ) ux)2]\mathbf{u}^\top\boldsymbol\Omega\mathbf{u} = \mathbb{E}_{\bar x}\big[\big(\sum_x \mathcal{A}(x\mid\bar x)\,u_x\big)^2\big] is an average of squares. With finitely many images, write p\mathbf{p} for their probabilities and T\mathbf{T} for the image-to-view matrix with entries Txˉx=A(x∣xˉ)T_{\bar x x} = \mathcal{A}(x\mid\bar x). Then Ω=T⊤diag⁡(p) T\boldsymbol\Omega = \mathbf{T}^\top\operatorname{diag}(\mathbf{p})\,\mathbf{T}, a weighted Gram matrix, a matrix of inner products in the sense of §0.10. So Ωˉ\bar{\boldsymbol\Omega} is symmetric positive semidefinite too. It has real eigenvalues γ1≥γ2≥⋯≥γN≥0\gamma_1 \ge \gamma_2 \ge \dots \ge \gamma_N \ge 0 and orthonormal eigenvectors u1,…,uN\mathbf{u}_1, \dots, \mathbf{u}_N.

The cell builds this graph from T\mathbf{T} and p\mathbf{p} and checks the spectrum for several qq.

Python · cell 4

The top eigenvectors found the classes without any label. That is the pattern the rest of the section turns into a theorem.

The spectral contrastive loss

Spectral clustering groups the vertices of a graph using the top eigenvectors of its normalized adjacency matrix. Those eigenvectors, divided by the square roots of the degrees, are nearly constant on groups that are well connected inside and weakly connected to each other. That is what happened in the example. But we cannot write the augmentation graph down, since it has a vertex for every possible view. HaoChen et al. observed that a simple loss on pairs of views, which needs only samples, has exactly these eigenvectors as its minimizers.

Let ff map each view to Rk\R^k. It stands for the whole network that produces the embedding, encoder and any head together. A positive pair (x,x+)(x, x^+) is two independent views of one image xˉ∼D\bar x \sim \mathcal{D}. A negative pair (x,x−)(x, x^-) is two independent views of two independently drawn images. So xx and x−x^- are independent, and each has the marginal distribution ω\boldsymbol\omega.

Two matrix facts are needed. The Frobenius norm ∥A∥F\lVert\mathbf{A}\rVert_F is the square root of the sum of squared entries, and ⟨A,B⟩=∑ijAijBij\langle\mathbf{A}, \mathbf{B}\rangle = \sum_{ij}A_{ij}B_{ij} is the matching inner product. The Eckart-Young theorem of §0.10 says that the best rank-kk approximation of a matrix in this norm keeps its top kk singular directions. The squared error is the sum of the squared singular values left out.

The proof has two steps. The positive term becomes an inner product with Ωˉ\bar{\boldsymbol\Omega}, and the negative term becomes the squared norm of FF⊤\mathbf{F}\mathbf{F}^\top. Completing the square then gives the factorization, and Eckart-Young gives the bound.

∂ Proof of the factorization and the bound

The probability that a positive pair equals (x,x′)(x, x') is Ωxx′\Omega_{xx'}, so

E(x,x+)[f(x)⊤f(x+)]=∑x,x′Ωxx′ f(x)⊤f(x′)=∑x,x′Ωxx′ωxωx′ ωxf(x)⊤ωx′f(x′)=∑x,x′Ωˉxx′ (FF⊤)xx′.\mathbb{E}_{(x,x^+)}\big[f(x)^\top f(x^+)\big] = \sum_{x,x'}\Omega_{xx'}\,f(x)^\top f(x') = \sum_{x,x'}\frac{\Omega_{xx'}}{\sqrt{\omega_x\omega_{x'}}}\,\sqrt{\omega_x}f(x)^\top\sqrt{\omega_{x'}}f(x') = \sum_{x,x'}\bar\Omega_{xx'}\,(\mathbf{F}\mathbf{F}^\top)_{xx'} .

A negative pair equals (x,x′)(x, x') with probability ωxωx′\omega_x\omega_{x'}, so

Ex,x−[(f(x)⊤f(x−))2]=∑x,x′ωxωx′(f(x)⊤f(x′))2=∑x,x′(FF⊤)xx′2=∥FF⊤∥F2.\mathbb{E}_{x,x^-}\big[\big(f(x)^\top f(x^-)\big)^2\big] = \sum_{x,x'}\omega_x\omega_{x'}\big(f(x)^\top f(x')\big)^2 = \sum_{x,x'}(\mathbf{F}\mathbf{F}^\top)_{xx'}^2 = \lVert\mathbf{F}\mathbf{F}^\top\rVert_F^2 .

Hence L(f)=−2⟨Ωˉ,FF⊤⟩+∥FF⊤∥F2\mathcal{L}(f) = -2\langle\bar{\boldsymbol\Omega}, \mathbf{F}\mathbf{F}^\top\rangle + \lVert\mathbf{F}\mathbf{F}^\top\rVert_F^2. Adding and subtracting ∥Ωˉ∥F2\lVert\bar{\boldsymbol\Omega}\rVert_F^2 completes the square, since ∥Ωˉ−FF⊤∥F2=∥Ωˉ∥F2−2⟨Ωˉ,FF⊤⟩+∥FF⊤∥F2\lVert\bar{\boldsymbol\Omega} - \mathbf{F}\mathbf{F}^\top\rVert_F^2 = \lVert\bar{\boldsymbol\Omega}\rVert_F^2 - 2\langle\bar{\boldsymbol\Omega}, \mathbf{F}\mathbf{F}^\top\rangle + \lVert\mathbf{F}\mathbf{F}^\top\rVert_F^2.

For the bound, FF⊤\mathbf{F}\mathbf{F}^\top has rank at most kk. The singular values of a symmetric positive semidefinite matrix are its eigenvalues, so Eckart-Young gives ∥Ωˉ−M∥F2≥∑i>kγi2\lVert\bar{\boldsymbol\Omega} - \mathbf{M}\rVert_F^2 \ge \sum_{i>k}\gamma_i^2 for every M\mathbf{M} of rank at most kk. Since ∥Ωˉ∥F2=∑iγi2\lVert\bar{\boldsymbol\Omega}\rVert_F^2 = \sum_i\gamma_i^2, we get L(f)≥∑i>kγi2−∑iγi2=−∑i≤kγi2\mathcal{L}(f) \ge \sum_{i>k}\gamma_i^2 - \sum_i\gamma_i^2 = -\sum_{i\le k}\gamma_i^2. Finally F⋆F⋆⊤=∑i≤kγiuiui⊤\mathbf{F}^\star\mathbf{F}^{\star\top} = \sum_{i\le k}\gamma_i\mathbf{u}_i\mathbf{u}_i^\top is the truncated eigendecomposition, which attains the bound. ∎

In plain words, minimizing this loss finds the best rank-kk approximation of the augmentation graph, which is what spectral clustering computes. The first term rewards agreement between views of one image. The second penalizes agreement between unrelated views, quadratically, and that is what rules out the constant solution. A constant f=cf = \mathbf{c} pays −2∥c∥2+∥c∥4-2\lVert\mathbf{c}\rVert^2 + \lVert\mathbf{c}\rVert^4, which is never below −1-1. The optimum is −∑i≤kγi2-\sum_{i\le k}\gamma_i^2, close to −k-k when the graph splits into kk pieces. In the hand example at q=0.1q = 0.1 with k=2k = 2, collapse scores −1-1 and the class indicators score −2-2.

The loss never mentions eigenvectors, and it can be estimated from samples. The first term uses two views of each image in a batch, and the second uses views of different images. That is what makes it trainable.

When a linear probe is perfect

Why is the top of the spectrum the right thing to learn? Because of what an eigenvalue of 1 means. A connected component of the graph is a piece with no edge of positive weight leaving it.

The proof of items 1 and 2 rests on one identity. For any vector, the matrix I−Ωˉ\mathbf{I} - \bar{\boldsymbol\Omega}, called the normalized graph Laplacian, gives a weighted sum of squared differences across edges. That sum is never negative. It is zero exactly when the vector, after rescaling by the degrees, is constant on each component.

∂ Proof of items 1 and 2

For any v∈RN\mathbf{v} \in \R^N put s=diag⁡(ω)−1/2v\mathbf{s} = \operatorname{diag}(\boldsymbol\omega)^{-1/2}\mathbf{v}. Then v⊤Ωˉv=∑x,x′Ωxx′sxsx′\mathbf{v}^\top\bar{\boldsymbol\Omega}\mathbf{v} = \sum_{x,x'}\Omega_{xx'}s_xs_{x'} and v⊤v=∑xωxsx2=∑x,x′Ωxx′sx2\mathbf{v}^\top\mathbf{v} = \sum_x\omega_x s_x^2 = \sum_{x,x'}\Omega_{xx'}s_x^2. Using the symmetry of Ω\boldsymbol\Omega to split sx2s_x^2 into 12(sx2+sx′2)\tfrac12(s_x^2 + s_{x'}^2),

v⊤(I−Ωˉ) v=∑x,x′Ωxx′(sx2−sxsx′)=12∑x,x′Ωxx′ (sx−sx′)2≥0.\mathbf{v}^\top(\mathbf{I} - \bar{\boldsymbol\Omega})\,\mathbf{v} = \sum_{x,x'}\Omega_{xx'}\big(s_x^2 - s_xs_{x'}\big) = \tfrac12\sum_{x,x'}\Omega_{xx'}\,(s_x - s_{x'})^2 \ge 0 .

So the Rayleigh quotient v⊤Ωˉv/v⊤v\mathbf{v}^\top\bar{\boldsymbol\Omega}\mathbf{v}/\mathbf{v}^\top\mathbf{v} never exceeds 1. The largest eigenvalue of a symmetric matrix is the maximum of its Rayleigh quotient (§0.10), so no eigenvalue exceeds 1, and positive semidefiniteness keeps them at or above 0.

An eigenvector with eigenvalue 1 makes the sum zero. Then sx=sx′s_x = s_{x'} across every edge with Ωxx′>0\Omega_{xx'} > 0, so s\mathbf{s} is constant on each connected component. Conversely, any s\mathbf{s} constant on components makes the sum zero. For a positive semidefinite matrix M\mathbf{M}, v⊤Mv=∥M1/2v∥2=0\mathbf{v}^\top\mathbf{M}\mathbf{v} = \lVert\mathbf{M}^{1/2}\mathbf{v}\rVert^2 = 0 forces Mv=0\mathbf{M}\mathbf{v} = \mathbf{0}. So v\mathbf{v} is in the null space of I−Ωˉ\mathbf{I} - \bar{\boldsymbol\Omega}, which makes it an eigenvector with eigenvalue 1. The space of such v=diag⁡(ω)1/2s\mathbf{v} = \operatorname{diag}(\boldsymbol\omega)^{1/2}\mathbf{s} is spanned by the rr vectors diag⁡(ω)1/21Sc\operatorname{diag}(\boldsymbol\omega)^{1/2}\mathbf{1}_{S_c}, which are nonzero with disjoint supports, so it has dimension exactly rr. ∎

Item 3 needs more care, because the eigenvalues just below 1 may tie, and then the minimizer is not unique. The derivation shows that every minimizer still keeps the whole eigenspace of eigenvalue 1.

∂ Proof that every minimizer keeps the top eigenspace

Let EE be the eigenspace of eigenvalue 1, of dimension r≤kr \le k, and let ff be any minimizer, with F\mathbf{F} as in the theorem. Let P\mathbf{P} be the orthogonal projector onto the column space of F\mathbf{F}, a space of dimension m≤km \le k, so that PFF⊤P=FF⊤\mathbf{P}\mathbf{F}\mathbf{F}^\top\mathbf{P} = \mathbf{F}\mathbf{F}^\top.

Step 1, the best matrix on a fixed subspace. For any symmetric X\mathbf{X}, ⟨Ωˉ−PΩˉP, PXP⟩=tr⁡(PΩˉPX)−tr⁡(PΩˉPX)=0\langle\bar{\boldsymbol\Omega} - \mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P},\, \mathbf{P}\mathbf{X}\mathbf{P}\rangle = \operatorname{tr}(\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\mathbf{X}) - \operatorname{tr}(\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\mathbf{X}) = 0, using the cyclic property of the trace and P2=P\mathbf{P}^2 = \mathbf{P}. Since PΩˉP−FF⊤=P(Ωˉ−FF⊤)P\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P} - \mathbf{F}\mathbf{F}^\top = \mathbf{P}(\bar{\boldsymbol\Omega} - \mathbf{F}\mathbf{F}^\top)\mathbf{P}, taking X=Ωˉ−FF⊤\mathbf{X} = \bar{\boldsymbol\Omega} - \mathbf{F}\mathbf{F}^\top and then X=Ωˉ\mathbf{X} = \bar{\boldsymbol\Omega}, Pythagoras gives

∥Ωˉ−FF⊤∥F2=∥Ωˉ−PΩˉP∥F2+∥PΩˉP−FF⊤∥F2,∥Ωˉ−PΩˉP∥F2=∥Ωˉ∥F2−∥PΩˉP∥F2.\lVert\bar{\boldsymbol\Omega} - \mathbf{F}\mathbf{F}^\top\rVert_F^2 = \lVert\bar{\boldsymbol\Omega} - \mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\rVert_F^2 + \lVert\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P} - \mathbf{F}\mathbf{F}^\top\rVert_F^2, \qquad \lVert\bar{\boldsymbol\Omega} - \mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\rVert_F^2 = \lVert\bar{\boldsymbol\Omega}\rVert_F^2 - \lVert\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\rVert_F^2 .

So L(f)≥−∥PΩˉP∥F2\mathcal{L}(f) \ge -\lVert\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\rVert_F^2.

Step 2, a bound in terms of eigenvalues. Expand Ωˉ=∑iγiuiui⊤\bar{\boldsymbol\Omega} = \sum_i\gamma_i\mathbf{u}_i\mathbf{u}_i^\top and put bij=(ui⊤Puj)2=bji≥0b_{ij} = (\mathbf{u}_i^\top\mathbf{P}\mathbf{u}_j)^2 = b_{ji} \ge 0 and pi=ui⊤Pui=∥Pui∥2∈[0,1]p_i = \mathbf{u}_i^\top\mathbf{P}\mathbf{u}_i = \lVert\mathbf{P}\mathbf{u}_i\rVert^2 \in [0, 1]. Since the uj\mathbf{u}_j form an orthonormal basis, ∑jbij=∥Pui∥2=pi\sum_j b_{ij} = \lVert\mathbf{P}\mathbf{u}_i\rVert^2 = p_i, and ∑ipi=tr⁡P=m≤k\sum_i p_i = \operatorname{tr}\mathbf{P} = m \le k. Then, using γiγj≤12(γi2+γj2)\gamma_i\gamma_j \le \tfrac12(\gamma_i^2 + \gamma_j^2),

∥PΩˉP∥F2=tr⁡(PΩˉPΩˉ)=∑i,jγiγj bij≤∑i,j12(γi2+γj2) bij=∑iγi2 pi.\lVert\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\rVert_F^2 = \operatorname{tr}(\mathbf{P}\bar{\boldsymbol\Omega}\mathbf{P}\bar{\boldsymbol\Omega}) = \sum_{i,j}\gamma_i\gamma_j\,b_{ij} \le \sum_{i,j}\tfrac12(\gamma_i^2 + \gamma_j^2)\,b_{ij} = \sum_i\gamma_i^2\,p_i .

Step 3, the deficit. Let Δ=∑i≤kγi2−∑iγi2pi\Delta = \sum_{i\le k}\gamma_i^2 - \sum_i\gamma_i^2p_i. The first rr eigenvalues equal 1. Put g=γr+1g = \gamma_{r+1} if k=rk = r and g=γkg = \gamma_k if k>rk > r, so that g<1g < 1, γi≤g\gamma_i \le g for i>ki > k, and γi≥g\gamma_i \ge g for r<i≤kr < i \le k. Write Δ=∑i≤r(1−pi)+∑r<i≤kγi2(1−pi)−∑i>kγi2pi\Delta = \sum_{i\le r}(1 - p_i) + \sum_{r<i\le k}\gamma_i^2(1 - p_i) - \sum_{i>k}\gamma_i^2p_i. Since 1−pi≥01 - p_i \ge 0 and pi≥0p_i \ge 0, we may replace γi2\gamma_i^2 by g2g^2 in the middle sum (a lower bound, and an empty sum when k=rk = r) and in the last sum (an upper bound on what is subtracted). Also ∑i>kpi≤k−∑i≤kpi=∑i≤k(1−pi)\sum_{i>k}p_i \le k - \sum_{i\le k}p_i = \sum_{i\le k}(1 - p_i). Therefore

Δ≥∑i≤r(1−pi)+g2∑r<i≤k(1−pi)−g2∑i≤k(1−pi)=(1−g2)∑i≤r(1−pi)≥0.\Delta \ge \sum_{i\le r}(1 - p_i) + g^2\sum_{r<i\le k}(1 - p_i) - g^2\sum_{i\le k}(1 - p_i) = (1 - g^2)\sum_{i\le r}(1 - p_i) \ge 0 .

Step 4, equality. Steps 1 to 3 give L(f)≥−∑i≤kγi2+Δ\mathcal{L}(f) \ge -\sum_{i\le k}\gamma_i^2 + \Delta, and the minimum value −∑i≤kγi2-\sum_{i\le k}\gamma_i^2 is attained by the theorem, so a minimizer has Δ=0\Delta = 0. Since g<1g < 1, every pip_i with i≤ri \le r equals 1, that is ∥Pui∥=∥ui∥\lVert\mathbf{P}\mathbf{u}_i\rVert = \lVert\mathbf{u}_i\rVert. So u1,…,ur\mathbf{u}_1, \dots, \mathbf{u}_r lie in the column space of F\mathbf{F}, and EE does too.

Step 5, the probe. Each diag⁡(ω)1/21Sc\operatorname{diag}(\boldsymbol\omega)^{1/2}\mathbf{1}_{S_c} is in EE, hence equals Fac\mathbf{F}\mathbf{a}_c for some ac∈Rk\mathbf{a}_c \in \R^k. Reading row xx, ωx 1[x∈Sc]=ωx f(x)⊤ac\sqrt{\omega_x}\,\mathbb{1}[x\in S_c] = \sqrt{\omega_x}\,f(x)^\top\mathbf{a}_c, and dividing by ωx>0\sqrt{\omega_x} > 0 gives ac⊤f(x)=1[x∈Sc]\mathbf{a}_c^\top f(x) = \mathbb{1}[x \in S_c]. A probe for a class that is a union ⋃c∈CSc\bigcup_{c\in C}S_c uses ∑c∈Cac\sum_{c\in C}\mathbf{a}_c. ∎

In plain words, augmentations draw a graph, and the graph's connected pieces show up as eigenvectors with eigenvalue 1. Given at least one dimension per piece, the spectral contrastive loss puts each piece at its own point of Rk\R^k, where a linear classifier can read it off. Which images share a piece is decided by the augmentations and the data, never by labels. So the augmentations are the supervision.

The theorem also says what goes wrong. If augmentations are too weak, each image is its own piece. Then there are far more eigenvalue-1 directions than dimensions, and the loss keeps an arbitrary few of them. If augmentations leak across classes, the class pieces merge and the class eigenvalues drop below 1. Once they fall below the directions that split a class internally, the loss prefers the wrong ones.

The cell tests both failures. It builds an augmentation graph for 300 points in three hidden classes. The augmentation adds Gaussian noise of standard deviation σ\sigma, the running example of HaoChen et al. A leak sends a fraction qq of each image's views to views of other classes.

Python · cell 5

The probe is fit and scored on the same 600 views, so it measures whether the classes are linearly separable in ff, not whether the probe generalizes. With σ=0.05\sigma = 0.05 the noise is too small to connect different images. The top eigenvalues are all 1 to four decimals, the three kept directions are arbitrary, and the probe gets 66 percent. With σ=0.4\sigma = 0.4 each class is nearly one piece, joined to the others only by tiny Gaussian tails. γ2\gamma_2 and γ3\gamma_3 stay above 0.996 while γ4\gamma_4, the first direction inside a class, is 0.68, and the probe reaches 98.7 percent.

As qq grows, the class eigenvalues fall toward the within-class ones, 0.73 against 0.49 at q=0.1q = 0.1 and 0.35 against 0.27 at q=0.3q = 0.3. When the gap closes at q=0.45q = 0.45, the probe drops to 47 percent. The widget lets you move through these regimes yourself.

When can augmentations alone reveal the classes?
Thirty images in three hidden classes, two noisy views of each. A leak sends views to other classes.
Loading interactive figure…

What this changes in practice is how you tune augmentations. They must be strong enough to connect the views of each class and weak enough never to connect different classes. An interviewer may ask "why do contrastive methods need such strong augmentation?", and this picture is the answer.

The eigenvectors above are a nonparametric answer, one free vector per view, which we could compute only because the graph was tiny. A network gives a parametric ff, a function with weights, and learns from sampled pairs. The next exercise writes the loss it learns from.

Interview

The spectral contrastive loss and its gradient, no loops

Write the batch version of the spectral contrastive loss with its gradients. A batch holds BB positive pairs with embeddings Z1,Z2∈RB×k\mathbf{Z}_1, \mathbf{Z}_2 \in \R^{B\times k}, where row ii of each comes from a view of image ii. The empirical loss is

L^=−2B∑i=1Bz1,i⊤z2,i+1B(B−1)∑i≠j(z1,i⊤z2,j)2.\hat{\mathcal{L}} = -\frac{2}{B}\sum_{i=1}^{B}\mathbf{z}_{1,i}^\top\mathbf{z}_{2,i} + \frac{1}{B(B-1)}\sum_{i\ne j}\big(\mathbf{z}_{1,i}^\top\mathbf{z}_{2,j}\big)^2 .

Pairs of different rows play the negatives. Implement spectral_loss(Z1, Z2) returning (loss, dZ1, dZ2), the loss and its gradients with respect to both inputs, without Python loops. Assume B≥2B \ge 2. This loss uses the raw embeddings f(x)f(x), so do not normalize them.

Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.

Now train a real parametric ff with this loss and see whether it finds what the theorem predicts. The cell uses four blobs of points at the corners of a square, labeled like XOR, with one class on each diagonal. Each class is then a union of two components, and no linear probe on the raw coordinates can separate them.

The encoder must be nonlinear, but we want to train it with plain gradient steps on one weight matrix. So it first maps each input through 128 fixed random Fourier features, ϕ(x)=2/D cos⁡(xWrff+brff)\phi(\mathbf{x}) = \sqrt{2/D}\,\cos(\mathbf{x}\mathbf{W}_{\text{rff}} + \mathbf{b}_{\text{rff}}) with D=128D = 128 and a random Wrff\mathbf{W}_{\text{rff}} and brff\mathbf{b}_{\text{rff}} drawn once (Rahimi and Recht 2007). Then f(x)=W⊤ϕ(x)f(\mathbf{x}) = \mathbf{W}^\top\phi(\mathbf{x}) is a nonlinear function of the input, and only W\mathbf{W} is trained. It never sees a label.

Python · cell 6

Everything the theorem says is visible. The final batch loss estimates L(f)\mathcal{L}(f), which can never fall below −∑i≤kγi2≥−k-\sum_{i\le k}\gamma_i^2 \ge -k. Training reaches about −1.9-1.9 for k=2k = 2 and −3.8-3.8 to −3.9-3.9 for k=4k = 4, so the graph's top four eigenvalues are all close to 1, one per blob. With k=4k = 4, at least the number of blobs r=4r = 4, the probe separates the XOR classes at 99 percent for every seed, while the raw coordinates give 50 percent.

With k=2<rk = 2 < r the loss cannot tell which two of the four blob directions to keep, since they are all worth the same. So the result depends on the random start, 99, 52, and 95 percent for three seeds. Gradient descent on a sampled loss found the spectral answer without ever forming the graph. In practice this is why embedding width matters, and why §28.3 finds that wider projection heads help.

Research

Spectral embedding of an augmentation graph

Compute the theorem's optimal features directly from a small graph. Implement three functions.

  • augmentation_graph(T, p) takes T of shape (n_images, N), whose row xˉ\bar x is the distribution A(⋅∣xˉ)\mathcal{A}(\cdot\mid\bar x) over the NN views, and the image probabilities p. It returns Ω=T⊤diag⁡(p) T\boldsymbol\Omega = \mathbf{T}^\top\operatorname{diag}(\mathbf{p})\,\mathbf{T}.
  • spectral_features(Omega, k) returns (f, gamma). Here f is the (N, k) array of optimal features f(x)=Fx⋆/ωxf(x) = \mathbf{F}^\star_x/\sqrt{\omega_x} from the theorem, and gamma holds the top k eigenvalues of Ωˉ\bar{\boldsymbol\Omega} in descending order.
  • population_spectral_loss(Omega, f) evaluates L(f)\mathcal{L}(f) exactly from the graph, as the two weighted sums in the proof.

The tests check three things. Your features must reach the bound −∑i≤kγi2-\sum_{i\le k}\gamma_i^2. They must be constant on the components of a disconnected graph even when eigenvalues tie, so comparing eigenvectors one by one would fail. And a probe on them, using the probe_accuracy helper from the setup, must recover the classes of a leaky graph.

Tests, hints, verified solutions, and AI help, with Pro at $7.99 a month.

What the theorem assumes

The theorem is exact, but each of its assumptions is violated in practice, so it is worth listing them.

  • It describes the population graph, every possible view of every possible image. The graph of the training set alone would have almost no edges, since two training images essentially never produce the identical crop.
  • It assumes ff reaches the global minimum. A real encoder is a finite network trained for a finite time. Its built-in preferences, called its inductive biases, decide how the loss extends to views it never saw.
  • It assumes class pieces are exactly disconnected, and real ones never are. HaoChen et al. also prove a bound for the nearly disconnected case (their Theorem 3.8), with at least twice as many dimensions as classes. There the probe error is controlled by how little edge weight crosses classes compared with how well each class holds together. They also show that the loss can be minimized from finite samples with guarantees, but the constants are not ones you would quote in an interview.

What carries over is the qualitative picture, which the cells and the widget show. Augmentations must connect each class and must not connect different classes.

How a representation is graded

Every number in the rest of the chapter comes from one of three protocols. Each takes a pretrained encoder and a labeled downstream dataset. §28.6 treats them in depth, with their recipes, their noise, and many variants. Here are the definitions you need to read a results table.

The protocols measure different things. A linear probe asks whether the classes can be read off the features by a linear function. k-NN asks whether neighbors share a class, which is a question about the local geometry. It needs no training at all, which makes it the cheap check people run during pretraining. Fine-tuning asks how good a starting point the weights are. It can reward representations that are neither linear nor locally clustered, as masked autoencoders will show in §28.5. The cell compares the first two on the XOR data.

Python · cell 7

The raw coordinates fail the linear probe (50 percent) and pass k-NN (99 percent), because XOR classes are locally coherent but not linearly separable. The learned features pass both. When two protocols disagree about a representation, they are telling you different true things about it. An interviewer who asks which protocol to report wants exactly this distinction, and usually wants both numbers.

Since 2018 the linear probe on ImageNet has been the headline number, and its trend is the story of this chapter.

In PyTorch

The torch block does three things. It checks autograd against the closed-form gradient of Exercise 28.1d. It trains a small MLP encoder with the spectral contrastive loss on the XOR blobs, with Gaussian noise as the augmentation. And it shows a memory-bank update done the way real code does it, under torch.no_grad(), so the bank never enters the computation graph.

🔥 In PyTorchfor a machine with PyTorch

Choosing a source of supervision

The families below cover the whole chapter and its neighbors. Each is defined by what it predicts, and each has a characteristic way to fail.

familywhat is predictedwhat prevents a trivial solutionstrengthsweaknesses
pretext classification (context, jigsaw, rotation)a small label made from the imagethe label is hard to fake, if shortcuts are blockedcheap, any architecture, no large batchesshortcuts, last layers specialize, weak features (linear probes in the 40s and 50s on ImageNet)
pixel prediction (colorization, inpainting, masked autoencoders)missing pixels or channelsthe target is the input itself, so it cannot collapsedense, local information, scales to huge modelsspends capacity on texture and exact color, weaker linear probes (§28.5)
instance discrimination and contrastive learningwhich view comes from the same imagenegatives, from a bank, a queue, or the batchstrong global features, linear probes in the 70sneeds many negatives, augmentations encode the invariances (§28.2)
non-contrastive joint embedding and self-distillationthe embedding of another viewasymmetry, decorrelation, or centering and sharpeningno negatives, the current frontier (DINOv3)collapse must be designed away (§28.3, §28.4)
weak supervision from textthe caption or hashtag that came with the imagethe text is an external targetopen vocabulary, zero-shot classificationneeds paired data, weaker dense features (CLIP)

A closely related idea keeps the "predict a hidden part" of masked modeling but predicts the embedding of the hidden region instead of its pixels. That is the idea behind joint-embedding predictive architectures, a family that combines the dense targets of the second row with the semantic features of the fourth.

Read more

Watch next

🎬 Lecture 7 Self-Supervised Learning -- UC Berkeley Spring 2020 - CS294-158 Deep Unsupervised Learning · Pieter AbbeelA full Berkeley lecture that walks from the pretext tasks of this section (context prediction, jigsaws, rotations, colorization) to instance discrimination and contrastive learning.
🎬 10L – Self-supervised learning in computer vision · Alfredo Canziani (冷在)Ishan Misra's NYU lecture on pretext tasks, their shortcuts, and PIRL's argument for invariance over equivariance, from one of the people who built these methods.
🎬 TUM AI Lecture Series - On Removing Supervision from Contrastive Self-Supervised... (Alexei Efros) · Matthias NiessnerAlexei Efros, a coauthor of context prediction and colorization, on what self-supervision really learns and how much the data and augmentations do the supervising.