X-Sample Contrastive Loss: Improving Contrastive Learning with Sample Similarity Graphs

Vlad Sobal, Mark Ibrahim, Randall Balestriero, Vivien Cabannes, Diane Bouchacourt, Pietro Astolfi, Kyunghyun Cho, Yann LeCun

TL;DR

This paper introduces X-Sample Contrastive (X-CLR), a novel loss function that enhances contrastive learning by explicitly modeling the nuanced relationships between data samples, rather than treating all non-positive samples as equally dissimilar. X-CLR leverages similarity graphs to capture these relationships, leading to improved representation learning across various tasks and datasets, particularly in low-data regimes.

What Problem Does This Paper Address?

Traditional contrastive learning methods, while effective for learning representations, operate on a binary notion of similarity: a sample is either a "positive" (related) or a "negative" (unrelated). This binary approach ignores the valuable information contained in the varying degrees of similarity between samples. For instance, two images of dogs might be highly similar, while a dog and a cat image share a moderate similarity, and a dog and a car are largely dissimilar. Standard contrastive losses treat the dog-cat pair the same as the dog-car pair, pushing them equally far apart in the embedding space. This paper aims to address this limitation by developing a more sophisticated contrastive objective that can capture these finer-grained similarities, leading to richer and more informative learned representations.

Methods

The core contribution of this paper is the X-Sample Contrastive (X-CLR) loss. This new objective moves away from the binary positive/negative distinction of standard contrastive losses. Instead, X-CLR frames contrastive learning as modifying a similarity graph. This graph is no longer binary but contains continuous scalars that represent the degree of relatedness between samples.

Here's how it works:

  • Similarity Graph: X-CLR constructs a "soft" similarity graph where edge weights represent the nuanced similarity between any two samples. This graph can be derived from various sources, such as class labels, text captions, or even semantic relationships from external knowledge bases.
  • Soft Targets: The X-CLR loss uses these continuous similarity values as "soft targets." This means instead of aiming for a hard binary classification (positive vs. negative), the model is trained to align its learned representations with these soft similarity scores.
  • X-CLR Loss Formulation: The paper reformulates the standard InfoNCE loss to incorporate these soft targets. The loss function encourages the model to produce embeddings where the cosine similarity between sample representations reflects the similarity defined in the graph.
  • Text-based Similarity: In their experiments, the authors primarily use text captions associated with images to infer the similarity graph. A trained text encoder is used to generate embeddings for captions, and the cosine similarity between these embeddings defines the soft similarity between image-text pairs.

Datasets

The authors evaluate X-CLR on three datasets of varying scales:

  • ImageNet-1k: A dataset with 1 million samples and high-quality labels.
  • CC3M (Conceptual Captions 3M): A dataset with 3 million noisy image-text caption pairs.
  • CC12M (Conceptual Captions 12M): A dataset with 12 million noisy image-text caption pairs.

They also use benchmarks for evaluating specific capabilities, including:

  • ImageNet Real: A revised version of ImageNet.
  • ImageNet-9: A benchmark for evaluating robustness to background perturbations.
  • ObjectNet: A dataset designed to test object recognition models on natural object transformations.
  • MIT States: A dataset for fine-grained attribute disambiguation.

Key Insights and Ideas

  • Beyond Binary Similarity: The most significant insight is that contrastive learning can be generalized beyond a strict positive/negative dichotomy. Recognizing and utilizing varying degrees of similarity between samples leads to richer representations.
  • Graph-based Perspective: Viewing contrastive learning through the lens of similarity graphs provides a powerful framework for understanding its limitations and developing new objectives.
  • Data Efficiency: X-CLR shows remarkable performance gains, especially in low-data regimes, suggesting that capturing nuanced relationships is more crucial than sheer data volume when data is scarce.
  • Attribute Disentanglement: The method appears to encourage models to learn representations that better separate objects from their attributes and backgrounds, a desirable property for robust visual understanding.
  • Label Quality Matters: For fine-grained tasks, the quality of the labels used to construct the similarity graph is more critical than the quantity of data.

Key Results

  • Outperforms Baselines: X-CLR consistently outperforms standard contrastive methods like SimCLR and even supervised contrastive methods (SupCon) on various downstream tasks, including classification and background decomposition.
  • ImageNet Performance: On ImageNet, X-CLR achieves gains of 12.2% over SimCLR and 1.3% over SupCon.
  • Low-Data Regimes: When trained on CC3M (3 million samples), X-CLR shows significant improvements over CLIP, with gains of 17.2% on ImageNet and 18.0% on ImageNet Real.
  • Attribute Disambiguation: X-CLR demonstrates gains of 3.3-5.6% over CLIP on ImageNet-9 for separating objects from attributes and backgrounds.
  • Computational Efficiency: X-CLR introduces minimal computational overhead compared to SimCLR during training.
  • Finetuning Capability: X-CLR can be effectively used as a finetuning objective for pretrained backbones, further improving performance.

Limitations and Future Work

  • Graph Construction: The paper acknowledges that constructing the cross-sample similarity graph requires additional data and memory. The quality of the learned representations is highly dependent on the quality of the graph, especially with noisy datasets like Conceptual Captions.
  • Noisy Captions: For datasets with noisy captions, better methods for inferring the graph are needed. The authors suggest exploring other metadata like location or time.
  • Integration with Other Objectives: The authors believe X-CLR's soft similarity graph approach could be integrated into non-contrastive objectives like BYOL or VICReg to enrich their representations.
  • Hyperparameter Sensitivity: While the paper explores the effect of temperature ($T_s$) on the softmax function for similarity calculation, further research into optimal hyperparameter tuning for different datasets and graph sources could be beneficial.

Summary updated

BibTeX
@inproceedings{sobal2025x,
  author = {Vlad Sobal and Mark Ibrahim and Randall Balestriero and Vivien Cabannes and Diane Bouchacourt and Pietro Astolfi and Kyunghyun Cho and Yann LeCun},
  title = {{X-Sample Contrastive Loss: Improving Contrastive Learning with Sample Similarity Graphs}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=vT53M6d0aR},
}
Figure