ML & Deep Learning Foundations·Question 40 of 55Advanced

Explain contrastive learning and the InfoNCE loss. How are modern embedding models (CLIP, text retrievers) actually trained?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Contrastive learning trains an encoder so that "matching" pairs score high similarity and non-matching pairs score low - without per-example labels. InfoNCE operationalises it as cross-entropy over similarities: for query q with positive k⁺ among a set of negatives {kᵢ},

Full worked answer with diagram

Contrastive learning trains an encoder so that "matching" pairs score high similarity and non-matching pairs score low - without per-example labels. InfoNCE operationalises it as cross-entropy over similarities: for query q with positive k⁺ among a set of negatives {kᵢ},

L = −log [ exp(sim(q,k⁺)/τ) / Σᵢ exp(sim(q,kᵢ)/τ) ]

i.e., an N-way classification where the "correct class" is the true partner. sim is usually cosine; τ (~0.05-0.1 typically; CLIP learns it, initialized to 0.07) sharpens the softmax so the loss concentrates on the hardest, most confusable negatives. Theoretically it's a lower bound on mutual information between the paired views (from the Contrastive Predictive Coding paper), though in practice people treat it as "softmax retrieval loss."

Where positives/negatives come from is the whole game:

  • CLIP: positives are (image, caption) pairs from web data; negatives are all other pairs in the batch (in-batch negatives) - an N×N similarity matrix with a symmetric loss in both directions. Batch size is critical because it sets the number/difficulty of negatives; CLIP used ~32k.
  • Text embedding/retrieval models (E5/GTE/BGE-style, DPR lineage): positives are (query, relevant passage); in-batch negatives are augmented with hard negatives mined by BM25 or a prior model - passages that are topically close but wrong, which teach fine-grained discrimination. Common recipe: large-scale weakly supervised contrastive pretraining on mined pairs, then fine-tuning on curated data with hard negatives, sometimes distilling from a cross-encoder reranker.
  • SimCLR-style self-supervised vision: positives are two augmentations of the same image.

Failure modes worth naming: false negatives (in-batch "negatives" that are actually relevant) cap achievable quality on noisy data; small batches → easy negatives → mushy embeddings (memory banks/MoCo-style queues were built to fake bigger negative pools); and if you train with cosine+τ, you must serve with cosine (normalize before indexing).

Worth sketching. Once the similarity matrix is on the board, batch size and hard negatives stop being separate topics: they are both just columns in that softmax.

flowchart LR
    A["Batch of N pairs"] --> B["Encode queries"]
    A --> C["Encode positives"]
    B --> D["N by N similarity matrix,<br/>divided by tau"]
    C --> D
    E["Mined hard negatives<br/>from BM25 or a prior model"] --> D
    D --> F["Softmax over each row:<br/>the diagonal is the correct class"]
    F --> G["Cross-entropy, both directions"]

What a good answer covers

  • Contrastive learning trains an encoder so that "matching" pairs score high similarity and non-matching…
  • InfoNCE operationalises it as cross-entropy over similarities
  • L = −log [ exp(sim(q,k⁺)/τ) / Σᵢ exp(sim(q,kᵢ)/τ) ]
  • i.e., an N-way classification where the "correct class" is the true partner
  • Theoretically it's a lower bound on mutual information between the paired views (from the Contrastive…

Key terms: InfoNCE · τ · CLIP · in-batch negatives · Text embedding/retrieval models · hard negatives · SimCLR-style self-supervised vision · false negatives

Interviewer follow-ups to expect

  1. Why do larger batches help contrastive training but matter less for standard supervised training?

  2. How do hard negatives change what the embedding space learns?

  3. How would you build the training set for a code-search embedder?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep