Contrastive & Self-Supervised Learning
Learning rich vector representations without human labels using InfoNCE loss and data augmentations.
What is Self-Supervised Contrastive Learning?
Supervised learning requires millions of human labeled samples .
Self-Supervised Contrastive Learning trains deep neural networks on unlabelled data by learning to distinguish similar inputs from dissimilar inputs.
The core intuition:
Pull representations of Positive Pairs (similar samples) close together in embedding space, while pushing Negative Pairs (different samples) far apart:
UNLABELLED DATA ──► Augmentation View 1 (x_i) ──► [ ENCODER f(x) ] ──► Embedding z_i ──┐ (PULLED TOGETHER!)
──► Augmentation View 2 (x_j) ──► [ ENCODER f(x) ] ──► Embedding z_j ──┘
DIFFERENT IMAGE ──► Augmentation View (x_k) ──► [ ENCODER f(x) ] ──► Embedding z_k ─── (PUSHED AWAY!)
InfoNCE Loss Function
Contrastive learning is driven by the InfoNCE (Information Noise Contrastive Estimation) Loss:
- : Cosine similarity between normalized embeddings.
- (tau): Temperature hyperparameter (typically ) scaling prediction sharpness.
- Numerator: Similarity of Positive Pair .
- Denominator: Sum of similarities across all Negative Pairs in the batch.
InfoNCE is mathematically equivalent to multi-class Cross-Entropy loss where the target is identifying the single positive pair out of candidate choices.
Foundational Architectures
┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. SimCLR (Google 2020) │ 2. MoCo (Meta 2020) │ 3. CLIP (OpenAI 2021) │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Requires HUGE mini batch │ Uses dynamic Memory Queue│ Contrastive vision-text │
│ sizes (4096) to collect │ and Momentum Encoder to │ pretraining across 400M │
│ enough negative samples. │ decouple queue from batch│ Image Text web pairs. │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘
1. SimCLR (Chen et al., 2020)
- Applies two data augmentations (Random Crop + Color Jitter + Blur) to an image to form positive pair .
- All other augmented images in the mini-batch serve as negative samples.
- Key Insight: Proves that Projection Heads (MLP layers placed after encoder representations during training and discarded for downstream tasks) boost feature quality by .
2. MoCo (Momentum Contrast - He et al., 2020)
SimCLR requires massive batch sizes () to get enough negative samples.
MoCo maintains a dynamic Memory Queue of 65,536 negative keys:
- Encoders keys using a slow Momentum-Updated Network ().
- Decouples negative sample pool size from GPU mini-batch size!
3. CLIP (Contrastive Language-Image Pretraining - Radford et al., 2021)
Applies contrastive learning across two modalities (Image + Text):
- Positive Pair: (Image , Matching Caption ).
- Negative Pairs: All non-matching (Image , Caption ) pairs in the batch.
Enables zero-shot image classification and powers text-to-image models (Stable Diffusion, DALL-E).
Say this out loud
Contrastive Learning is a self-supervised paradigm that pulls positive pair embeddings together while pushing negative pair embeddings apart using InfoNCE loss. SimCLR uses augmented views of unlabelled images with high batch sizes. MoCo uses a momentum encoder and negative sample queue, while CLIP applies contrastive learning to align image and text representations.
Followups to expect
- What is Collapse in Self-Supervised Learning? A failure mode where the encoder outputs a constant vector for all inputs, minimizing distance for positive pairs trivially. Avoided in BYOL (Bootstrap Your Own Latent) using asymmetric predictor networks and EMA updates.
- What is Supervised Contrastive Learning (SupCon)? Extending InfoNCE loss to labeled data by defining all samples sharing the same class label as positive pairs, outperforming standard Cross-Entropy classification.
Check yourself
What core loss function optimizes positive pair similarity while pushing negative pairs apart in Contrastive Learning?