Deep Learning

Contrastive & Self-Supervised Learning

Learning rich vector representations without human labels using InfoNCE loss and data augmentations.

🔴 advanced5 min readunsupervised
Contrastive Learning is a self-supervised representation learning paradigm that trains neural networks without human annotations. It pulls augmented positive pairs of the same sample together in embedding space while pushing negative pairs apart using InfoNCE loss. Foundational architectures include SimCLR, MoCo, and CLIP, building high quality representations for vision, text, and multimodal retrieval.

What is Self-Supervised Contrastive Learning?

Supervised learning requires millions of human labeled samples $(x, y)$.

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:

$$\mathcal{L}{\text{InfoNCE}} = -\log \frac{\exp\left(\text{sim}(z_i, z_j) / \tau\right)}{\sum{k=1}^K \exp\left(\text{sim}(z_i, z_k) / \tau\right)}$$

InfoNCE is mathematically equivalent to multi-class Cross-Entropy loss where the target is identifying the single positive pair out of $K$ 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)

2. MoCo (Momentum Contrast - He et al., 2020)

SimCLR requires massive batch sizes ($4096$) to get enough negative samples.

MoCo maintains a dynamic Memory Queue of 65,536 negative keys:

3. CLIP (Contrastive Language-Image Pretraining - Radford et al., 2021)

Applies contrastive learning across two modalities (Image + Text):

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

  1. What is Collapse in Self-Supervised Learning? A failure mode where the encoder outputs a constant vector $z = c$ for all inputs, minimizing distance for positive pairs trivially. Avoided in BYOL (Bootstrap Your Own Latent) using asymmetric predictor networks and EMA updates.
  2. What is Supervised Contrastive Learning (SupCon)? Extending InfoNCE loss to labeled data by defining all samples sharing the same class label $y$ as positive pairs, outperforming standard Cross-Entropy classification.

Check yourself

Question 1 of 3

What core loss function optimizes positive pair similarity while pushing negative pairs apart in Contrastive Learning?

More in Deep Learning

See all →
Activation Functions4 minDropout4 minBackpropagation5 min