AI Grounds

Open AI Grounds on a desktop

These interactive lessons need a larger screen. Please continue on a desktop or laptop computer.

Guided discovery

Contrastive Loss Lab

Move pairs. Watch the margin stop the penalty.

Your dataset

Three hand-authored one-dimensional embeddings are numeric coordinates. A is the shared anchor; P its similar partner, N its dissimilar partner. Pair A–P has fixed label Y=0 (similar), and A–N fixed label Y=1 (dissimilar). Positive and negative describe pair roles, not coordinate signs or probabilities. Only these two pairs are scored; P–N is not.

Values
A–P
d=20
A–N
d=10

Focus a dot and use arrow keys. Shift moves by 10; Home / End moves to 0 / 100.

Minimum separation that removes the dissimilar-pair penalty, in coordinate units. This parameter edit keeps all points fixed. Use arrow keys on the slider. Press Enter or leave the number field to apply an exact edit.

Near negative · A=40, P=60, N=50 · Margin=30 · Selected P · Negative pair inside margin

Selecting or focusing a point only inspects and preserves answers. Actual coordinate or margin changes clear stale explanations. Coincident points occupy separate handle rows without changing their numeric positions.

Two labeled pairs

Distances are absolute coordinate differences. Y=0 chooses the similar penalty; Y=1 the dissimilar penalty. The margin shortfall is used only by the dissimilar term.
PairRelationshipYDistanceShortfallPenaltyCalculation
A–PSimilar020Not used200½ × 20² = 200
A–NDissimilar11020200½ × max(0, 30 − 10)² = 200

Mean pair loss = (200 + 200) / 2 = 200. Include both terms, even a zero term.

Distance and Margin use toy coordinate units; penalties and their mean use squared coordinate units. They are not probabilities, confidence scores or nats. At or beyond the margin the dissimilar penalty is zero, with no further loss reward for increasing that distance.

Positive penalty

Squared separation cost for the similar pair A–P.

200

½ × 20²

Zero only when this similar pair coincides.

Negative penalty

Squared margin shortfall for the dissimilar pair A–N.

200

½ × max(0, 30 − 10)²

Zero at and beyond the margin; never negative.

Mean pair loss

Average of exactly these two authored pair penalties.

200

(200 + 200) / 2

Moving shared anchor A can change both terms.

Construction and limits

This page scores scalar toy embeddings rather than learning an encoder from data. A real shared encoder maps inputs to embeddings through shared parameters; manual point edits here isolate the objective. Only A–P and A–N are scored. The fixed pair labels represent authored similarity relationships; distance does not relabel a pair.

Each coordinate is an integer 0..100. Margin is strictly positive, 10..100 in steps of 10. One-dimensional Euclidean distance is |a−b|. The convention is Y=0 similar, Y=1 dissimilar, with factor ½ in each term. Other implementations may reverse label coding or omit that factor; inspect their declared formula. The reference sums pair losses; this page also explicitly averages its two selected terms.

Collapsed puts all points at 40 with Margin 30: the similar penalty is zero, dissimilar penalty ½×30²=450 and mean 225. The negative term penalizes collapse here. The page draws no gradient arrows and makes no claim about a unique descent direction at a coincident dissimilar pair. Zero loss on two authored pairs does not establish learned semantics or generalization.

This margin objective is distinct from triplet loss and from temperature-based multi-negative softmax objectives. No temperature, normalization, probability model, optimizer or automatic training is used. Coordinates, labels and the chosen pair set define every displayed value. Prediction and Reset restore Near negative for the current experiment; free-exploration Reset begins Experiment 1.

Hadsell, Chopra & LeCun · Contrastive loss, Equation 4