Self-supervised learning.
All five chapters, including worked answers and static lab comparisons. This edition loads no JavaScript.
01
Where targets come from
A learning signal does not have to arrive as a human-written category.
One image, two answer keys
Imagine handing a model the small arrow below. In a supervised direction-classification task, a person could supply the label “up.” The image is the input; “up” is the target, the answer against which a prediction is judged. Now cover part of the same image. The original pixels behind the cover provide another answer key, without anyone naming the arrow.
The pixels and the illustrative “up” label are invented. The right-hand target is extracted from the original, then withheld from the predictor’s input.
That change in the source of the answer is the central idea of self-supervised learning. In the approaches we will study, a training task constructs its targets or relationships from the data itself. BERT predicts selected text tokens; masked autoencoders reconstruct missing image content. We will start with a tiny image example so that the target is something you can inspect.
“Self” does not mean “no design”
A prediction is the model's proposed answer. A loss is a number that measures how well that answer satisfies the task. During training, an optimizer uses the loss to update the model's parameters. Self-supervision changes how the task gets its answer key; it does not remove the need to specify what counts as a good answer.
People still choose the data, transformations, model and objective. Text may be human-authored, and an image collection may be curated. “No category labels needed for this pretraining task” is therefore much more precise than “no human supervision anywhere.” A later application may use labeled examples to train or evaluate a classifier. The SimCLR evaluation protocol, for example, separates representation pretraining from labeled evaluation.
Learn something we can reuse
An encoder maps an input into a vector of features, called a representation. Think of the vector as an intermediate description that another task can use. Pretraining gives the encoder a task before that later, or downstream, use. A representation is useful only relative to what the downstream task needs to recover from it.
We will compare two ways to construct a pretraining task: hide content and predict it, or construct two related views and compare their representations. These are two important families, not an exhaustive definition of every self-supervised method.
Our arrow, pixel predictions and later vectors are invented teaching values. This guide computes losses; it does not train an encoder or measure feature quality. With basic arithmetic, you can follow where a target comes from, how one loss is calculated, and when a proposed transformation conflicts with a later task. First, let's make the hidden-pixel answer key explicit.
02
Predicting the missing part
The original data can supply the answer without giving that answer to the predictor.
Keep the answer on a separate path
Our invented image has binary pixels: 1 for an arrow pixel and 0 for the background. We hide the top-right region. The predictor would receive the remaining context and the positions of the missing pixels. A separate target path keeps the 16 original values for scoring.
Crossed cells are unavailable input values, not zeros. Gray prediction cells are prescribed 0.5 values—not output from a trained decoder. Only the 16 hidden positions are scored.
The distinction is about roles, not separate datasets. We can inspect the complete original to construct the exercise. The hypothetical predictor must not receive those hidden values as its input answer key. Otherwise, a low reconstruction error could come from copying the supplied answer rather than using the visible context.
What does a perfect score prove?
Check your understanding: why not feed the original patch to the predictor?
It could copy the 16 target values and obtain zero error without inferring anything missing. Keeping the scoring target separate from the input removes that direct shortcut in this toy. Zero error still would not, on its own, establish a useful downstream representation.
The strictly hidden mask here is a teaching simplification. Original BERT does not hide every selected target token: its corruption rule also includes random replacements and unchanged selected tokens. See BERT §3.1 for that distinction.
So far, the target has been missing content. Another route does not ask the model to reconstruct pixels. It asks which two views came from the same source.
03
Learning from two views
Common origin supplies a relationship. The pixels do not have to be identical.
Change the view, keep the source
Make two intensity variants of our arrow. Their pixels differ, but both were constructed from the same source image. We call this known relationship a positive pair. The pairing requires a record of common origin, not the semantic label “up.” Our intensity-only change is an illustrative augmentation, not SimCLR's complete transformation recipe.
In SimCLR's architecture, the two views pass through the same encoder. “Same” means shared parameters: changing an encoder weight changes the function used on both branches. A small network called a projection head then maps the encoder's features into the space used by the contrastive objective.
Here is a view, the encoder with parameters , and the projector with parameters . The encoder representation and projected representation are different objects. SimCLR applies its contrastive loss to and retains the encoder representation for downstream use.
A SimCLR-style schematic: fθ is the shared encoder, gφ the shared projection head. The objective compares projected z; the encoder’s h is retained for downstream use. Intensity-only views are illustrative. No encoder runs on this page.
Positive is a construction rule
Take one view as an anchor. Its paired view is the positive candidate. In the contrastive setup we will illustrate, views from other source images supply negative candidates. The objective compares the positive's similarity to the anchor with the competing similarities; matching the positive is not judged in isolation.
A negative here does not mean “known to belong to a different human class.” Two different images could both show upward arrows. Treating them as negatives can create a semantic false negative: the construction rule separates sources that a later task might want to group. Source identity and semantic category are not interchangeable.
Matching is not enough
Suppose every input receives exactly the same representation. Positive pairs match perfectly, but so does everything else. This is representation collapse: that representation preserves no distinction between inputs. The next chapter's three-candidate loss makes the problem concrete—equal scores give the positive only one third of the probability, not a perfect score.
Does every self-supervised method need negatives?
No. BYOL uses an online predictor, a moving-average target network and a stop-gradient target, without negative pairs in its objective. It is a different complete design. Its existence does not show that simply deleting negatives from this contrastive loss will produce useful learning, or that one component guarantees freedom from collapse.
Let's now isolate candidate competition. We will prescribe four tiny vectors directly, leaving the encoder out of the calculation so that every score is visible.
04
A loss that compares
Similarity becomes useful to this objective only in relation to the other candidates.
Read the geometry
The following vectors are hand-positioned toy representations, not encodings of our arrow. Fix the anchor at . Place the positive at angle on a unit circle, negative A at , and negative B at . Every vector has length one.
Cosine similarity measures alignment. For unit vectors it is simply their dot product, so the anchor reads each candidate's horizontal coordinate. The positive score is ; the two negative scores are and . Moving the positive toward the anchor increases its score. This uses SimCLR's cosine-similarity convention.
Turn three scores into one loss
Divide each score by a positive temperature , exponentiate, then normalize the three weights so they sum to one. This normalization is called softmax. Let be the sum of those weights:
The positive is included in the denominator; the anchor is not. Our single-anchor InfoNCE-style loss is the negative natural logarithm of the positive's probability, measured in nats. A larger means a smaller loss. This is a teaching subset, not the complete SimCLR minibatch objective. See CPC's candidate-classification formulation and SimCLR Eq. 1.
At and , the scores are . Their scaled values are . The resulting positive probability is about 0.705385 and the loss is 0.349012 nats. These are deterministic calculations from the stated toy values.
Try a sharper comparison
Predict first: at , would reducing temperature from 1 to 0.1 lower the positive's loss? Set that angle, change only temperature, and watch the probability bars and loss. The vectors and their raw similarities should stay still.
Geometry becomes a loss.
Toy: hand-positioned unit vectors, not representations learned from the arrow. Three candidates, one anchor, no training.
Before you touch the controls
At an angle of 120°, Negative A scores higher than the positive. Predict: will reducing temperature from 1.0 to 0.1 lower the positive’s loss?
Try those two settings. Then read the explanation below the lab.
● Positive ▷ Anchor □ Negative A ◇ Negative B
Axes x and y are vector coordinates. All vectors have length 1. The positive’s horizontal projection is its similarity to the anchor.
At 60° and temperature 0.5, the positive receives 0.705 of the candidate probability. Loss is 0.349 nats. The positive has the highest similarity.
| Candidate | Similarity s | Scaled s / τ | Probability |
|---|---|---|---|
| Positive | 0.500 | 1.000 | 0.705 |
| Negative A | 0.000 | 0.000 | 0.259 |
| Negative B | -1.000 | -2.000 | 0.035 |
The positive is in the denominator; the anchor is not. These probabilities are relative to this candidate set, not confidence in an image category. Values round to three decimals.
Read the same experiment without sliders
| Angle | Temperature | Positive p | Loss, nats |
|---|---|---|---|
| 60° | 0.5 | 0.705 | 0.349 |
| 0° | 0.5 | 0.867 | 0.143 |
| 120° | 1.0 | 0.307 | 1.180 |
| 120° | 0.1 | 0.007 | 5.007 |
60° → 0° at τ = 0.5: closer positive, lower loss. At 120°, τ = 1.0 → 0.1: the higher-scoring negative takes nearly all the probability, so positive loss increases.
Separate hypothetical: if every representation were identical, all scores would tie. Then positive probability is 1/3 and loss is ln(3) ≈ 1.099 at any positive temperature.
Lower temperature sharpens score differences, favoring the highest-scoring candidate. At 120°, negative A scores 0 while the positive scores −0.5. In this specific comparison, sharpening raises the positive's loss from about 1.180270 to 5.006760 nats. Lower temperature does not universally lower positive loss; the effect depends on the scores and temperature.
These probabilities are relative weights among these three candidates—not calibrated confidence that an image belongs to a semantic category. Nor does moving a slider train a model: angle directly changes the prescribed geometry, while temperature changes only the score-to-probability calculation.
What if every score ties?
Transfer: all three candidate scores are 0. What can temperature change?
Nothing about their relative weights. Each exponential is , so and for every positive temperature. Any three equal scores give this result. Identical representations are one way to produce equal scores; tying just two points in the plot is not full collapse.
This arithmetic explains what the objective scores, not a guarantee about where training will converge. Even a low loss leaves another question unanswered: what information will the representation retain for a later task?
05
What the representation keeps
A useful invariance for one task can erase the answer to another.
A transformation makes a promise
Constructing two views as a positive pair asks the objective to treat them as related despite their differences. The transformation therefore expresses a choice about which changes should matter less. Invariance means a representation remains unchanged under a transformation. Whether that is useful depends on the later task—not just whether the transformed image looks plausible. SimCLR's augmentation discussion makes these task-defining choices explicit.
Suppose our downstream task is to distinguish upward from downward arrows. A brightness change keeps the direction. A 180° rotation reverses it. These transformations cannot carry the same promise for that task.
Invented direction task. Brightness preserves its target; a half-turn changes it. Rotation can be sensible for other tasks. The question is which distinctions the downstream representation must keep.
Follow the information
Transfer: should up and down be forced into the same frozen representation?
No, if a later head must distinguish their directions using only that representation. Suppose the frozen encoder produces exactly the same vector for an upward arrow and its downward rotation. A deterministic head receives identical inputs and must return identical outputs. Since the two targets differ, it cannot classify both correctly. Brightness invariance does not create this particular conflict because the direction target stays the same.
This is an exact logical counterexample, not a measured failure rate or a universal ban on rotations. It assumes identical frozen encoder representations. Approximate similarity needs empirical analysis. And remember the two spaces in SimCLR: an invariance in projected does not automatically prove that encoder features have lost the same information.
Test the representation you will use
One evaluation route is a linear probe: freeze the encoder, compute , and train a linear classifier on labeled examples. Only the classifier's parameters change. This tests how accessible the task information is to that linear head, not everything any possible decoder could recover. It follows the linear evaluation protocol described in SimCLR.
A different route is fine-tuning: update some or all pretrained encoder parameters for the downstream task, usually along with a task-specific head. BERT's downstream procedure illustrates this distinction. Fine-tuning can change the representation; a frozen-encoder information argument does not describe those updated features.
For either route, specify the target, training data and held-out evaluation data. Check the representation actually used downstream rather than treating low pretraining loss as a substitute for that evaluation. No probe, fine-tuning run or downstream benchmark has been performed by this guide.
The core idea is now compact: construct a learning signal from the data, optimize for that signal, then test what transfers. When you meet a new self-supervised method, ask three questions: Where does its target come from? What distinctions does its objective preserve or suppress? How will the downstream task check that choice?
Glossary
- Target
- The answer or relationship used to score a prediction. It need not be a human category label.
- Self-supervised pretraining
- Learning with targets or relationships constructed from the observed data before a downstream use.
- Encoder / representation
- A function mapping an input to features; the resulting feature vector. Here .
- Projection head
- A network mapping encoder features to . SimCLR applies its contrastive objective in this projected space.
- Augmentation / view
- A transformation of an example, and the resulting input. Which changes preserve useful information is task-dependent.
- Anchor, positive, negative
- The reference view and its candidates. Here the positive shares a source with the anchor; negatives come from other sources, not necessarily other semantic classes.
- Cosine similarity
- Alignment between vectors: . For unit vectors, it is the dot product.
- Softmax / temperature
- Exponentiation and normalization of scores into weights that sum to one. Temperature divides scores before exponentiation.
- Nat
- The loss unit here because the objective uses the natural logarithm.
- Collapse
- The loss of distinctions when inputs receive identical representations. A tie between two candidates alone is not full collapse.
- Invariance
- A representation remaining unchanged under a transformation.
- Linear probe / fine-tuning
- A probe freezes the encoder and trains a linear head. Fine-tuning updates pretrained parameters for the downstream task.
Sources
- BERT — Devlin et al., NAACL 2019
Sections 3.1–3.2: selected-token prediction and labeled downstream fine-tuning. Original BERT does not mask every selected target. Claims CL-001, CL-002, CL-010.
- Masked Autoencoders — He et al., CVPR 2022
Section 3: masking and reconstruction loss over masked patches; section 4: evaluation. Our fixed binary block-mask example is not a reproduction. Claims CL-001–CL-003, CL-012.
- SimCLR — Chen et al., ICML 2020
Section 2.1, Figure 2 and Eq. 1: views, shared encoder, projector and contrastive loss. Sections 2.3 and 3: evaluation and augmentations. Claims CL-001, CL-004–CL-006, CL-010–CL-011.
- Contrastive Predictive Coding — van den Oord et al., 2018
Section 2.3, Eq. 4: the InfoNCE candidate-classification formulation. Inspected version 2, 2019. The toy does not establish CPC sampling assumptions or a mutual-information guarantee. Claim CL-005.
- BYOL — Grill et al., NeurIPS 2020
Section 3.1: a negative-free objective with online predictor, moving-average target network and stop-gradient target. Not a guarantee that arbitrary positive-only objectives work. Claims CL-008–CL-009, CL-011.