Modern AI Engineering

Lesson 2.9 · 27 min · video lesson

Contrastive Learning: Training by Comparison

How can a model learn that two photos show the same dog, or that a caption describes an image, when nobody has labelled a single example?

In short: Contrastive learning trains an encoder to map inputs to embeddings so that related pairs (positives) end up close together and unrelated pairs (negatives) end up far apart. Positive pairs usually come for free, from two augmented views of the same image or from an image and its caption, so huge unlabelled datasets can be used. Losses such as InfoNCE turn this into a "pick the true partner out of the batch" task, and methods like SimCLR, MoCo and CLIP use it to build the embeddings behind semantic search, RAG and multimodal AI.

What you will learn

  • What is Contrastive Learning?
  • Why do we need Contrastive Learning?
  • The key idea behind Contrastive Learning.
  • Positive pairs and Negative pairs.
  • How does Contrastive Learning work step-by-step?
  • Loss functions used in Contrastive Learning.
  • Popular Contrastive Learning methods.
  • Real-world use cases of Contrastive Learning.
  • [Feature Engineering in Machine Learning](https://www.youtube.com/watch?v=QLlywrWuXag) (Video)
  • [One-hot Encoding in Machine Learning](https://www.youtube.com/watch?v=6AmedU5i9go) (Video)

In this lesson

  1. What is contrastive learning?
  2. Why do we need contrastive learning?
  3. The key idea: pull together, push apart
  4. Positive pairs and negative pairs
  5. How contrastive learning works, step by step
  6. Loss functions used in contrastive learning
  7. Code: training a tiny encoder with InfoNCE
  8. Popular contrastive learning methods
  9. Real-world use cases, and limits
  10. Common mistakes and how to spot them
  11. Practice: try it yourself

Key terms in this lesson

Contrastive learning
Learning embeddings by pulling related pairs together and pushing unrelated pairs apart.
Positive pair
Two inputs that should have similar embeddings, such as two views of the same image.
Negative pair
Two inputs that should have dissimilar embeddings.
InfoNCE
A softmax-based contrastive loss that asks the model to identify the positive among many negatives.
Temperature (τ)
A scale applied to similarities before the softmax; smaller values make the distribution sharper.
Representation collapse
A failure where the encoder maps all inputs to nearly the same embedding.
Hard negative
A negative example that looks similar to the anchor and is therefore informative to train on.

This lesson is free. Sign in or create a free account to open it with its video and quiz.

← 2.8 Reinforcement Learning: Teaching Agents Through Reward · 3.1 Neural Network Bias: What It Is and Why It Matters →