6.5.4 · D2Research Frontiers & Practice

Visual walkthrough — Self-supervised and contrastive learning (SimCLR, CLIP)

2,355 words11 min readBack to topic

This page is the visual companion to Self-supervised and contrastive learning (SimCLR, CLIP). If you have seen Metric learning and embedings the idea of "distance between points that mean things" will feel familiar; if not, we build it here from scratch.


Step 1 — A representation is just an arrow to a point

WHAT. A neural network takes an image and spits out a list of numbers, like . We are going to use only 2 numbers so we can draw it. A list of 2 numbers is a point on a flat sheet of paper — or equivalently an arrow from the centre (called the origin) out to that point.

WHY. Everything in contrastive learning is about where points land. Before we can say "pull these together, push those apart", we need a place where points live. That place is called embedding space, and here it is just the 2D page you are looking at.

PICTURE. Look at the figure. Three images (a cat photo, a second crop of the same cat, and a dog) each become one arrow. The symbol means "the arrow for one image". A little superscript will soon tell us which kind of image it is.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

Step 2 — We only care about direction, so we measure angle

WHAT. Take two arrows and . We ask: do they point the same way? The single number that answers this is the cosine of the angle between them:

Term by term:

  • (say "u dot v") — multiply matching numbers and add: . This is the dot product. It is big when the arrows lean the same way.
  • (say "length of u") — how long the arrow is, . Dividing by the two lengths cancels length out, leaving only direction.
  • — the cosine of the angle. It runs from (same direction, ) through (perpendicular, ) down to (opposite, ).

WHY this tool and not plain distance? We could measure straight-line distance between the two points. But a cat photo and its brighter crop might land at different distances from the origin (one arrow longer than the other) while still meaning the same thing. Direction survives brightness/scale changes; length does not. So we throw length away on purpose and keep the angle. That is why cosine, not Euclidean distance.

PICTURE. Two arrows and the wedge between them. As shrinks to , climbs to ; as they splay apart, it falls. The green wedge is small for the cat–cat pair, wide for the cat–dog pair.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)
similarity of two identical-direction vectors
similarity of two opposite vectors

Step 3 — Name the players: anchor, positive, negatives

WHAT. Pick one arrow and call it the anchor, written . The arrow that should be its friend (a different crop of the same image) is the positive, written . Every other arrow in the batch is a negative, written , where counts them.

WHY. We cannot say "pull together" and "push apart" until we have named what goes where. The plus and minus superscripts are the whole vocabulary: = "belongs with me", = "does not".

PICTURE. One blue anchor arrow. One orange positive nearby (small angle — good). Several red negatives fanned out at wide angles. This is the entire cast of the loss function on one page.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

Step 4 — Turn "similar or not" into a guessing game (softmax)

WHAT. Line up similarity scores: one for the positive, , and one for each negative, . Now imagine a quiz: "Which of these arrows is the true partner of the anchor?" We want the model to point confidently at the positive.

To turn scores into a probability of guessing correctly, we exponentiate each score and divide each by the total. This machine is called softmax:

Term by term:

  • — the exponential . It is always positive and it exaggerates gaps: a score of becomes much bigger than a score of . That is exactly the "amplify the winner" behaviour we want. (We use and not, say, squaring, because it turns addition of scores into multiplication of probabilities and never goes negative — perfect for probabilities.)
  • numerator — the true partner's exponentiated score.
  • denominator — the true one plus all imposters, so the fraction is a genuine probability between and (it can never exceed the whole sum it sits inside).

WHY a probability? Because now we can grade the model with one clean rule: reward it for putting probability on the truth. That converts a fuzzy geometric wish ("point the same way") into a single number to optimise.

PICTURE. A bar chart: the tall orange bar is the positive's score; short red bars are negatives. Softmax = "orange bar's height ÷ total height of all bars". You can literally see the probability as the orange slice of the whole.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

Step 5 — The temperature knob : how picky is the quiz?

WHAT. Before exponentiating, divide every similarity by a small positive number (Greek "tau"), the temperature:

Term by term:

  • — divides each score. Because is small (like ), dividing magnifies every score. A gap of between positive and negative becomes a gap of after dividing by — huge once exponentiated.

WHY. controls how sharp the softmax is:

  • Small (): scores get stretched apart → the winner dominates → the model is forced to be very confident and pays extra attention to the hardest (closest) negatives.
  • Large (): scores stay close → probabilities flatten → all negatives treated gently.

PICTURE. The same bar chart at three temperatures. At the bars are comparable (soft). At the orange bar towers over a flattened field of red (sharp). One knob, sliding the whole distribution between "relaxed" and "ruthless".

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)
if is very small, the model focuses on...
the hardest (most similar) negatives

Step 6 — Grade the guess:

WHAT. We have a probability of guessing the positive. A perfect model gives ; a clueless one gives near . We turn "how good is this probability?" into a loss to minimise by taking the negative logarithm:

Term by term:

  • — the logarithm. As goes from down toward , goes from down toward .
  • the minus sign — flips it so loss is when (perfect) and grows huge as (terrible). Minimising this loss is maximising the probability.

WHY and not ? The log punishes confident mistakes brutally: being sure and wrong ( tiny) sends the loss toward infinity, producing a big corrective gradient. A plain tops out at and barely nudges. Log also turns the product of many examples' probabilities into a clean sum, which is numerically stable and easy to differentiate.

PICTURE. The curve from to : it kisses zero at the right (perfect) and rockets up on the left (disaster). A dot slides down the curve as training improves.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

Assembling Steps 4–6 gives back exactly the parent's formula: Nothing here is arbitrary anymore — every piece answered a question.


Step 7 — Watch one gradient step move the arrows

WHAT. Plug in numbers like the parent's worked example. Anchor–positive similarity , six negatives around , temperature .

  • Numerator: .
  • Denominator: (six negative terms each) .
  • Probability , loss .

WHY show the geometry? Because minimising that does something you can draw: gradient descent (see Batch normalization and training techniques for the training machinery) rotates the anchor toward the positive and away from each negative, proportional to how confidently each was confused.

PICTURE. Before vs after one update. The blue anchor swings toward the orange positive (angle shrinks, rises) and the red negatives drift to wider angles. This is "pull together, push apart" made mechanical.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

Step 8 — Edge and degenerate cases (never get surprised)

WHAT & WHY. A good understanding covers what happens at the extremes:

  1. Zero negatives (). Denominator = numerator, so , loss always. Nothing to learn — the model can place all arrows identically and never be corrected. Lesson: negatives are what create the "push apart" force; that is why Metric learning and embedings and SimCLR crave many negatives (large batches).
  2. Positive collapses onto a negative (some points the same way as ). Then two big terms fight in the denominator, drops, loss spikes — the loss complains loudly, exactly the corrective signal we want.
  3. All arrows identical (total collapse). Every similarity equals after scaling, positive and negatives indistinguishable, , loss . The loss stays positive, so gradient descent is pushed to break the tie — good, collapse is penalised.
  4. Temperature . Softmax becomes a hard "winner-takes-all": if any negative outscores the positive even slightly, loss explodes. Too aggressive — training gets unstable. This is why lives in , never at .

PICTURE. Four mini-panels, one per case: (a) no negatives → flat zero force; (b) a red arrow overlapping the orange → alarm; (c) all arrows stacked → penalised; (d) knife-edge.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

The one-picture summary

Everything on this page collapses into a single pipeline: arrows → angles → softmax quiz → grade → gradient that rotates arrows.

Figure — Self-supervised and contrastive learning (SimCLR, CLIP)

image x

encoder f gives arrow z

cosine sim measures angle

divide by tau and exp

softmax gives probability p

minus log p is the loss

gradient rotates arrows

Recall Feynman retelling — say it out loud

Every picture becomes an arrow. I only care which way arrows point, so I measure the angle between them with cosine — same direction gives , opposite gives . I pick one arrow as the anchor, one true friend (the positive) and a crowd of strangers (negatives). Then I play a quiz: which arrow is the anchor's real friend? Softmax turns the angle-scores into a probability of guessing right, and the temperature decides whether the quiz is easygoing or ruthless. I grade the guess with : perfect probability scores zero loss, a confident wrong guess scores near-infinity. Minimising that loss physically rotates the anchor toward its friend and away from the strangers. Take away the negatives and there is no push, so nothing is learned — which is why big batches matter. Set too low and the quiz becomes a hair-trigger and training wobbles. That whole chain — arrow, angle, softmax, , rotate — is contrastive learning.

Recall Rebuild the formula from scratch

Start from cosine similarity. Divide by . Exponentiate. Put the positive on top, the positive-plus-all-negatives on the bottom (a softmax). Take . Done — that is InfoNCE.

See also: Representation learning · Transfer learning and fine-tuning · Zero-shot and few-shot learning · Multi-modal learning