7 routes42 sections324 units

Unit 5.2 · about 4 h

201 / 324

Temperature, negatives and positive construction

Temperature and batch size set the difficulty of the discrimination game. The positive construction declares which variation the representation must ignore.

  • derive
  • measure

You can now

  • Derive the \(1/\tau\) sharpening of logits and gradients, and predict saturation at small \(\tau\).
  • Explain how batch size sets the negative count and when longer training closes the gap.
  • Build positives by augmentation composition and state the invariance each transform declares.

Covers

  • Temperature and batch-negative effects
  • Positive and negative construction

Study, in this order

3 steps

  1. Temperature and normalization
    SimCLR - A Simple Framework for Contrastive Learning of Visual Representations

    Section 5.1, Table 5: \(\tau\) 0.05, 0.1, 0.5 and 1.0 with L2 normalization; best Top-1 64.4 at \(\tau = 0.1\); the hard-negative weighting sentence

  2. Batch size and training length
    SimCLR - A Simple Framework for Contrastive Learning of Visual Representations

    Sections 2.2 and 5.2 with Figure 9: batch sizes 256 to 8192 with 16,382 negatives per positive at 8192; larger batches help early and the gap shrinks with longer training

  3. Augmentation composition
    SimCLR - A Simple Framework for Contrastive Learning of Visual Representations

    Section 3.1 with Figures 5 and 6: single transforms fail while crop plus color distortion wins; color histograms alone suffice as a shortcut

Focus on

  • Dividing by \(\tau\) multiplies every logit gap. Small \(\tau\) sharpens toward \(\operatorname{argmax}\); large \(\tau\) flattens toward uniform.
  • The positive-logit gradient is \(-(1 - p_{\text{pos}})/\tau\). Easy pairs saturate near zero gradient at small \(\tau\), so hard negatives drive learning.
  • Negatives per positive equal \(2(N-1)\): 510 at batch 256 and 16,382 at batch 8192. Longer training closes much of the batch gap.
  • Random crop plus color distortion wins because it blocks the color-histogram shortcut, not because either transform is strong alone.

Reproduce closed-book

Closed-book, write the NT-Xent gradient for the positive similarity. For similarities \(\left[\begin{smallmatrix}2&1&0\end{smallmatrix}\right]\), compute the \(\tau = 1\) softmax \(\left[\begin{smallmatrix}0.665&0.245&0.090\end{smallmatrix}\right]\) with gradient -0.335, and the \(\tau = 0.1\) gradient of about -0.0005.

Traps

  • Pushing \(\tau\) ever lower. Below 0.1 easy pairs saturate and a few hard negatives dominate; Table 5 drops from 64.4 at 0.1 to 59.7 at 0.05.
  • Assuming bigger batches always win. Figure 9 shows large gains at 100 epochs that mostly disappear with longer training on randomly resampled batches.
  • Letting labels choose positives and still calling the method self-supervised. SimCLR positives are two views of one image; once class labels pick positives, it is supervised contrastive learning and must be compared as such.

Practice

TensorTonic: Implement Symmetric CLIP Contrastive Loss

Named problem on the free language-modeling-from-scratch plan: implement the two-directional loss over an image-text similarity batch.

Transfer your NT-Xent derivation: CLIP swaps augmentation pairs for image-text pairs and averages both softmax directions.

Self-test

  1. Similarities \(\left[\begin{smallmatrix}2&1&0\end{smallmatrix}\right]\) at \(\tau = 1\): what is the positive probability?

  2. What happens to easy-pair gradients as \(\tau\) goes to 0?

  3. Batch 8192 gives how many negatives per positive, and why?

  4. Positive-identification accuracy is near perfect but linear evaluation is poor. What went wrong?

Completion criterion

Stop when

You predict \(\tau\)-sharpening and batch-size effects from the formulas, and your two-directional implementation passes the page tests.

Mark passed, then open 5.3 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • SimCLR - A Simple Framework for Contrastive Learning of Visual Representations
    Chen, Kornblith, Norouzi and Hinton (ICML 2020)requiredpaper
    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 foundations
    Simon J. D. Princeprimarytheory
    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 Transformer
    D2L authorsrequiredimplementation
    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 slice
    Stanford Universitysupportcourse
    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 loss
    TensorTonicsupportpractice
    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 Loss
    Deep-MLoptionalpractice
    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 Supervision
    Radford et al. (OpenAI, ICML 2021)requiredpaper
    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 Learning
    Chen and He (CVPR 2021)supportpaper
    Use Figure 2 and Table 1 for collapse under removed stop-gradient or predictor, including the per-channel spread diagnostic.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels