Unit 1 of 7 · about 3 hours · derive

Embedding geometry and contrastive logits

Contrastive objectives compare directions, not positions. If norms leak into the logits, the loss rewards loud embeddings instead of aligned ones.

You'll be able to

  • Explain why cosine similarity keeps angles and discards norms, and when that helps retrieval.
  • Derive NT-Xent as softmax cross-entropy over the other views, with logit scaling.
  • Predict the loss for degenerate batches, including the uniform case.

Covers

Embeddings, dot products, cosine geometry and normalizationMetric learning and contrastive logits
1

Read

In this order, only the parts listed

0 of 2 done

  1. NT-Xent and cosine similaritySimCLR - A Simple Framework for Contrastive Learning of Visual Representations

    Section 2.1, Eq. (1): as the L2-normalized dot product and with the sum over ; Algorithm 1 for the full loop

  2. Why normalization mattersSimCLR - A Simple Framework for Contrastive Learning of Visual Representations

    Section 5.1, Table 5: L2 Yes versus No rows, with the sentence that normalization plus temperature weights examples by hardness

    Skip the temperature-value comparison; the next unit owns .

2

Focus on

  • Cosine similarity is a dot product after L2 normalization. Positive scaling of either vector changes nothing.
  • NT-Xent treats the other views as classes. The positive stays in the denominator, so the loss is a valid cross-entropy.
  • With identical embeddings every row is uniform at any , so the loss equals : for .
  • Without normalization, one large-norm embedding dominates every softmax row it appears in.
Common mistakes · 3
  • Calling cosine similarity translation-invariant. It is invariant to positive scaling, not to shifts; centering the batch changes all angles.
  • Dropping the positive from the denominator. SimCLR sums over , which includes the positive; without it the loss can go negative.
  • Reading NT-Xent as fixed-class classification. The classes are the current batch views, so the same pair gets a different loss in different batches.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive from softmax cross-entropy over pairwise similarities. Then compute the loss by hand for embeddings , , , and get 0.5514.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Embeddings , , , at give loss 0.5514. Show the per-row terms.Answer

    For row 0 the positive scores 1 and the two negatives score 0, so . Rows 1, 2 and 3 match by symmetry, and the mean over all four rows is 0.5514.

  2. 2.All four embeddings are identical. What is the loss at ?Answer

    . Every row is uniform over its three candidates, so each row contributes .

  3. 3.Why does SimCLR Table 5 favor L2 normalization?Answer

    Raw dot products mix angle with norm, so loud vectors dominate the softmax. With normalization plus temperature the loss weights examples by relative hardness, and Top-1 rises from 57.2 to 64.4 at the best setting. Without normalization the contrastive accuracy is higher (91.7 versus 87.8) yet the representation is worse, so the pretext score does not measure representation quality.

  4. 4.Is the positive pair part of its own denominator?Answer

    Yes. The sum runs over all including the positive index , which keeps the loss a proper cross-entropy bounded below by 0.

5

Practice

  • Deep-ML 384: Contrastive Loss (InfoNCE / SimCLR-style)

    Implement contrastive_loss(embeddings, temperature). The visible tests check two identical pairs at for 0.5514 and four identical embeddings for 1.0986.

    Then add local tests for temperature extremes, a missing positive, duplicate pairs, and normalized versus unnormalized inputs.

You're done when

You derive NT-Xent without notes, reproduce 0.5514 by hand, and your implementation passes the Deep-ML tests plus the four local edge tests.

Only when you can do this without notes.

Leads to

Notes

Resources · 1 for this unit, 7 more in the section
  • SimCLR - A Simple Framework for Contrastive Learning of Visual RepresentationsChen, Kornblith, Norouzi and Hinton (ICML 2020) · required

    Use Section 2.1 Eq. (1) and Algorithm 1 for NT-Xent, 2.3 for the linear-evaluation protocol, 3.1 with Figures 5-6 for augmentation composition and shortcuts, 4.2 for the projection head, and 5.1-5.2 with Table 5 and Figure 9 for temperature and batch-size effects.

Also in this section

  • Understanding Deep Learning - representation foundationsSimon J. D. Prince · primary

    Use 9.3.6-9.3.7 for transfer and self-supervision, 12.9-12.10 for image transformers, and 13.1-13.4 plus 13.10 for GNN vocabulary. Open notebooks 9.4, 12.4 and 13.1 only when a figure or mechanism is unclear.

  • D2L - Vision TransformerD2L authors · required

    Use vision-transformer.md for patch extraction, patch embeddings and a tested small encoder. Add a compact SimCLR-style or paired-data contrastive objective in the explicit training loop from Sections 2 and 3.

  • Stanford CS231n - transformer and self-supervised assignment sliceStanford University · support

    Use only the Assignment 3 components for transformers, self-supervised learning, CLIP or DINO that directly test representation and pairing. Keep the controlled local evaluator as the gate.

  • TensorTonic - patchify and symmetric CLIP lossTensorTonic · support

    Solve only Patchify a Batch of Images and Implement Symmetric CLIP Contrastive Loss. Transfer both into the controlled experiment and preserve local leakage and collapse tests.

  • Deep-ML candidate - InfoNCE LossDeep-ML · optional

    If the platform admission check passes, use problem 384 once after deriving the objective. Add local tests for temperature extremes, missing positives, duplicate pairs and normalized versus unnormalized embeddings.

  • CLIP - Learning Transferable Visual Models From Natural Language SupervisionRadford et al. (OpenAI, ICML 2021) · required

    Use Section 2.3 for the symmetric image-text objective and learned temperature, 2.5 for batch size and temperature limits, and 3.1.2 with 3.1.4 for zero-shot transfer and prompt ensembling.

  • SimSiam - Exploring Simple Siamese Representation LearningChen and He (CVPR 2021) · support

    Use Figure 2 and Table 1 for collapse under removed stop-gradient or predictor, including the per-channel spread diagnostic.