Quiz: contrastive learning
Before this unit: Pretext tasks and masked image modelling (previous unit); Day 41: Modern deep learning; Day 37: Training networks in practice.
See also: Denoising and what you may measure (day 5).
Short questions on views, the InfoNCE loss, augmentations and collapse. Try each on paper first, then open the answer.
Q1. In contrastive learning, what plays the role of the label?
Which two embeddings should agree?
Which views come from the same image: the two augmented views of one image are a positive pair, all other views in the batch are negatives. No human label is needed.
Q2. A batch has 128 images. An untrained network gives every view the same embedding. What is the InfoNCE loss?
Every candidate gets the same probability.
\ln 128 = 4.85: the partner has probability 1/128, as for guessing.
Q3. You lower the temperature τ. Which negatives matter more, and which less?
The push of negative j has the weight p_j of a softmax of similarity / τ.
The softmax gets sharper: the most similar (“hardest”) negatives get almost all of the weight, the far ones almost none.
Q4. You want features that tell cells in G1 from cells in G2 using a DNA stain. Which augmentation must you leave out?
What distinguishes G1 from G2 in a DNA stain?
Intensity augmentations (gain, gamma, per-image normalisation): G2 cells have twice the DNA, so the difference is mostly the integrated intensity. An intensity augmentation teaches the network to ignore exactly that.
Q5. Why does training with only “pull the two views together” fail?
What is the cheapest way to make every pair agree?
Collapse: the same output for every image makes every pair agree perfectly. The negatives make this the worst solution (loss ln N).
Q6. Two training runs: A reaches a loss of 2.5, B a loss of 3.3. Can you conclude that A’s features are better?
What else changes the difficulty of the task?
No. The loss depends on the augmentations, the batch size and τ: weak augmentations make the task easy and the loss low, with worse features (the demo: no augmentation ends at 2.46 with 58 % shape accuracy, full augmentation at 3.34 with 100 %). Evaluate features with a probe or nearest neighbours on labelled data.
Q7. Your negatives come from many plates; the features end up sorting cells by plate. What happened?
What makes two crops from one plate easy to tell apart from crops of another plate?
A batch effect: illumination, background or stain batch differ between plates and survive the augmentations, so they are an easy way to find the partner. Take the negatives from the same plate, augment the plate differences away, or correct the features per plate.
Q8. (math) Show that the InfoNCE term of one view is the cross-entropy of a softmax classifier, and give its gradient with respect to the scores.
Scores s_j = \mathbf z^\top\mathbf z_j'/\tau; the correct class is the partner.
\mathcal L = -\ln\big(e^{s_i}/\sum_j e^{s_j}\big) is -\ln p_\text{true class} for the softmax over the N candidates, with the partner as the true class. Its gradient is \partial\mathcal L/\partial s_j = p_j - [j = i]: predicted minus wanted.