Abstract
In this paper, we study contrastive learning from an optimization perspective, aiming to analyze and address a fundamental issue of existing contrastive learning methods that either rely on a large batch size or a large dictionary of feature vectors. We consider a global objective for contrastive learning, which contrasts each positive pair with all negative pairs for an anchor point. From the optimization perspective, we explain why existing methods such as SimCLR require a large batch size in order to achieve a satisfactory result. In order to remove such requirement, we propose a memory-efficient Stochastic Optimization algorithm for solving the Global objective of Contrastive Learning of Representations, named SogCLR. We show that its optimization error is negligible under a reasonable condition after a sufficient number of iterations or is diminishing for a slightly different global contrastive objective. Empirically, we demonstrate that SogCLR with small batch size (e.g., 256) can achieve similar performance as SimCLR with large batch size (e.g., 8192) on self-supervised learning task on ImageNet-1K.
Introduction
Contrastive learning methods like SimCLR have become the industry standard for self-supervised representation learning. However, they suffer from a "batch size curse": they typically require massive mini-batches (e.g., 8192) to perform well. This creates a significant hardware barrier, as such large batches require extensive GPU clusters.
The root of this problem lies in the gradient bias. Because SimCLR only looks for negative samples within the current mini-batch, its gradient updates become biased when the batch size is small. While methods like MoCo use a "memory bank" to store previous features, these are often seen as heuristic workarounds. This research asks whether we can mathematically define a global objective and optimize it efficiently without needing massive batches or complex dictionaries.
Methodology: Global Contrastive Objective (GCO)
The authors reframe contrastive learning as a Global Contrastive Objective (GCO). Instead of contrasting an image against a few neighbors in a batch, the model contrasts it against the entire dataset. To solve this without exceeding GPU memory, they propose SogCLR (Stochastic Optimization for Global Contrastive Learning).
Scalar Tracking and Moving Averages
SogCLR maintains a single scalar value ($u_i$) for every image in the dataset. This scalar tracks the running average of that image's "negative" contributions (the denominator in the InfoNCE loss). During training, when an image is sampled, its specific scalar is updated using a moving average.
Provable Convergence
The authors provide a theoretical proof that SimCLR’s gradient is inherently biased, with an optimization error proportional to $1/\sqrt{\text{Batch Size}}$. In contrast, SogCLR is shown to have a diminishing optimization error. This means that given enough time, it will converge to a high-quality solution regardless of batch size.
Experimental Results
The approach was validated on standard benchmarks including ImageNet-1K and ImageNet-S.
Impact of Batch Size
A key finding is that SogCLR is remarkably robust to changes in batch size. On ImageNet-1K, SogCLR with a batch size of 256 achieved 69.4% top-1 accuracy, matching SimCLR's performance at a batch size of 8192 (69.3%).
As shown in the figure above, as batch sizes decreased from 8192 down to 128, SimCLR’s performance dropped by nearly 5%, while SogCLR’s performance remained stable, dropping less than 1%.
Efficiency and Scalability
SogCLR is highly memory-efficient, needing to store only one scalar per image ($O(n)$ memory). For ImageNet-1K, this is just a few megabytes. Furthermore, the algorithm exhibits a linear speedup; while it works with small batches, it can benefit from larger ones by converging in fewer total iterations.
Conclusion
SogCLR removes the hardware barrier for high-performance contrastive learning by enabling training with standard batch sizes. While storing a scalar per image is efficient for datasets like ImageNet, future work may explore scaling this to "web-scale" datasets with billions of images. The proposed optimization technique is generic and can be applied to other complex losses, such as two-way contrastive losses for bimodal (vision + language) learning.
The code is available in the open-source library LibAUC.
Summary updated