Skip to content
AI360XpertBeta
Core ML

Visual explainer

Contrastive Learning

Compare two views of one image against other batch views. Trace cosine scores, temperature, gradients and the limits of instance-based supervision.

A batch creates two views of each source image.
A batch creates two views of each source image.

Contrastive learning trains representations by identifying a related example among competing candidates. Our synthetic batch starts with two images, A and B. Each produces two augmented views: A1, A2, B1 and B2. Repeated triangles and squares identify the source image, not semantic class labels. The forked routes show which views share an original. Suitable transforms should preserve information useful for the intended task.

The mechanism follows SimCLR, which processes augmented views through a shared encoder and projection head. This sequence illustrates its objective, not measured training results.

Select The Partner

One anchor has one partner and two negative views.
One anchor has one partner and two negative views.

Choose A1 as the anchor. The solid route identifies A2 as its positive partner. Dashed routes identify B1 and B2 as competing negative views. A1 is excluded from its own candidate set. With NN original images, each anchor has one positive and 2N−22N-2 negatives. Here N=2N=2, so the denominator contains three candidates. Negative means another instance in this objective; it does not establish semantic unrelatedness.

Measure The Angles

Normalized vectors turn angles into cosine scores.
Normalized vectors turn angles into cosine scores.

Use unit vectors A1=(1,0)A1=(1,0), A2=(0.8,0.6)A2=(0.8,0.6), B1=(−0.8,0.6)B1=(-0.8,0.6) and B2=(−1,0)B2=(-1,0). The circle has radius one; every endpoint lies on it. With unit vectors, cosine similarity equals the dot product. The anchor's three scores are 0.8,−0.8,−10.8,-0.8,-1.

For temperature τ>0\tau>0, define sk=sim⁡(A1,k)s_k=\operatorname{sim}(A1,k) and

pk=exp⁡(sk/τ)∑j∈{A2,B1,B2}exp⁡(sj/τ),L=−log⁡pA2.p_k=\frac{\exp(s_k/\tau)}{\sum_{j\in\{A2,B1,B2\}}\exp(s_j/\tau)},\qquad L=-\log p_{A2}.

At τ=0.5\tau=0.5, the probabilities are approximately (0.936254,0.038164,0.025582)(0.936254,0.038164,0.025582) and L=0.065868L=0.065868. Natural logarithms are used. SimCLR averages the corresponding directed losses over anchors; this calculation isolates A1.

Follow The Loss Gradient

Loss gradients reward the partner and penalize competing scores.
Loss gradients reward the partner and penalize competing scores.

The bars show derivatives with respect to the three cosine scores, with zero on the horizontal baseline and positive upward. Each unit of derivative occupies 1,000 drawing units. The derivatives are

∂L∂sk=pk−1[k=A2]τ≈(−0.127491,0.076328,0.051164).\frac{\partial L}{\partial s_k}=\frac{p_k-\mathbf{1}[k=A2]}{\tau}\approx(-0.127491,0.076328,0.051164).

Gradient descent locally favours increasing the partner score and decreasing competing scores. These bars are not embedding displacement vectors. Shared encoder parameters and normalization couple the actual updates; literal independent springs would misrepresent that optimization.

Separate Features From Projections

The contrastive head can discard an encoder feature.
The contrastive head can discard an encoder feature.

The encoder produces a representation hh; a projection head gg produces the contrastive vector zz. To expose that distinction, assume hA2=(0.8,0.6,9)h_{A2}=(0.8,0.6,9) and the illustrative map g(a,b,c)=(a,b)g(a,b,c)=(a,b). Then zA2=(0.8,0.6)z_{A2}=(0.8,0.6), already normalized. The third coordinate disappears. All bars use 18 drawing units per feature unit.

This hand-chosen projector is not a trained SimCLR head. It shows why the two representations need not retain identical information. SimCLR evaluates encoder features before its projection head. Neither the loss nor this example guarantees semantic class clusters.

Count The Competing Views

More originals create more negative views per anchor.
More originals create more negative views per anchor.

A is the excluded anchor, P the partner and N a negative view. Two originals produce four views and two negatives per anchor. Four originals produce eight views and six negatives. Increasing a batch changes the contrast set and resource requirements; it is not a universal quality improvement. Batch size, transforms, training budget and the task must be evaluated together.

Watch For False Negatives

Different instances can become false negatives for a class task.
Different instances can become false negatives for a class task.

Now change one assumption: A and B are separate photographs of objects in the same supplied triangle class. The repeated triangle denotes class identity here; the distinct outer frames retain instance identity. Their dashed negative relation remains valid for instance discrimination but may conflict with a downstream goal of grouping that class. It encourages lowering their similarity, not necessarily sending them to opposite poles. Contrastive instance labels are not a semantic oracle.

Change Only Temperature

Temperature changes probability concentration for fixed scores.
Temperature changes probability concentration for fixed scores.

Return to the exact vectors and scores in frame 3. At temperature 1, A2 receives probability 0.731424; at 0.5 it receives 0.936254. The rest bars combine both negatives, and both panels use 200 drawing units per probability unit. The respective losses are 0.312761 and 0.065868. Lowering temperature sharpens this already-correct ranking. It does not fix a wrongly chosen positive or establish a better downstream representation.

Rank A Fresh Query

A fresh query can rank the learned vectors by cosine.
A fresh query can rank the learned vectors by cosine.

Introduce a fresh unit query Q=(0.6,0.8)Q=(0.6,0.8). Its dot products with A1, A2, B1 and B2 are 0.6,0.96,0,−0.60.6,0.96,0,-0.6. Bars extend right for positive scores and left for negative scores, using 180 drawing units per score unit. A2 ranks first. This is a constructed retrieval result on the stated coordinates, not evidence of semantic accuracy. Real retrieval needs representative queries and relevance judgments.

Where It Breaks

Identical features cannot distinguish the positive partner.
Identical features cannot distinguish the positive partner.

For a different limiting representation, map every view to the same unit vector (1,0)(1,0). Every similarity becomes 1. Each of the three candidates receives probability 1/31/3, independent of temperature, giving L=log⁡3≈1.098612L=\log3\approx1.098612. Equal bars use 150 drawing units per probability unit; their 50-unit heights depict the tie. Positive agreement alone cannot identify the partner. Negative competition makes this collapsed representation worse than our earlier fixture, although that comparison is not a guarantee that every optimization run avoids collapse.

The Quick Version

  • Two transformed views share their source image identity.
  • Exclude the anchor when forming contrastive candidates.
  • Unit-vector dot products give cosine similarities.
  • Loss derivatives favour partners over competing scores.
  • Encoder features and contrastive projections can retain different information.
  • More originals supply more competing negative views.
  • Different instances may share a downstream class.
  • Temperature changes concentration without correcting supervision.
  • Fresh queries can rank representations by similarity.
  • Identical features make the positive partner indistinguishable.