Contrastive learning (CL) has become a cornerstone of modern AI, powering everything from image search to large language models. It works by teaching a model to map "similar" items closer together in a mathematical space than "dissimilar" items. Despite its empirical success, a fundamental question remains: How many samples do we actually need to learn a good representation?
In deep learning, a "folklore belief" suggests that classical learning theory (like PAC-learning) is practically useless because neural networks are so complex that theoretical bounds become vacuous. This paper challenges that belief, providing the first tight theoretical bounds on the sample complexity of contrastive learning and demonstrating that these classical statistical bounds accurately predict the behavior of modern deep learning models.
Theoretical Framework and Methods
The authors analyze contrastive learning through the lens of PAC (Probably Approximately Correct) learning. They specifically evaluate the VC dimension (for binary choices) and Natarajan dimension (for multiple choices) of various distance-based hypothesis classes.
Key technical approaches include:
- Output-Centric Analysis: Instead of analyzing the millions of parameters in a network like ResNet, the authors assume the model is trying to find an embedding into a $d$-dimensional space ($\mathbb{R}^d$).
- Algebraic Geometry: To prove bounds for $\ell_p$ distances (like Euclidean distance), they utilize tools from algebraic geometry—specifically Warren’s theorem—to count the number of possible "sign patterns" polynomials can form. This bounds how many different ways a set of points can be classified by distance relations.
- Diverse Metric Settings: The analysis extends beyond standard Euclidean space, providing bounds for arbitrary distances, tree metrics, and cosine similarity.
Key Insights
- The Dimensionality Factor: For $\ell_p$ distances, the sample complexity is $\tilde{\Theta}(nd)$. The amount of data needed scales linearly with both the number of unique items in the dataset ($n$) and the dimension of the representation ($d$).
- Theory is Not Vacuous: The most striking insight is that classical VC-dimension bounds have strong predictive power for deep learning. By scaling their theoretical curve by a single constant, the authors found that predicted error rates almost perfectly matched empirical errors across different datasets and embedding dimensions.
- Well-Separated Samples: In many practical cases, the "positive" example is much closer to the anchor than the "negative" one. In this "well-separated" setting, sample complexity becomes nearly independent of the dimension $d$, explaining why CL works so well even with very high-dimensional embeddings.
- Tree Metrics: If data naturally fits a hierarchy (a tree), only roughly $O(n \log n)$ samples are needed, which is significantly more efficient than general spatial embeddings.
Main Results and Bounds
The paper establishes a comprehensive "menu" of sample complexities for different settings, where $n$ is the number of points and $d$ is the embedding dimension.
As shown in the table above, the bounds are tight for most settings. For instance, learning arbitrary distances requires $\Theta(n^2)$ samples, while cosine similarity and even-$p$ $\ell_p$ distances require $\Theta(nd)$ samples.
Empirical Validation
The authors validated their theoretical findings using standard computer vision benchmarks, including CIFAR-10, CIFAR-100, MNIST, Fashion-MNIST, and ImageNet. They trained ResNet-18 architectures from scratch using various contrastive losses.
In these experiments, they showed that the generalization gap (the difference between training and test error) scales as $\approx \sqrt{n/m}$, where $m$ is the number of samples. This relationship held true whether the models used 32-dimensional or 1024-dimensional embeddings.
Limitations and Future Work
- Odd $p$ Values: For $\ell_p$ distances where $p$ is odd (like Manhattan distance), there remains a small logarithmic gap between the lower and upper bounds ($nd$ vs $nd \log n$).
- Adaptive Queries: The current paper assumes samples are drawn randomly (non-adaptive). In practice, techniques like "hard negative mining" (picking the most difficult samples) are common. Exploring adaptive sample complexity is a vital next step.
- The Constant Factor: While the shape of the error curve matches theory perfectly, the theory doesn't yet predict the exact constant factor (the "steepness" of the curve) for a specific neural architecture; this currently must be estimated experimentally.
Summary updated