Tail Batch Sampling: Approximating Global Contrastive Losses as Optimization over Batch Assignments

Vin Sachidananda, Ziyi Yang, Chenguang ZhuarXiv 2022

Abstract

Contrastive Learning has recently achieved state-of-the-art performance in a wide range of tasks. Many contrastive learning approaches use mined hard negatives to make batches more informative during training but these approaches are inefficient as they increase epoch length proportional to the number of mined negatives and require frequent updates of nearest neighbor indices or mining from recent batches. In this work, we provide an alternative to hard negative mining, Global Contrastive Batch Sampling (GCBS), an efficient approximation to the batch assignment problem that upper bounds the gap between the global and training losses, $\mathcal{L}^{Global} - \mathcal{L}^{Train}$, in contrastive learning settings. Through experimentation we find GCBS improves state-of-the-art performance in sentence embedding and code-search tasks. Additionally, GCBS is easy to implement as it requires only a few additional lines of code, does not maintain external data structures such as nearest neighbor indices, is more computationally efficient than the most minimal hard negative mining approaches, and makes no changes to the model being trained.

Introduction

Contrastive learning relies on comparing "positive" pairs (similar items) against "negative" pairs (dissimilar items). To learn effectively, models need hard negatives—dissimilar items that the model currently thinks are similar. The standard approach, Hard Negative Mining, searches the entire dataset for the most confusing examples for every sample. However, this is computationally expensive, often multiplying training time by the number of negatives mined, and requires maintaining complex external data structures like nearest-neighbor indices.

This paper introduces Global Contrastive Batch Sampling (GCBS), a method that improves contrastive learning by optimizing how data is partitioned into batches. By treating batch assignment as a global permutation problem, GCBS provides "harder" negatives more efficiently than traditional mining techniques.

Methodology: Batching as a Permutation Problem

The core innovation of GCBS is moving from greedy, local mining to global optimization. The authors identify a fundamental gap between the Global Loss (calculated over all $N^2$ possible pairs) and the Training Loss (calculated only on the subset of pairs within the same batch). GCBS aims to minimize this gap by strategically reordering the dataset so that hard negatives are naturally "clumped" into the same batches.

Comparison of Global Loss, Randomized Batching, and Optimized Permutation (GCBS). GCBS reorders the dataset to concentrate high-loss pairs (darker pixels) into the training batches along the diagonal.

Theoretical Framework

The authors prove that the gap between global and training losses is upper-bounded by a Quadratic Bottleneck Assignment Problem (QBAP). To make training most effective, the goal is to maximize the similarity of the "hardest" negatives within each batch. Since QBAP is NP-Hard, the problem is relaxed into a matrix bandwidth minimization task.

Implementation with Cuthill-McKee

The dataset's similarities are represented as a sparse matrix, keeping only the top $q$ percentile of similarities. GCBS then seeks a permutation that moves these high-similarity "negative" values as close to the main diagonal as possible. This is solved efficiently using the Cuthill-McKee algorithm, a classic graph theory technique.

GCBS is remarkably lightweight, requiring no changes to the model architecture and can be implemented in less than 50 lines of PyTorch code. It runs once at the start of each epoch to reshuffle the data based on the model's current state.

Evaluation and Results

The authors evaluated GCBS across three domains: sentence embeddings (STS, SICK-R), code search (CodeSearchNet, CosQA), and computer vision (Imagenette, CIFAR-10).

Key Findings

  • Performance Gains: On the CodeSearchNet (CSN) benchmark, GCBS improved the state-of-the-art UniXcoder model by 2.2 points (MRR). In sentence embeddings, it improved RoBERTa-large models by over 1.0 points on average.
  • Efficiency: GCBS is significantly faster than hard negative mining. For the Code Search (Ruby) task, GCBS took ~502 seconds per epoch, compared to ~882 seconds for a minimal hard negative mining implementation—a 40% reduction in time.
  • Loss Approximation: Empirical tests showed that GCBS reduces the gap between training and global loss by 40% compared to random sampling.

Conclusion and Future Work

GCBS demonstrates that "mining" doesn't have to be an active search during training. By reordering the training list, hard negatives can be efficiently grouped. While faster than mining, GCBS still incurs a cost due to the similarity matrix computation at each epoch. Future work may involve scaling to massive datasets (hundreds of millions of pairs) using space partitioning techniques like FAISS to maintain tractability.

Summary updated

BibTeX
@misc{sachidananda2022tail,
  author = {Vin Sachidananda and Ziyi Yang and Chenguang Zhu},
  title = {{Tail Batch Sampling: Approximating Global Contrastive Losses as Optimization over Batch Assignments}},
  year = {2022},
  eprint = {2210.12874},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2210.12874},
}
Figure