Contrastive Learning Framework and InfoNCE Loss
What you'll learn: How the contrastive learning framework combines data augmentation, encoders, and the InfoNCE loss to learn powerful representations by distinguishing similar from dissimilar samples.
The Complete Picture
You've learned the individual pieces—now let's see how they fit together into a unified contrastive learning system.
The contrastive learning framework follows this pipeline:
- Create pairs: Generate positive pairs through data augmentation (two views of the same image) and treat all other samples as negatives
- Encode: Pass each view through an encoder network, then through a projection head
- Compute InfoNCE loss: Pull positive pairs together while pushing negative pairs apart
- Update: Backpropagate to improve the encoder's representations
How InfoNCE Drives the Framework
The InfoNCE loss is the mathematical heart of this framework:
L = -log(exp(sim(z_i, z_i+) / τ) / Σ exp(sim(z_i, z_k) / τ))
For a given sample (the anchor), InfoNCE:
- Maximizes similarity with its positive pair
- Minimizes similarity with all negatives in the batch
- Uses temperature τ to control how hard the discrimination is
Think of it like teaching a student to recognize cats: you show them many cat photos (positives should cluster) while also showing dogs, birds, and cars (negatives should separate). The student learns what makes cats distinctive.
Why This Framework Works
The framework forces your encoder to learn invariances (both augmented views should be similar) while maintaining discrimination (different samples should remain distinct). Large batch sizes provide more negatives, making the task harder and the learned features richer. Momentum encoders provide stable negative representations across training steps.
Key Takeaway: The contrastive learning framework orchestrates data augmentation, encoding, and InfoNCE loss to create a self-supervised system that learns by distinguishing transformed views of the same sample from all other samples in the batch.