Motivation
Contrastive representation learning trains an encoder f to map positive pairs close together. Positive pairs are typically two random augmentations of one sample, such as two crops of one image. Features of unrelated samples, called negatives, are pushed apart. Most modern methods normalize the features to the unit sphere Sm−1 and train with the contrastive (InfoNCE) loss.
Wang and Isola (2020) identify two properties that this loss optimizes:
- Alignment: positive pairs receive the same feature.
- Uniformity: the features are spread uniformly over the sphere.
They prove that as the number of negatives grows, the contrastive loss splits into one term for each property. They also introduce the uniformity loss Luniform, built from the Gaussian potential, and characterize its minimizers and its range.
This mission formalizes the theory of Section 4 and Appendix A of the paper.
Setting
Data have a distribution pdata on Rn. Positive pairs have a distribution ppos on Rn×Rn that is symmetric and whose marginal is pdata. An encoder is a measurable map f:Rn→Sm−1. With temperature τ>0 and M negatives drawn i.i.d. from pdata, the contrastive loss is
Lcontrastive(f;τ,M)=E[−logef(x)⊤f(y)/τ+∑i=1Mef(xi−)⊤f(y)/τef(x)⊤f(y)/τ],(x,y)∼ppos.
An encoder is perfectly aligned if f(x)=f(y) almost surely over positive pairs. It is perfectly uniform if the law of f(x) is the normalized surface measure σm−1.
Uniformity is measured with the Gaussian potential Gt(u,v)=e−t∥u−v∥22:
Luniform(f;t)=logEx,y i.i.d. pdataGt(f(x),f(y)).
Formalization targets
Goal: asymptotics of the contrastive loss (Theorem 1)
For fixed τ>0, as M→∞,
Lcontrastive(f;τ,M)−logM → −τ1E(x,y)∼ppos[f(x)⊤f(y)]+Ex∼pdata[logEx−∼pdataef(x−)⊤f(x)/τ].
The theorem has three further parts:
- The first term is minimized exactly by the perfectly aligned encoders.
- If perfectly uniform encoders exist, they are exactly the minimizers of the second term.
- The deviation from the limit is O(M−1/2).
Milestones
- Lemma 1: the Gaussian kernel Gt is strictly positive definite on Sd.
- Proposition 1: σd is the unique minimizer of the expected pairwise Gaussian potential over Borel probability measures on Sd.
- Proposition 2: for each N, take N points on Sd that minimize the pairwise Gaussian potential. Their normalized counting measures converge weak* to σd.
- Proposition 3: the expected pairwise Gaussian potential has range [e−2t0F1(;2d+1;t2),1]. The minimum is attained only at σd and the maximum only at Dirac masses. The minimum decreases strictly in d to e−2t.
- Corollary 1: Luniform(f;t)∈[−2t+log0F1(;2m;t2),0]. The lower end is attained exactly by perfectly uniform encoders and the upper end exactly by constant encoders.
- Theorem 1, eq. (2) and result 3: the limit of Lcontrastive−logM and its O(M−1/2) rate.
- Theorem 1, proof of result 2, step 2: at every minimizer μ∗ of the relaxed problem minμ∫logUμdμ, with Uμ(u)=∫eu⊤v/τdμ(v), the function Uμ∗ is constant μ∗-almost surely.
- Theorem 1, proof of result 2, step 3: σd is the unique minimizer of the relaxed problem.
Significance
The paper explains the contrastive loss by what it optimizes, alignment and uniformity, rather than as a bound on mutual information. Its uniformity loss is used in practice as a training objective and as an evaluation metric. The location of the logarithm matters. Pushing the log inside the inner expectation gives an objective minimized by every distribution with mean zero. In the uniformity term and in Luniform, the uniform distribution is the unique minimizer.
Difficulty
- Lemma 1 and Propositions 1-2: classical potential theory on the sphere (Borodachov, Hardin and Saff, 2019): Schoenberg-type positive definiteness and equidistribution of energy minimizers. Mathlib has the surface measure on spheres but no spherical harmonics.
- Proposition 3: evaluating ∫e2tu⊤vdσd through a Bessel-function identity, and series estimates for 0F1.
- Theorem 1: a law-of-large-numbers limit under the integral, an O(M−1/2) deviation bound, and a variational argument on the space of probability measures (existence of a minimizer by weak* compactness, then a first-variation argument).
Formalization scope
- Spaces: Rk is
EuclideanSpace ℝ (Fin k). Sd is the unit sphere of Rd+1, and probability measures on Sd are Borel probability measures on Rd+1 concentrated on it. σd is Mathlib's surface measure volume.toSphere, normalized.
- Distributions: pdata and ppos are probability measures, not necessarily with densities. The Assumption of Section 3 (symmetry and matching marginal) is a hypothesis of every statement about the contrastive loss.
- Encoders: Borel measurable maps into the unit sphere. All minimizations range over these maps.
- Signed measures: in Lemma 1, a finite signed measure is a difference of two finite measures, and its energy is expanded by bilinearity.
- Dimensions: all statements on Sd hold for every d≥0, and feature dimensions satisfy m≥1 (this is automatic, since an encoder into Sm−1 cannot exist when m=0).
Not included:
- Theorem 2 (single negative sample) is false. On S1 with τ=1, the density (1+cos4θ)/(2π) has a smaller value of ∬log(e1/τ+eu⊤v/τ)dμdμ than σ1. The paper's proof relies on a function that is claimed to be completely monotone but is not.
- Lemma 2 (Borodachov et al., Proposition 4.4.1, Theorem 6.2.1 and Corollary 6.2.2) and Lemma 3 (Serfozo 1982, Theorem 3.3) are cited results from other works, restated by the paper without proof. Lemma 2 concerns general extended-valued lower semicontinuous kernels, and its uses in this mission are Propositions 1 and 2.
- The Realizability paragraph of Section 4.2, the entropy-estimator and InfoMax remarks, and the bounds on empirical estimates of Luniform in Appendix A.1.1 are informal or unnumbered remarks.
- Sections 5 and B are experiments.
Selected references
- T. Wang and P. Isola, Understanding Contrastive Representation Learning through Alignment and Uniformity on the Hypersphere, ICML 2020, arXiv:2005.10242. https://arxiv.org/abs/2005.10242
- S. V. Borodachov, D. P. Hardin and E. B. Saff, Discrete Energy on Rectifiable Sets, Springer, 2019. https://doi.org/10.1007/978-0-387-84808-2
- R. Serfozo, Convergence of Lebesgue integrals with varying measures, Sankhyā Ser. A 44 (1982). https://www.jstor.org/stable/25050327
- A. van den Oord, Y. Li and O. Vinyals, Representation Learning with Contrastive Predictive Coding, arXiv:1807.03748, 2018. https://arxiv.org/abs/1807.03748