Deep LearningSelf-Supervised Visual Representation Learning

What are positive and negative pairs in contrastive learning?

Positive and negative pairs tell a contrastive-learning model which image views should be treated as related and which should be distinguished. A positive pair is usually two augmented views of the *same source image*. A negative pair is usually a view of that image paired with a view of a *different source image*. These relationships can be created without human class labels.

What are the three roles?

  • Anchor: The image view whose learned representation we are comparing with other views.
  • Positive: A related view—for example, another crop or brightness-adjusted version of the anchor's original photograph. We want its embedding to remain similar to the anchor's.
  • Negative: A contrasting view, typically from another original photograph. The model is trained to distinguish it from the anchor.
  • Important distinction: In common *instance-discrimination* training, “different images” does not necessarily mean “different semantic classes.” Two photos can both show bicycles yet be assigned a negative relationship because they come from different original images.

Beginner-friendly example: Selecting image pairs

Suppose our dataset contains three unlabeled source photos:

  • Photo A: A red bicycle beside a shop.
  • Photo B: A red motorcycle on a road.
  • Photo C: A green tree.

For Photo A, create two random but meaning-preserving views: a crop and a slightly color-adjusted crop.

  • Anchor: First view of Photo A.
  • Positive: Second view of Photo A—same underlying photo, despite visual changes.
  • Harder negative: A view of Photo B, since the red motorcycle may share visual features with the red bicycle.
  • Easier negative: A view of Photo C, a tree with quite different visual content.

The relationships were created by tracking which original photo each view came from. Nobody had to manually label the pictures as bicycles, motorcycles or trees.

Tiny numerical example: How similar are these pairs?

Imagine an encoder has produced the following illustrative, unit-length 2D embeddings:

  • Anchor: a = [0.6, 0.8]
  • Positive: p = [0.8, 0.6]
  • Hard negative: n_h = [1, 0]
  • Easy negative: n_e = [−0.8, 0.6]

For unit-length vectors, cosine similarity is their dot product.

  1. Anchor–positive: sim(a,p) = 0.6×0.8 + 0.8×0.6 = 0.96.
  2. Anchor–hard-negative: sim(a,n_h) = 0.6×1 + 0.8×0 = 0.60.
  3. Anchor–easy-negative: sim(a,n_e) = 0.6×(−0.8) + 0.8×0.6 = 0.00.

Interpretation: The positive is very similar (0.96); the hard negative is more similar (0.60) to the anchor than the easy negative (0.00), so it is harder to distinguish. These numbers are invented for teaching, not empirical measurements or guaranteed outcomes. Training can change all of these similarities.

Where do in-batch negatives come from?

An efficient strategy is to create two views per source image in a training batch.

  • For `N = 3` source photos: The batch contains 2N = 6 augmented views.
  • Choose one anchor view: Exclude the anchor itself.
  • One corresponding positive: The other view of the same original photo.
  • Remaining contrasting views: The other `2N − 2 = 4` views come from the two different source photos and can serve as in-batch negatives.

They are called *in-batch* because the other examples already in the same training batch are reused as contrasts; no separate hand-labeled negative dataset is necessary. This count describes the two-view, one-positive instance-discrimination setup, not every contrastive-learning method. More negatives may increase compute and may increase semantic collisions; more is not automatically better.

What are false negatives and hard negatives?

  • False negative: Two views come from different source images but are actually semantically related for the downstream task. For example, two separate pictures of bicycles may be labeled negative by source identity, even though a future bicycle-category classifier should regard them as similar.
  • Hard negative: A genuinely contrasting example whose current embedding is unusually similar to the anchor. A bicycle and a motorcycle might be hard to distinguish because of shared shapes and wheels.
  • The risk: A highly similar image chosen as a “hard negative” can actually be a false negative. Aggressively pushing it away may damage the semantics the model should preserve.

Research on false-negative cancellation and hard-negative sampling studies ways to identify or reduce these issues. There is no guarantee that simply mining the closest negatives improves representations.

How do you choose pairs responsibly?

  • Preserve meaning in positives: A crop should not accidentally remove the relevant object or turn the view into something fundamentally different. Poorly chosen augmentations can make false positive relationships.
  • Avoid known duplicates or semantic collisions: Where metadata or labels are available, they can help detect cases in which “different filenames” do not imply distinct content.
  • Be cautious with hard-negative mining: Similarity may reflect true shared semantics, not merely a good training challenge; use appropriate filtering and check downstream quality.
  • Use the right relationship for the objective: In *supervised contrastive learning*, images with the same known class can be treated as positives. In common self-supervised instance discrimination, source-image identity provides the relationship without those labels.
  • Evaluate on real tasks: Check whether the learned encoder improves retrieval, classification or transfer—not just whether pair similarities separate in training.

Key takeaway: Positive pairs normally link different views of one source image; negative pairs normally compare different sources. Hard negatives can provide useful challenges, but false negatives and poorly selected augmentations can corrupt the learning signal.

For the overall purpose of learning embeddings, see “What is contrastive representation learning in deep neural networks?” (Question 262). The InfoNCE loss and temperature parameter are covered in Questions 264 and 265; their formulas are deliberately left out here.

Research: Chen et al. (ICML 2020), *A Simple Framework for Contrastive Learning of Visual Representations*; Huynh et al. (WACV 2022), *Boosting Contrastive Self-Supervised Learning With False Negative Cancellation*; Robinson et al. (ICLR 2021), *Contrastive Learning With Hard Negative Samples*.

#what are positive and negative pairs in contrastive learning#contrastive learning anchor positive negative examples#positive negative pairs cosine similarity numerical example#what are in-batch negatives in contrastive learning#false negatives semantic collision self-supervised learning#hard negatives versus false negatives contrastive learning#how to construct image pairs contrastive learning#deep-learning#contrastive-learning#positive-pairs

Learning Sequence

Deep Learning Curriculum

92% Completed
Up NextDeep Learning

What is InfoNCE loss in contrastive learning?

Explore Related Questions

Deepen your understanding across related Deep Learning topics.

Machinoai Logo
Machinoai

Your edge in the AI era. Deep dives into machine learning concepts, paper explainers, verified industry jobs, and developer tools.

© 2026 Machinoai. All rights reserved.