Contrastive Training with more data

Stephen Mander, Scott Piao, Hossein RahmaniOpenReview 2023 · Submission to Tiny Papers @ ICLR

Abstract: This paper proposes a new method of contrastive training over multiple data points, focusing on the scaling issue present when using in-batch negatives. Our approach compares transformer training with dual encoders vs training with multiple encoders. Our method can provide a feasible approach to improve loss modelling as encoders scale.

Introduction

Contrastive learning—the engine behind models like CLIP—relies on comparing "positive" pairs (e.g., an image and its correct caption) against "negative" pairs (the image and every other caption in the batch). The effectiveness of this training is heavily tied to the ratio of positive to negative samples.

Currently, most systems use two encoders. When researchers attempt to add a third or fourth data stream (like audio or depth data), they typically perform multiple pairwise comparisons (e.g., Image-Text, Image-Audio, Text-Audio). This approach is inefficient: it does not actually increase the pool of negative samples relative to the positives, and it introduces complexity in balancing learning rates across different modalities. Furthermore, the mathematics of "similarity" become problematic when attempting to dot-product more than two vectors at once. This paper proposes a method to scale contrastive learning beyond the standard dual-encoder setup, exponentially increasing negative samples to build more robust feature representations.

Methodology

The Logit Cube and Beyond Pairs

Instead of comparing an input $X$ to $Y$, the proposed model evaluates $n$ encoders simultaneously. This shifts the loss space from a 2D matrix of similarities to an $n$-dimensional "cube" (or hypercube) of logits.

Visualization of the 3D logit cube for a batch size of 10 across 3 encoders, highlighting the target true diagonal.

The goal of the training is to maximize values along the "true diagonal" of this cube, where all $n$ inputs represent the same underlying concept.

Variance-Based Similarity

The authors identify a geometric pitfall in standard approaches: while the dot product $a \cdot b$ is an excellent similarity metric, $a \cdot b \cdot c$ is not. For example, if all three vectors are negative, their product is negative, which would erroneously suggest low similarity.

To resolve this, the authors define similarity ($S$) based on the variance (L2 distance to the mean) of the features across the different encoders. If the features from all $n$ sources are very close to their collective mean, the variance is low, and the similarity is high.

Computational Optimization

Calculating variance for every combination in a batch is highly memory-intensive, scaling at $B^n$. To make larger batch sizes feasible, the authors derive a simplified mathematical formula that allows this "distance to mean" to be calculated much more efficiently, significantly reducing the computational footprint.

Key Insights and Results

  • Exponential Scaling of Negatives: In a standard batch of size $B$ with two encoders, there are $B-1$ negatives for every positive. By moving to $n$ encoders, the number of negatives grows to $B^n - 1$. This massive increase in "distractors" forces the model to learn much finer-grained distinctions.
  • Mathematical Efficiency: The primary result of the paper is the derivation of a "corrected loss" formula. By converting the variance calculation into a form that uses the sum of squares and the square of sums, the authors demonstrate that significantly less computation is needed to handle $n$-dimensional arrays.
  • Visual Proof of Concept: The authors successfully illustrate a mapping of logits for a batch size of 10 across 3 encoders, proving how the target diagonal can be isolated within the massive $B^n$ space.

Limitations and Future Work

The paper focuses heavily on mathematical derivation and the theoretical framework of the loss function, meaning empirical benchmarking on specific datasets is not discussed. Consequently, there is a lack of experimental results showing that this method outperforms standard CLIP-style training on tasks like image classification or retrieval.

Furthermore, hardware constraints remain a challenge. Even with the optimized formula, the $B^n$ scaling is aggressive and drastically reduces the achievable batch size for a finite amount of hardware. Finally, while the method simplifies the architecture, balancing learning rates between different intra-modal parts of the network remains a significant implementation hurdle. Future work will likely involve testing this loss function on large-scale multimodal datasets to determine if the theoretical boost in negative samples translates to better zero-shot performance.

Summary updated

Abstract

This paper proposes a new method of contrastive training over multiple data points, focusing on the scaling issue present when using in-batch negatives. Our approach compares transformer training with dual encoders vs training with multiple encoders. Our method can provide a feasible approach to improve loss modelling as encoders scale.
BibTeX
@misc{mander2023contrastive,
  author = {Stephen Mander and Scott Piao and Hossein Rahmani},
  title = {{Contrastive Training with more data}},
  year = {2023},
  url = {https://openreview.net/forum?id=ZTp85mW5nFy},
  howpublished = {OpenReview},
  note = {Submitted to Tiny Papers @ ICLR},
}
Figure