6.5.4Research Frontiers & Practice

Self-supervised and contrastive learning (SimCLR, CLIP)

3,261 words15 min readdifficulty · medium

What is Self-supervised Learning?

Key distinction:

  • Unsupervised learning: Find structure (clustering, density estimation) with no labels
  • Self-supervised learning: Create pseudo-labels from data structure, then train with those
  • Supervised learning: Use human-provided labels

Why does this work?

To solve pretext tasks well, the model must learn general features about the data. If you train a model to predict whether two image crops come from the same photo, it must learn:

  • Object parts and boundaries
  • Colors, textures, shapes
  • Spatial relationships

These representations transfer to downstream tasks like classification or detection.

What is Contrastive Learning?

The contrastive objective mathematically:

For anchor sample xx, positive x+x^+ (augmented/related view), and negatives {xi}\{x^-_i\}:

Lcontrastive=logexp(sim(z,z+)/τ)exp(sim(z,z+)/τ)+i=1Nexp(sim(z,zi)/τ)\mathcal{L}_{\text{contrastive}} = -\log \frac{\exp(\text{sim}(z, z^+) / \tau)}{\exp(\text{sim}(z, z^+) / \tau) + \sum_{i=1}^{N} \exp(\text{sim}(z, z^-_i) / \tau)}

where:

  • z=f(x)z = f(x) is the learned representation
  • sim(u,v)=uvuv\text{sim}(u, v) = \frac{u^\top v}{\|u\| \|v\|} is cosine similarity
  • τ\tau is a temperature parameter (controls concentration)

Why this form? This is the InfoNCE loss (Noise Contrastive Estimation). Let's derive why it makes sense:

  1. Goal: Make positive pair similar, negatives dissimilar
  2. Softmax over similarities: Treat it as classifying the positive among N+1 choices
  3. Temperature τ\tau: Smaller τ\tau → sharper distribution, model must be more confident
  4. Logarithm: Convert products to sums, stabilize gradients

The denominator normalizes so the loss interprets as "negative log probability of identifying the positive sample."

SimCLR: Simple Framework for Contrastive Learning

The SimCLR Recipe (4 key ingredients)

1. Data Augmentation Create two random augmented views x~i\tilde{x}_i and x~j\tilde{x}_j from image xx:

  • Random crop + resize
  • Color distortion (brightness, contrast, saturation, hue)
  • Gaussian blur
  • Random grayscale

Why? Strong augmentation creates challenging positive pairs. The model can't cheat by memorizing pixels; it must learn semantic features invariant to these transforms.

2. Base Encoder A neural network f()f(\cdot) (typically ResNet) that maps images to representations: hi=f(x~i)Rdh_i = f(\tilde{x}_i) \in \mathbb{R}^d

3. Projection Head A small MLP g()g(\cdot) that maps representations to a space where contrastive loss is applied: zi=g(hi)=W(2)σ(W(1)hi)R128z_i = g(h_i) = W^{(2)} \sigma(W^{(1)} h_i) \in \mathbb{R}^{128}

Why a projection head? Loss is applied to zz, but we save hh for downstream tasks. This separation improves performance (empirically shown in the SimCLR paper). The projection head can contain information useful for the pretext task but not for transfer.

4. Contrastive Loss For a batch of NN samples, we get 2N2N augmented views. For each positive pair (i,j)(i, j):

i,j=logexp(sim(zi,zj)/τ)k=12N1kiexp(sim(zi,zk)/τ)\ell_{i,j} = -\log \frac{\exp(\text{sim}(z_i, z_j) / \tau)}{\sum_{k=1}^{2N} \mathbb{1}_{k \neq i} \exp(\text{sim}(z_i, z_k) / \tau)}

The final loss averages over all positive pairs: L=12Nk=1N[2k1,2k+2k,2k1]\mathcal{L} = \frac{1}{2N} \sum_{k=1}^{N} [\ell_{2k-1, 2k} + \ell_{2k, 2k-1}]

Why this formulation? Each augmented view serves as anchor once. We treat all other 2N2N- samples in the batch as negatives (all images in the batch are different, except the paired augmentation).

Step 1: Sample 4 images: cat, dog, car, tree
Step 2: Augment each twice → 8 views: cat₁, cat₂, dog₁, dog₂, car₁, car₂, tree₁, tree₂

Step 3: Encode through ff and gg:

  • zcat1,zcat2,zdog1,zdog2,..z_{\text{cat}_1}, z_{\text{cat}_2}, z_{\text{dog}_1}, z_{\text{dog}_2}, ..

Step 4: Compute loss for anchor cat₁:

  • Positive: cat₂ (sim = 0.85)
  • Negatives: dog₁, dog₂, car₁, car₂, tree₁, tree (sims ≈ 0.1-0.3)

cat1,cat2=loge0.85/0.5e0.85/0.5+e0.2/0.5+e0.15/0.5+...+e0.25/0.5\ell_{\text{cat}_1, \text{cat}_2} = -\log \frac{e^{0.85/0.5}}{e^{0.85/0.5} + e^{0.2/0.5} + e^{0.15/0.5} + ... + e^{0.25/0.5}}

With τ=0.5\tau=0.5, numerator e1.7=5.47\approx e^{1.7} = 5.47, denominator 5.47+1.49+...15\approx 5.47 + 1.49 + ... \approx 15, so loss log(5.47/15)1.01\approx -\log(5.47/15) \approx 1.01.

Why this step? Softmax amplifies the positive pair's contribution. Gradients push cat₁ and cat₂ closer, push negatives away.

Result: After training, hh embedings cluster same objects together, generalize to new images.

Why SimCLR Works: The Magic of Large Batches & Strong Augmentation

Negatives per sample=2N2\text{Negatives per sample} = 2N - 2

With N=4096N=4096, each sample contrasts against 8190 negatives. This provides rich supervision.

Temperature τ\tau: Controls hardness of negatives.

  • Small τ\tau (e.g., 0.1): Focus on hard negatives (sharp distribution)
  • Large τ\tau (e.g., 1.0): Treat all negatives more equally (soft distribution)

Typical value: τ[0.1,0.5]\tau \in [0.1, 0.5]

Augmentation strength: Stronger augmentation forces invariance to more transformations. SimCLR found combining color distortion + crop + blur crucial.

CLIP: Contrastive Language-Image Pre-training

The CLIP Architecture

Two encoders:

  1. Image encoder: Vision Transformer (ViT) or ResNet I:imageRdI: \text{image} \rightarrow \mathbb{R}^d

  2. Text encoder: Transformer T:captionRdT: \text{caption} \rightarrow \mathbb{R}^d

Both project to a joint embedding space of dimension dd (typically 512 or 1024).

Training data: 400 million (image, text) pairs scraped from the internet.

CLIP's Contrastive Objective: A Symmetric Dance

Given a batch of NN (image, caption) pairs:

  1. Compute image embedings: {I1,I2,...,IN}\{I_1, I_2, ..., I_N\}
  2. Compute text embedings: {T1,T2,...,TN}\{T_1, T_2, ..., T_N\}
  3. Compute pairwise cosine similarities: Sij=IiTjIiTjS_{ij} = \frac{I_i \cdot T_j}{\|I_i\| \|T_j\|}

The symmetric loss: For each image, its correct caption is positive, all others are negative (and vice versa).

Image-to-text loss (row-wise softmax): Li2t=1Ni=1Nlogexp(Sii/τ)j=1Nexp(Sij/τ)\mathcal{L}_{i2t} = -\frac{1}{N} \sum_{i=1}^{N} \log \frac{\exp(S_{ii} / \tau)}{\sum_{j=1}^{N} \exp(S_{ij} / \tau)}

Text-to-image loss (column-wise softmax): Lt2i=1Nj=1Nlogexp(Sj/τ)i=1Nexp(Sij/τ)\mathcal{L}_{t2i} = -\frac{1}{N} \sum_{j=1}^{N} \log \frac{\exp(S_{j} / \tau)}{\sum_{i=1}^{N} \exp(S_{ij} / \tau)}

Total CLIP loss: LCLIP=12(Li2t+Lt2i)\mathcal{L}_{\text{CLIP}} = \frac{1}{2}(\mathcal{L}_{i2t} + \mathcal{L}_{t2i})

Why symmetric? Both directions (image→text and text→image) provide gradient signal. This treats both modalities equally, preventing one from dominating.

Similarity matrix (after encoding, values are cosine similarities):

         T_cat  T_dog  T_car  T_tree
I_cat     0.85   0.15   0.10   0.12
I_dog     0.18   0.82   0.14   0.11
I_car     0.09   0.13   0.88   0.15
I_tree    0.11   0.10   0.16   0.84

Image-to-text for I_cat (want to pick T_cat): i2tcat=loge0.85/0.07e0.85/0.07+e0.15/0.07+e0.10/0.07+e0.12/0.07\ell_{i2t}^{\text{cat}} = -\log \frac{e^{0.85/0.07}}{e^{0.85/0.07} + e^{0.15/0.07} + e^{0.10/0.07} + e^{0.12/0.07}}

With τ=0.07\tau=0.07: numerator e12.11.8×105\approx e^{12.1} \approx 1.8 \times 10^5, denominator dominated by correct pair, loss≈ 0.05 (small, good).

Why this step? High diagonal values (correct pairs) mean low loss. Gradients make encoders align matching pairs more strongly.

Zero-shot use: At test time, for class "airplane", compute similarity between image and text "a photo of an airplane" without any airplane training labels.

Why CLIP is Revolutionary

1. Zero-shot transfer: No labeled data for new tasks

  • Create text prompts for classes: "a photo of a [CLASS]"
  • Compute similarity with image embedding
  • Predict class with highest similarity

2. Robustness: Trained on diverse internet data, not curated datasets

  • Better handles distribution shift
  • More robust to adversarial examples (not explicitly optimized for ImageNet quirks)

3. Flexibility: Understands nuanced language

  • Can distinguish "a photo of a cat" vs "a drawing of a cat"
  • Handles compositional queries: "a cat wearing sunglasses"

Encode: IRdI \rightarrow \mathbb{R}^d, each tkRdt_k \rightarrow \mathbb{R}^d

Compute logits: k=ItkItkα\ell_k = \frac{I \cdot t_k}{\|I\| \|t_k\|} \cdot \alpha

where α=1/τ\alpha = 1/\tau is a learned temperature scaling.

Apply softmax for probability: p(y=kI)=exp(k)j=1Kexp(j)p(y=k | I) = \frac{\exp(\ell_k)}{\sum_{j=1}^{K} \exp(\ell_j)}

Why this works: The embedding space was trained so that semantically matching (image, text) pairs have high cosine similarity. At test time, the image naturally aligns with the correct class description.

Common Mistakes & Misconceptions

Why it's wrong: The model never sees the same augmented view twice. It must learn semantic features that remain invariant across augmentations (rotation, crop, color jitter). It can't memorize pixel patterns.

The fix: Contrastive learning forces the model to extract features robust to augmentations. These features generalize because they capture high-level semantics, not low-level pixels.

Why it's wrong: Contrastive learning needs many negatives. With batch size 32, SimCLR only has 62 negatives per sample. This provides weak signal.

The fix: Use large batch sizes (≥1024) or memory banks/momentum encoders (like MoCo) to maintain a large pool of negatives. More negatives → harder task → better representations.

Math: With NN samples, negatives = 2N22N-2. For N=32N=32:62 negatives. For N=4096N=4096: 8190 negatives (132× more contrast).

Why it's wrong: Empirically, using a projection head g()g(\cdot) and saving h=f(x)h=f(x) for downstream tasks improves transfer performance by 10+ percentage points.

Why it works: The projection head can contain information useful for the contrastive pretext task (e.g., color, exact spatial position) that's not useful for semantic downstream tasks. By separating representation (hh) from contrastive space (zz), we get better general features.

Why it's wrong: For tasks with abundant labeled data (e.g., ImageNet classification), fine-tuned supervised models still outperform CLIP zero-shot. CLIP's strength is generalization to new tasks/distributions, not peak accuracy on standard benchmarks.

The tradeoff: CLIP trades some peak accuracy for flexibility and robustness. Use CLIP for:

  • Zero/few-shot learning
  • Tasks with scarce labels
  • Domain adaptation
  • Open-vocabulary recognition

Use supervised fine-tuning when:

  • You have abundant labeled data
  • You need maximum accuracy on a fixed task
  • Latency/compute is critical (CLIP models are larger)

Connections

  • Supervised learning fundamentals - contrastive learning avoids expensive labeling
  • Transfer learning and fine-tuning - self-supervised pretraining → fine-tune on downstream tasks
  • Data augmentation techniques - SimCLR depends critically on augmentation design
  • Metric learning and embedings - contrastive learning learns a metric space with semantic meaning
  • Vision transformers (ViT) - CLIP uses ViT as image encoder
  • Attention mechanisms and transformers - CLIP's text encoder is a Transformer
  • Zero-shot and few-shot learning - CLIP enables zero-shot classification via language
  • Multi-modal learning - CLIP bridges vision and language modalities
  • Representation learning - core goal: learn features that transfer across tasks
  • Batch normalization and training techniques - large batch training requires careful optimization (LARS optimizer)

Recall Explain to a 12-year-old

Imagine you're learning what cats and dogs look like, but nobody tells you which is which.

Instead, you play a game: I show you two photos. Sometimes they're two pictures of the same cat (just from different angles or lighting). Sometimes one is a cat and one is a dog. Your job is to say "same" or "different."

At first you guess randomly. But to get good at this game, you start noticing: "Oh, cats have pointy ears and whiskers. Dogs have floppy ears and longer snouts." You're learning what makes cats look like cats and dogs look like dogs, even though nobody labeled them!

That's contrastive learning. SimCLR does this with millions of images. It learns "these two weird-looking pictures are actually the same cat" by focusing on what doesn't change when you tilt the camera or change the lighting.

CLIP adds language: now you also have captions. It learns "this picture goes with the sentence 'a fluffy cat' but not with'a red car'." So when you show it a new picture and ask "is this a cat or a car?", it compares the image to those descriptions and answers correctly—without ever being told "this is a cat photo" during training!

CLIP = Contrastive Language-Image Pre-training
"CLIP images to captions with contrastive loss"

Contrastive loss mnemonic: "Push Negatives, Pull Positives"
The loss denominator includes positives (numerator) and negatives, normalized.

Key equation structure: log(positive/(positive+negatives))-\log(\text{positive} / (\text{positive} + \text{negatives}))
Think: "Maximize probability of picking the positive from a lineup"

#flashcards/ai-ml

What is the core principle of contrastive learning? :: Contrastive learning maximizes agreement (similarity) between positive pairs while minimizing agreement between negative pairs in a learned embedding space.

What are the four key components of SimCLR?
(1) Data augmentation to create positive pairs, (2) Base encoder (e.g., ResNet), (3) Projection head MLP, (4) Contrastive loss over augmented views.

Write the InfoNCE contrastive loss formula for anchor zz, positive z+z^+, and negatives {zi}\{z_i^-\} :: L=logexp(sim(z,z+)/τ)exp(sim(z,z+)/τ)+iexp(sim(z,zi)/τ)\mathcal{L} = -\log \frac{\exp(\text{sim}(z,z^+)/\tau)}{\exp(\text{sim}(z,z^+)/\tau) + \sum_i \exp(\text{sim}(z,z_i^-)/\tau)} where sim\text{sim} is cosine similarity and τ\tau is temperature.

Why does SimCLR use a projection head on top of the encoder?
The projection head allows the contrastive loss to operate in a space optimized for the pretext task, while the encoder representations remain general for downstream transfer tasks. Empirically improves performance by 10+ points.
What is CLIP's training objective?
CLIP uses symmetric contrastive loss: maximize cosine similarity between matched (image, caption) pairs and minimize similarity between unmatched pairs in a joint embedding space.
How does CLIP perform zero-shot classification?
Create text prompts for each class (e.g., "a photo of a [class]"), encode both image and text prompts, compute cosine similarities, and predict the class with highest similarity.
Why do contrastive methods need large batch sizes?
Larger batches provide more negative samples per positive pair, creating stronger contrastive signal. SimCLR uses batches of 4096-8192; with N samples you get 2N-2 negatives per anchor.
What is the temperature parameter τ in contrastive loss and what does it control?
Temperature τ controls the concentration of the softmax distribution. Smaller τ (e.g., 0.1) makes the model focus on hard negatives; larger τ (e.g., 1.0) treats all negatives more equally. Typical values: 0.1-0.5.
What is the difference between self-supervised and unsupervised learning?
Self-supervised learning creates pseudo-labels from data structure (e.g., predict masked tokens, match augmented views) and trains with them. Unsupervised learning finds structure (clustering, density) without any labels or pseudo-labels.
Why is CLIP robust to distribution shift?
CLIP is trained on 400M diverse (image, text) pairs from the internet, not curated datasets. This exposure to natural distribution makes it generalize better to new domains and resist dataset-specific biases.
What data augmentations are critical for SimCLR?
Random crop + resize, color distortion (brightness, contrast, saturation, hue), Gaussian blur, and random grayscale. Strong augmentation forces the model to learn semantic features invariant to transformations.
When should you use CLIP vs supervised models?
Use CLIP for zero/few-shot learning, tasks with scarce labels, domain adaptation, and open-vocabulary recognition. Use supervised models when you have abundant labeled data and need maximum accuracy on a fixed benchmark.

Concept Map

solves

forces model to learn

is a type of

pulls together

pushes apart

optimized by

optimized by

controlled by

uses

creates pairs via

contrasts image and text

transfer to

Self-supervised Learning

Pretext Task

Useful Representations

Contrastive Learning

Positive Pairs

Negative Pairs

InfoNCE Loss

Temperature tau

SimCLR

CLIP

Downstream Tasks

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho yaar, core intuition simple hai: humare paas internet pe crores images aur text available hain, but unko manually label karna (yeh cat hai, yeh dog hai) bohot mehenga aur time-consuming kaam hai. Toh self-supervised learning kehti hai ki data ko khud hi apna label banane do! Model ko ek "pretext task" diya jaata hai — jaise ek image ke do alag-alag crops dekhke pehchano ki dono same photo se aaye hain ki nahi. Yeh task solve karne ke liye model ko automatically object ke shapes, colors, textures, boundaries sab seekhna padta hai. Bina kisi human label ke, model useful features seekh leta hai.

Ab contrastive learning ismein ek specific trick hai — iska funda hai "positive pairs ko paas kheencho, negative pairs ko door dhakelo." Matlab same cat ke do augmented views (thoda crop kiya, color thoda change kiya) ko representation space mein similar banao, aur cat vs dog ko alag rakho. Yeh maths mein InfoNCE loss ke through hota hai, jismein basically ek softmax classification jaisa setup hota hai — model ko positive sample ko N+1 options mein se pehchanna hai. Temperature parameter τ control karta hai ki distribution kitni sharp ho. SimCLR isi idea ko use karta hai: strong augmentation lagao, ResNet encoder se features nikaalo, aur ek projection head ke through contrastive loss apply karo.

Yeh cheez important kyun hai? Kyunki pretraining ek baar bina labels ke ho jaaye, toh phir tumhe downstream tasks (classification, detection) ke liye sirf thode se labeled data ki zaroorat padti hai. CLIP jaise models toh images aur text dono ko ek saath align karte hain, jisse zero-shot classification possible ho jaata hai — bina training ke naye categories pehchan lena. Regional students ke liye yeh isliye bhi kaam ka hai kyunki jahan labeled data kam hai (jaise local languages ya specific domains), wahan self-supervised approach bohot powerful ban jaata hai. Yeh modern AI ka backbone hai — GPT, BERT, foundation models sab isi philosophy pe khade hain.

Go deeper — visual, from zero

Test yourself — Research Frontiers & Practice

Connections