BatchSampler: Sampling Mini-Batches for Contrastive Learning in Vision, Language, and Graphs

Zhen Yang, Tinglin Huang, Ming Ding, Yuxiao Dong, Rex Ying, Yukuo Cen, Yangliao Geng, Jie Tang · DOI

Abstract

Contrastive learning is a dominant paradigm in self-supervised representation learning, yet its performance is heavily influenced by the quality of negative samples in a mini-batch. Standard uniform sampling often produces "easy negatives" that provide minimal gradient signal, while kNN-based sampling frequently introduces "false negatives"—different instances of the same semantic class—which can degrade model performance. We introduce BatchSampler, a modality-agnostic sampling strategy that constructs a randomized proximity graph and employs Random Walks with Restart (RWR) to identify "hard yet true" negatives. Our approach consistently outperforms traditional sampling methods across vision, language, and graph benchmarks, offering both improved accuracy and faster convergence.

Introduction

Self-supervised contrastive learning has become a cornerstone of modern AI, powering everything from image recognition to large language models. The core objective is to bring "positive" pairs (different views of the same instance) closer in representation space while pushing "negative" pairs (different instances) apart.

However, the effectiveness of this process is limited by the sampling strategy used to form mini-batches. Most models rely on Uniform Sampling, which often results in "easy negatives"—pairs so obviously different that the model learns very little from them. Conversely, kNN Sampling picks the nearest neighbors as negatives. While these are "hard" and challenging, they are often false negatives—different instances that belong to the same underlying category (e.g., two different images of dogs). Training on false negatives confuses the model and degrades performance.

Overview of the BatchSampler framework across different modalities.

BatchSampler addresses this by finding a "Goldilocks zone": negatives that are hard enough to be informative but distant enough to likely be true negatives.

Methodology

BatchSampler introduces a two-step process that can be integrated into existing contrastive learning frameworks like SimCLR, MoCo, or SimCSE.

1. Proximity Graph Construction

Instead of a full kNN graph, which is computationally expensive and prone to clustering same-class items, BatchSampler builds a randomized proximity graph. For every instance, a candidate set of $M$ items is randomly selected, and only the top $K$ most similar ones are kept as neighbors. This randomization helps break up "pure" class clusters, effectively reducing the prevalence of false negatives.

2. Random Walk with Restart (RWR) Sampling

To form a mini-batch, the sampler starts at a random node on the proximity graph and performs a random walk. At each step, it can either move to a neighbor or "teleport" back to the starting node with probability $\alpha$.

  • A high $\alpha$ keeps the walker local, identifying very hard negatives.
  • A low $\alpha$ allows the walker to explore further, finding more diverse negatives.
The BatchSampler workflow: Graph Construction and RWR Sampling.

The graph is updated periodically as the model's embeddings evolve, ensuring the samples remain relevant throughout training.

Experimental Evaluation

The researchers evaluated BatchSampler across three distinct modalities:

  • Vision: Benchmarks included CIFAR-10/100, STL-10, ImageNet-100, and ImageNet ILSVRC-2012.
  • Language: Evaluated on 7 Semantic Textual Similarity (STS) tasks using a 1-million sentence subset of English Wikipedia.
  • Graphs: Tested on seven common benchmarks including IMDB, COLLAB, REDDIT, and NCI1.

Key Insights and Results

The Power of the "In-Between"

The most significant insight is that optimal negative samples balance gradient signal (hardness) with label integrity. BatchSampler consistently outperformed standard uniform sampling:

  • Vision: On ImageNet-100, it improved SimCLR’s top-1 accuracy from 68.7% to 69.2% and reached baseline performance levels in significantly fewer epochs (e.g., 600 vs 1000 on STL-10).
  • Language: It improved SimCSE-BERT by an average of 1.09% across STS tasks and successfully "rescued" advanced loss functions like DCL and HCL, which often fail in language tasks due to high initial embedding similarity.
  • Graphs: It achieved absolute gains of 0.4% to 2.9% across various graph datasets.

Dynamic Hardness and Efficiency

The authors found that a linear decay strategy for the restart probability ($\alpha$) works best. Early in training, a low $\alpha$ encourages wide exploration; as the model matures, a higher $\alpha$ provides increasingly difficult challenges. Furthermore, the cost of graph construction is amortized over many iterations, making the sampling process significantly faster than the model's backpropagation step.

Limitations and Future Work

While effective, BatchSampler introduces new hyperparameters ($M, K, \alpha, t$) that may require tuning for new domains. Additionally, if representations change too rapidly between graph updates, the sampler may use "stale" data. Future research could explore automated parameter adjustment and scaling to web-scale datasets where graph construction bottlenecks might emerge.

Summary updated

BibTeX
@inproceedings{yang2023batchsampler,
  author = {Zhen Yang and Tinglin Huang and Ming Ding and Yuxiao Dong and Rex Ying and Yukuo Cen and Yangliao Geng and Jie Tang},
  title = {{BatchSampler: Sampling Mini-Batches for Contrastive Learning in Vision, Language, and Graphs}},
  booktitle = {ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
  year = {2023},
  doi = {10.1145/3580305.3599263},
}
Figure