Log inStart free
LESSON 5.10 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Contrastive losses

Watch matching image and caption points pull together while the rest push apart.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 5.10 · LOSS BAYBATCH 4 · STEP 20 · τ 0.5
01
Stage 1 of 6 · The batch
A batch holds 4 image–caption pairs. Two encoders map each one to a point on a circle.
Explain it simply

Each dot is an image and each square is a caption. An encoder, a model that turns an input into numbers, places each one as a direction on the circle. A line joins each image to its own caption. Training wants each pair to point the same way.

4 images as dots and 4 captions as squares on a circle. Image 1 is at 63 degrees, caption 1 at 46 degrees.

image (dot) · caption (square)margin · stepscore s · softmax ploss · push and pulleven softmax log N
12341234image 1 θ 63°caption 1 φ 46°s 0.96image 2 θ 94°caption 2 φ 79°s 0.97image 3 θ 289°caption 3 φ 296°s 0.99image 4 θ 184°caption 4 φ 189°s 1.00the batch on a circlepairs in the batchs₁₁ = cos(63° − 46°) = 0.9612341234image 1 θ 63°caption 1 φ 46°s 0.96image 2 θ 94°caption 2 φ 79°s 0.97image 3 θ 289°caption 3 φ 296°s 0.99image 4 θ 184°caption 4 φ 189°s 1.00the batch on a circlepairs in the batchs₁₁ = cos(63° − 46°) = 0.96
Toy: 2, 4 or 8 image–caption pairs on a circle
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
sij=cos⁡(θi−φj)\sy{s}{out}{s_{ij}} = \cos(\sy{theta}{data}{\theta_i} - \sy{phi}{data}{\varphi_j})
s11=cos⁡(63∘−46∘)=0.96s_{11} = \cos(63^\circ - 46^\circ) = 0.96

The score is the cosine of the angle between two points.

Common mix-ups

MYTHContrastive training needs hand-labelled negative pairs.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With 8 pairs in the batch instead of 4, does the loss go up or down?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.