$\mathbb{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

Abstract

Learning good representations involves capturing the diverse ways in which data samples relate. Contrastive loss - an objective matching related samples - underlies methods from self-supervised to multimodal learning. Contrastive losses, however, can be viewed more broadly as modifying a similarity graph to indicate how samples should relate in the embedding space. This view reveals a shortcoming in contrastive learning: the similarity graph is binary, as only one sample is the related positive sample. Crucially, similarities across samples are ignored. Based on this observation, we revise the standard contrastive loss to explicitly encode how a sample relates to others. We experiment with this new objective, called $\mathbb{X}$-Sample Contrastive ($\mathbb{X}$-CLR), to train vision models based on similarities in class or text caption descriptions. Our study spans three scales: ImageNet-1k with 1 million, CC3M with 3 million, and CC12M with 12 million samples. The representations learned via our objective outperform both contrastive self-supervised and vision-language models trained on the same data across a range of tasks. When training on CC12M, we outperform CLIP by $0.6\%$ on both ImageNet and ImageNet Real. Our objective appears to work particularly well in lower-data regimes, with gains over CLIP of $16.8\%$ on ImageNet and $18.1\%$ on ImageNet Real when training with CC3M. Finally, our objective seems to encourage the model to learn representations that separate objects from their attributes and backgrounds, with gains of $3.3$-$5.6$% over CLIP on ImageNet9.

The Problem: The Binary Constraint of Contrastive Learning

Most modern representation learning relies on contrastive loss (e.g., SimCLR, CLIP). In this framework, a model learns by pulling "positive" pairs together (like an image and its augmentation, or an image and its caption) and pushing all other "negative" pairs apart.

The authors argue that this binary view is too restrictive. In a typical training batch, two different images of dogs are treated as "negatives" of each other, even though they share significant semantic content. By ignoring these cross-sample similarities, standard contrastive learning misses out on valuable signals about how different concepts relate to one another in the real world. This binary paradigm forces the model to learn a sparse similarity graph that does not reflect the continuous nature of semantic relationships.

Methodology: $\mathbb{X}$-Sample Contrastive ($\mathbb{X}$-CLR)

To address this, the authors propose $\mathbb{X}$-Sample Contrastive ($\mathbb{X}$-CLR). Instead of a hard binary target (1 for the positive sample, 0 for all others), $\mathbb{X}$-CLR uses a continuous similarity graph to define how samples should relate in the embedding space.

The diagram of X-CLR and its Python-style pseudo-code.

Key components of the method include:

  • Soft Similarity Graph: The target distribution for a sample is derived from metadata. For image-text datasets, they use a frozen text encoder (like a Sentence Transformer) to compute similarities between captions. This creates a "soft" target where a dog image is highly similar to other dog images, moderately similar to cat images, and dissimilar to images of cars.
  • Loss Function: The objective is the cross-entropy between the model's predicted image-image similarities and the precomputed soft target similarities.
  • Temperature Scaling: A hyperparameter $\tau_s$ controls the "softness" of the target graph. As $\tau_s$ approaches zero, the method recovers standard supervised contrastive learning.

Key Insights: Beyond Binary Targets

Viewing contrastive learning as "graph modification" reveals that standard methods use incredibly sparse, binary graphs. $\mathbb{X}$-CLR shows that "softening" this graph provides a much richer training signal. This is visually evident when comparing the similarity matrices produced by different training regimes.

Comparison of similarity matrices: Self-supervised (diagonal) vs. Supervised (block diagonal) vs. X-CLR (soft relationships).

By allowing for non-zero similarities between different samples, the model achieves better semantic disentanglement. Because the model isn't forced to push away every other sample, it learns to better separate objects from their backgrounds and attributes. It recognizes that two images might share a "grassy background" but have different "foreground objects," leading to improved robustness on benchmarks like ImageNet-9.

Experimental Results

The study evaluates $\mathbb{X}$-CLR across three scales of data: ImageNet-1k (1M samples), CC3M (3M noisy pairs), and CC12M (12M noisy pairs).

Performance and Data Efficiency

  • Outperforming CLIP: When trained on CC12M, $\mathbb{X}$-CLR outperforms CLIP by 0.6% on both ImageNet and ImageNet Real.
  • Low-Data Gains: The advantage is even more dramatic on the smaller CC3M dataset, where $\mathbb{X}$-CLR beats CLIP by 16.8% on ImageNet and 18.1% on ImageNet Real. This suggests that $\mathbb{X}$-CLR is significantly more data-efficient, extracting more information from the relationships between samples.
  • Robustness: On ImageNet-9, $\mathbb{X}$-CLR shows gains of 3.3-5.6% over CLIP, indicating it is much better at ignoring distracting backgrounds.
  • Label Quality: The authors found that $\mathbb{X}$-CLR trained on 1 million high-quality labels outperformed CLIP trained on 12 million noisy web captions for fine-grained tasks.

Limitations and Future Work

While powerful, $\mathbb{X}$-CLR requires metadata (like captions or labels) to build the similarity graph, meaning it cannot currently be used in a purely "unlabeled" self-supervised setting without initial clustering. Performance is also dependent on the quality of the source used to build the graph (e.g., the choice of text encoder). Future work may explore other metadata sources, such as temporal or locational data, and integrate this soft-graph approach into non-contrastive methods like VICReg or BYOL.

Summary updated

BibTeX
@inproceedings{sobal2025mathbb,
  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 = {{$\textbackslash\{\}mathbb\{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