Optimal Sample Complexity of Contrastive Learning

Noga Alon, Dmitrii Avdiukhin, Dor Elboim, Orr Fischer, Grigory Yaroslavtsev · DOI

Introduction

Contrastive learning has become a cornerstone of modern AI, powering state-of-the-art models like CLIP and SimCLR. However, theoretical understanding has often lagged behind practical success. This research provides the first tight theoretical bounds on the sample complexity of contrastive learning, answering the fundamental question: exactly how many labeled examples are needed to learn a robust data representation?

The Problem: Quantifying Data Requirements

In contrastive learning, models learn from tuples (e.g., an anchor, a positive example, and one or more negative examples) by ensuring the anchor is more similar to the positive than to the negatives. Unlike traditional supervised learning, there are no explicit labels. This paper addresses the gap between classical PAC-learning theory—often dismissed as too pessimistic for deep learning—and the actual empirical behavior of these models. The goal is to determine the minimum number of labeled tuples required to achieve high generalization accuracy.

Theoretical Framework and Methodology

The authors frame contrastive learning within the Probably Approximately Correct (PAC) learning framework. Their primary technical tools include the Vapnik-Chervonenkis (VC) dimension for binary choices and the Natarajan dimension for settings involving multiple negative examples.

To calculate the "capacity" of various distance functions (such as $\ell_p$ norms or cosine similarity), the researchers utilize Warren’s Theorem from algebraic geometry. By treating the embedding process as a system of polynomial inequalities, they can count the number of possible "sign patterns" produced, allowing them to bound how many different ways a dataset can be labeled or "shattered." This directly leads to the derivation of sample complexity bounds.

Complexity bounds for quadruplet learning and multiple negative samples.

Key Insights

  • The $nd$ Bottleneck: For common settings like $\ell_2$ distance or cosine similarity, the complexity is $\Theta(\min(nd, n^2))$. This is intuitive as $nd$ represents the total number of parameters in an embedding matrix for $n$ points in $d$ dimensions.
  • Theory vs. Folklore: While deep learning folklore suggests that statistical learning theory cannot explain generalization due to high parameter counts, this paper argues that focusing on the output dimension ($d$) rather than the number of neurons makes the theory highly predictive.
  • The "Well-Separated" Bonus: In practice, positive examples are often significantly closer to the anchor than negatives. The authors prove that if data is "well-separated" by a factor of $(1+\alpha)$, the sample complexity becomes almost independent of the dimension $d$, explaining why contrastive learning scales so well to high-dimensional data.
  • Metric Geometry Matters: The complexity varies significantly based on the geometry of the space. For instance, learning tree-based metrics is significantly more data-efficient ($O(n \log n)$) than learning arbitrary distances ($\Theta(n^2)$).

Empirical Results and Validation

The authors validated their theory using standard benchmarks including MNIST, CIFAR-10, CIFAR-100, and ImageNet. Training ResNet-18 architectures from scratch, they observed that empirical generalization error follows the $1/\sqrt{m}$ decay (where $m$ is the number of samples) predicted by their PAC-learning bounds. Furthermore, they demonstrated that once the embedding dimension $d$ is sufficiently large, the error rate remains stable, matching their "well-separated" theoretical prediction.

Summary of sample complexity bounds for various distance metrics.

Limitations and Future Work

While the bounds for even $p$ (like $\ell_2$) are perfectly tight, a small logarithmic gap remains for odd $p$ (like $\ell_1$). Additionally, while the asymptotic behavior matches experimental curves, a constant scaling factor between theoretical bounds and observed error still requires refinement. Future research may also explore adaptive query complexity, where models actively choose "hard negatives" rather than drawing samples i.i.d. from a distribution.

Summary updated

BibTeX
@inproceedings{alon2024optimal,
  author = {Noga Alon and Dmitrii Avdiukhin and Dor Elboim and Orr Fischer and Grigory Yaroslavtsev},
  title = {{Optimal Sample Complexity of Contrastive Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=NU9AYHJvYe},
}
Figure