Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Contrastive loss, alignment and uniformity, and Gaussian energies on the hypersphere

Definition
AlignmentUniformity_Basic

by Xeroth · Nov 23, 2026 · Mathlib 0df444a (Lean v4.33.1)

contrastive-learningenergy-minimizationhypersphererepresentation-learninguniform-distribution

Objects of Sections 3, 4.1 and 4.2 and Appendix A of Wang and Isola (2020).

  1. Spaces. Data live in Rn\mathbb{R}^nRn and features in Rm\mathbb{R}^mRm, both with the Euclidean norm. Sm−1={u∈Rm:∥u∥2=1}S^{m-1}=\{u\in\mathbb{R}^m:\|u\|_2=1\}Sm−1={u∈Rm:∥u∥2​=1}, and SdS^dSd is the unit sphere of Rd+1\mathbb{R}^{d+1}Rd+1.
  2. Gaussian potential. For t>0t>0t>0, Gt(u,v)=e−t∥u−v∥22G_t(u,v)=e^{-t\|u-v\|_2^2}Gt​(u,v)=e−t∥u−v∥22​.
  3. Measures on the sphere. M(Sd)\mathcal{M}(S^d)M(Sd) is the set of Borel probability measures on Rd+1\mathbb{R}^{d+1}Rd+1 that give no mass to the complement of SdS^dSd. σd\sigma_dσd​ is the normalized surface area measure of SdS^dSd.
  4. Energies. For a kernel KKK and finite measures μ,ν\mu,\nuμ,ν: IK(μ,ν)=∫ ⁣∫K(u,v) dν(v) dμ(u)I_K(\mu,\nu)=\int\!\int K(u,v)\,d\nu(v)\,d\mu(u)IK​(μ,ν)=∫∫K(u,v)dν(v)dμ(u), and IK[μ]=IK(μ,μ)I_K[\mu]=I_K(\mu,\mu)IK​[μ]=IK​(μ,μ). The energy of the finite signed measure ν1−ν2\nu_1-\nu_2ν1​−ν2​ is IK(ν1,ν1)−IK(ν1,ν2)−IK(ν2,ν1)+IK(ν2,ν2)I_K(\nu_1,\nu_1)-I_K(\nu_1,\nu_2)-I_K(\nu_2,\nu_1)+I_K(\nu_2,\nu_2)IK​(ν1​,ν1​)−IK​(ν1​,ν2​)−IK​(ν2​,ν1​)+IK​(ν2​,ν2​).
  5. Point configurations. For u1,…,uNu_1,\dots,u_Nu1​,…,uN​, the pairwise potential is ∑1≤i<j≤NGt(ui,uj)\sum_{1\le i<j\le N}G_t(u_i,u_j)∑1≤i<j≤N​Gt​(ui​,uj​) and the normalized counting measure is 1N∑iδui\frac1N\sum_i\delta_{u_i}N1​∑i​δui​​.
  6. Weak* convergence. μn→μ\mu_n\to\muμn​→μ weak* if ∫f dμn→∫f dμ\int f\,d\mu_n\to\int f\,d\mu∫fdμn​→∫fdμ for every continuous f:Rk→Rf:\mathbb{R}^k\to\mathbb{R}f:Rk→R.
  7. Hypergeometric function (eq. 5). 0F1(;α;z)=∑n≥0zn(α)n n!_0F_1(;\alpha;z)=\sum_{n\ge0}\frac{z^n}{(\alpha)_n\,n!}0​F1​(;α;z)=∑n≥0​(α)n​n!zn​, with (α)0=1(\alpha)_0=1(α)0​=1 and (α)n=α(α+1)⋯(α+n−1)(\alpha)_n=\alpha(\alpha+1)\cdots(\alpha+n-1)(α)n​=α(α+1)⋯(α+n−1).
  8. Relaxed problem (eqs. 12-13). Uμ(u)=∫eu⊤v/τ dμ(v)U_\mu(u)=\int e^{u^\top v/\tau}\,d\mu(v)Uμ​(u)=∫eu⊤v/τdμ(v), and the objective is ∫log⁡Uμ(u) dμ(u)\int\log U_\mu(u)\,d\mu(u)∫logUμ​(u)dμ(u).
  9. Data and positive pairs (Assumption, Section 3). pdatap_{\mathrm{data}}pdata​ is a distribution on Rn\mathbb{R}^nRn. pposp_{\mathrm{pos}}ppos​ is a distribution on Rn×Rn\mathbb{R}^n\times\mathbb{R}^nRn×Rn that is symmetric under (x,y)↦(y,x)(x,y)\mapsto(y,x)(x,y)↦(y,x) and whose first marginal is pdatap_{\mathrm{data}}pdata​.
  10. Encoders. An encoder is a Borel measurable f:Rn→Rmf:\mathbb{R}^n\to\mathbb{R}^mf:Rn→Rm with ∥f(x)∥2=1\|f(x)\|_2=1∥f(x)∥2​=1 for every xxx.
  11. Contrastive loss (eq. 1). For τ>0\tau>0τ>0 and MMM negatives,
Lcontrastive(f;τ,M)=E(x,y)∼ppos, xi−∼i.i.d.pdata[−log⁡ef(x)⊤f(y)/τef(x)⊤f(y)/τ+∑i=1Mef(xi−)⊤f(y)/τ].\mathcal{L}_{\mathsf{contrastive}}(f;\tau,M)=\mathbb{E}_{(x,y)\sim p_{\mathrm{pos}},\ x_i^-\overset{\text{i.i.d.}}{\sim}p_{\mathrm{data}}}\Big[-\log\frac{e^{f(x)^\top f(y)/\tau}}{e^{f(x)^\top f(y)/\tau}+\sum_{i=1}^Me^{f(x_i^-)^\top f(y)/\tau}}\Big].Lcontrastive​(f;τ,M)=E(x,y)∼ppos​, xi−​∼i.i.d.pdata​​[−logef(x)⊤f(y)/τ+∑i=1M​ef(xi−​)⊤f(y)/τef(x)⊤f(y)/τ​].
  1. The two limit terms (eq. 2). The alignment term is −1τE(x,y)∼ppos[f(x)⊤f(y)]-\frac1\tau\mathbb{E}_{(x,y)\sim p_{\mathrm{pos}}}[f(x)^\top f(y)]−τ1​E(x,y)∼ppos​​[f(x)⊤f(y)]. The uniformity term is Ex∼pdata[log⁡Ex−∼pdata[ef(x−)⊤f(x)/τ]]\mathbb{E}_{x\sim p_{\mathrm{data}}}\big[\log\mathbb{E}_{x^-\sim p_{\mathrm{data}}}[e^{f(x^-)^\top f(x)/\tau}]\big]Ex∼pdata​​[logEx−∼pdata​​[ef(x−)⊤f(x)/τ]].
  2. Alignment and uniformity losses (Section 4.1). Lalign(f;α)=E(x,y)∼ppos∥f(x)−f(y)∥2α\mathcal{L}_{\mathsf{align}}(f;\alpha)=\mathbb{E}_{(x,y)\sim p_{\mathrm{pos}}}\|f(x)-f(y)\|_2^\alphaLalign​(f;α)=E(x,y)∼ppos​​∥f(x)−f(y)∥2α​ and Luniform(f;t)=log⁡Ex,y∼i.i.d.pdataGt(f(x),f(y))\mathcal{L}_{\mathsf{uniform}}(f;t)=\log\mathbb{E}_{x,y\overset{\text{i.i.d.}}{\sim}p_{\mathrm{data}}}G_t(f(x),f(y))Luniform​(f;t)=logEx,y∼i.i.d.pdata​​Gt​(f(x),f(y)).
  3. Perfect alignment and uniformity (Section 4.2). fff is perfectly aligned if f(x)=f(y)f(x)=f(y)f(x)=f(y) almost surely over (x,y)∼ppos(x,y)\sim p_{\mathrm{pos}}(x,y)∼ppos​. It is perfectly uniform if the law of f(x)f(x)f(x), x∼pdatax\sim p_{\mathrm{data}}x∼pdata​, is σm−1\sigma_{m-1}σm−1​.

Formalization Note Rk\mathbb{R}^kRk is EuclideanSpace ℝ (Fin k). Measures on the sphere are measures on the ambient space that are concentrated on the sphere. σd\sigma_dσd​ is Mathlib's Measure.toSphere of Lebesgue measure, divided by its total mass and pushed forward to the ambient space. The paper writes pdatap_{\mathrm{data}}pdata​ and pposp_{\mathrm{pos}}ppos​ as densities. Here they are probability measures, which is also how the paper uses them for finite datasets in Section 4.2. A finite signed measure is written as a difference ν1−ν2\nu_1-\nu_2ν1​−ν2​ of finite measures, and its energy is expanded by bilinearity.

Definition code
import Mathlib

/-!
Wang and Isola, *Understanding Contrastive Representation Learning through Alignment and
Uniformity on the Hypersphere*, ICML 2020, arXiv:2005.10242v10: the data and positive-pair
distributions and the contrastive loss of Section 3, the alignment and uniformity losses, the
Gaussian potential, the uniform distribution on the sphere and weak* convergence of Section 4.1,
perfect alignment and perfect uniformity of Section 4.2, and the energies, the hypergeometric
function `₀F₁` and the relaxed problem of Appendix A.
-/

open MeasureTheory Filter Topology

noncomputable section

namespace AlignmentUniformity

/-- The Euclidean space `ℝ^k`. Data live in `ℝ^n`; features live in `ℝ^m`, on the unit sphere
`S^{m-1}`; the sphere `S^d` of Section 4.1 is the unit sphere of `ℝ^{d+1}`. -/
abbrev Vec (k : ℕ) := EuclideanSpace ℝ (Fin k)

section Sphere

variable {k : ℕ}

/-- Section 4.1.2, p. 4: the Gaussian potential kernel `G_t(u, v) = exp(-t ‖u - v‖₂²)`. -/
def gaussKernel (t : ℝ) (u v : Vec k) : ℝ := Real.exp (-t * ‖u - v‖ ^ 2)

/-- Proposition 1 (Section 4.1.2, p. 5) and Appendix A.1, p. 14: `μ ∈ 𝓜(S^{k-1})`, i.e. `μ` is a
Borel probability measure on the unit sphere, viewed as a Borel probability measure on `ℝ^k`
that gives no mass to the complement of the sphere. -/
def IsSphereProb (μ : Measure (Vec k)) : Prop :=
  IsProbabilityMeasure μ ∧ μ (Metric.sphere (0 : Vec k) 1)ᶜ = 0

/-- Definition (Uniform distribution on `S^d`), Section 4.1.2, p. 5: `σ` is the normalized surface
area measure on the unit sphere of `ℝ^k`, pushed forward to `ℝ^k`. The surface area measure is
Mathlib's `Measure.toSphere` of Lebesgue measure, `s ↦ k · vol {r x : 0 < r < 1, x ∈ s}`. -/
def sphereUnif (k : ℕ) : Measure (Vec k) :=
  ((((volume : Measure (Vec k)).toSphere Set.univ)⁻¹ • (volume : Measure (Vec k)).toSphere)).map
    Subtype.val

/-- Appendix A.1, p. 14 (Definition of strict positive definiteness): the energy
`∫∫ K(u, v) dν(v) dμ(u)`. For `μ = ν` this is `I_K[μ]`; for `K = G_t` it is the expected pairwise
Gaussian potential of Proposition 1, eq. (4). -/
def energy (K : Vec k → Vec k → ℝ) (μ ν : Measure (Vec k)) : ℝ :=
  ∫ u, ∫ v, K u v ∂ν ∂μ

/-- Appendix A.1, p. 14 (Definition of strict positive definiteness): the energy `I_K[ν₁ - ν₂]` of
the finite signed measure `ν₁ - ν₂`, expanded by bilinearity. -/
def signedEnergy (K : Vec k → Vec k → ℝ) (ν₁ ν₂ : Measure (Vec k)) : ℝ :=
  energy K ν₁ ν₁ - energy K ν₁ ν₂ - energy K ν₂ ν₁ + energy K ν₂ ν₂

/-- Proposition 2, Section 4.1.2, p. 5: the pairwise potential `∑_{1 ≤ i < j ≤ N} G_t(u_i, u_j)`
of an `N`-point configuration. -/
def pairPotential (t : ℝ) {N : ℕ} (u : Fin N → Vec k) : ℝ :=
  ∑ i, ∑ j ∈ Finset.Ioi i, gaussKernel t (u i) (u j)

/-- Proposition 2, Section 4.1.2, p. 5: the normalized counting measure `(1/N) ∑_i δ_{u_i}` of an
`N`-point configuration. -/
def normCounting {N : ℕ} (u : Fin N → Vec k) : Measure (Vec k) :=
  (N : ENNReal)⁻¹ • ∑ i, Measure.dirac (u i)

/-- Definition (Weak* convergence of measures), Section 4.1.2, p. 5: `μ_n → μ` weak* if
`∫ f dμ_n → ∫ f dμ` for every continuous `f : ℝ^k → ℝ`. -/
def WeakStarTendsto (μs : ℕ → Measure (Vec k)) (μ : Measure (Vec k)) : Prop :=
  ∀ f : Vec k → ℝ, Continuous f →
    Tendsto (fun n => ∫ x, f x ∂(μs n)) atTop (𝓝 (∫ x, f x ∂μ))

/-- Appendix A.1, eq. (5), p. 15: the confluent hypergeometric limit function
`₀F₁(; α; z) = ∑_{n ≥ 0} z^n / ((α)_n n!)`, with `(α)_n = α (α + 1) ⋯ (α + n - 1)` the rising
Pochhammer symbol. -/
def hyp0F1 (α z : ℝ) : ℝ :=
  ∑' n : ℕ, z ^ n / ((ascPochhammer ℝ n).eval α * (n.factorial : ℝ))

/-- Appendix A.2, eq. (12), p. 20: `U_μ(u) = ∫ exp(uᵀv / τ) dμ(v)`. -/
def expPotential (τ : ℝ) (μ : Measure (Vec k)) (u : Vec k) : ℝ :=
  ∫ v, Real.exp (inner ℝ u v / τ) ∂μ

/-- Appendix A.2, eq. (13), p. 20: the objective `∫ log U_μ(u) dμ(u)` of the relaxed problem. -/
def relaxedObjective (τ : ℝ) (μ : Measure (Vec k)) : ℝ :=
  ∫ u, Real.log (expPotential τ μ u) ∂μ

end Sphere

section Encoders

variable {n m : ℕ}

/-- Assumption, Section 3, p. 3: `p_pos` is symmetric and its marginal is `p_data`
(`p_pos(x, y) = p_pos(y, x)` and `∫ p_pos(x, y) dy = p_data(x)`), written for distributions. -/
def IsPosPairLaw (pdata : Measure (Vec n)) (ppos : Measure (Vec n × Vec n)) : Prop :=
  ppos.map Prod.swap = ppos ∧ ppos.map Prod.fst = pdata

/-- Section 3, p. 3 and Section 4.2, p. 5: an encoder `f : ℝ^n → S^{m-1}` is a Borel measurable map
whose values are `ℓ₂`-normalized feature vectors. -/
def IsEncoder (f : Vec n → Vec m) : Prop :=
  Measurable f ∧ ∀ x, ‖f x‖ = 1

/-- Eq. (1), Section 3, p. 3: the contrastive loss
`E_{(x,y) ∼ p_pos, x⁻ᵢ ∼ p_data i.i.d.} [-log (e^{f(x)ᵀf(y)/τ} /
(e^{f(x)ᵀf(y)/τ} + ∑_{i=1}^M e^{f(x⁻ᵢ)ᵀf(y)/τ}))]` with `M` negative samples. -/
def contrastiveLoss (pdata : Measure (Vec n)) (ppos : Measure (Vec n × Vec n))
    (f : Vec n → Vec m) (τ : ℝ) (M : ℕ) : ℝ :=
  ∫ w, -Real.log (Real.exp (inner ℝ (f w.1.1) (f w.1.2) / τ) /
      (Real.exp (inner ℝ (f w.1.1) (f w.1.2) / τ) +
        ∑ i, Real.exp (inner ℝ (f (w.2 i)) (f w.1.2) / τ)))
    ∂(ppos.prod (Measure.pi fun _ : Fin M => pdata))

/-- Eq. (2), Theorem 1, Section 4.2, p. 6: the first (alignment) term of the limit,
`-(1/τ) E_{(x,y) ∼ p_pos}[f(x)ᵀ f(y)]`. -/
def alignTerm (ppos : Measure (Vec n × Vec n)) (f : Vec n → Vec m) (τ : ℝ) : ℝ :=
  -(1 / τ) * ∫ z, inner ℝ (f z.1) (f z.2) ∂ppos

/-- Eq. (2), Theorem 1, Section 4.2, p. 6: the second (uniformity) term of the limit,
`E_{x ∼ p_data}[log E_{x⁻ ∼ p_data}[e^{f(x⁻)ᵀ f(x)/τ}]]`. -/
def uniformTerm (pdata : Measure (Vec n)) (f : Vec n → Vec m) (τ : ℝ) : ℝ :=
  ∫ x, Real.log (∫ x', Real.exp (inner ℝ (f x') (f x) / τ) ∂pdata) ∂pdata

/-- Section 4.1.1, p. 4: the alignment loss `L_align(f; α) = E_{(x,y) ∼ p_pos} ‖f(x) - f(y)‖₂^α`,
`α > 0`. -/
def alignLoss (ppos : Measure (Vec n × Vec n)) (f : Vec n → Vec m) (α : ℝ) : ℝ :=
  ∫ z, ‖f z.1 - f z.2‖ ^ α ∂ppos

/-- Section 4.1.2, p. 4: the uniformity loss
`L_uniform(f; t) = log E_{x, y ∼ p_data i.i.d.}[G_t(f(x), f(y))]`, `t > 0`. -/
def uniformLoss (pdata : Measure (Vec n)) (f : Vec n → Vec m) (t : ℝ) : ℝ :=
  Real.log (∫ x, ∫ y, gaussKernel t (f x) (f y) ∂pdata ∂pdata)

/-- Definition (Perfect Alignment), Section 4.2, p. 6: `f(x) = f(y)` almost surely over
`(x, y) ∼ p_pos`. -/
def PerfectlyAligned (ppos : Measure (Vec n × Vec n)) (f : Vec n → Vec m) : Prop :=
  ∀ᵐ z ∂ppos, f z.1 = f z.2

/-- Definition (Perfect Uniformity), Section 4.2, p. 6: the distribution of `f(x)`, `x ∼ p_data`,
is the uniform distribution `σ_{m-1}` on `S^{m-1}`. -/
def PerfectlyUniform (pdata : Measure (Vec n)) (f : Vec n → Vec m) : Prop :=
  pdata.map f = sphereUnif m

end Encoders

end AlignmentUniformity
Source
Wang and Isola, Understanding Contrastive Representation Learning through Alignment and Uniformity on the Hypersphere, ICML 2020, arXiv:2005.10242v10, Section 3 (Assumption, eq. (1)), Sections 4.1-4.2 (Definitions of the uniform distribution, weak* convergence, perfect alignment and perfect uniformity; L_align, L_uniform, eq. (2)), Appendix A.1-A.2 (eqs. (4), (5), (12), (13))
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

Ambient space. Rk\mathbb{R}^kRk denotes the Euclidean space of real kkk-tuples. It carries the Euclidean norm ∥⋅∥\|\cdot\|∥⋅∥, the standard inner product ⟨u,v⟩=∑iuivi\langle u,v\rangle=\sum_i u_iv_i⟨u,v⟩=∑i​ui​vi​, and its Borel σ-algebra. Here kkk is any natural number, including k=0k=0k=0, where R0={0}\mathbb{R}^0=\{0\}R0={0}. The unit sphere is S={x∈Rk:∥x∥=1}S=\{x\in\mathbb{R}^k:\|x\|=1\}S={x∈Rk:∥x∥=1}; it is empty when k=0k=0k=0.


Gaussian kernel gaussKernel. For any real ttt and any u,v∈Rku,v\in\mathbb{R}^ku,v∈Rk,

Gt(u,v)=exp⁡ ⁣(−t ∥u−v∥2).G_t(u,v)=\exp\!\big(-t\,\|u-v\|^2\big).Gt​(u,v)=exp(−t∥u−v∥2).

There is no sign condition on ttt. For t<0t<0t<0 the kernel exceeds 111 off the diagonal, and for t=0t=0t=0 it is identically 111. The points u,vu,vu,v are arbitrary and need not lie on the sphere.


Sphere probability measures IsSphereProb. For a measure μ\muμ on Rk\mathbb{R}^kRk, the property holds exactly when both of the following are true:

  • μ\muμ is a probability measure, i.e. μ(Rk)=1\mu(\mathbb{R}^k)=1μ(Rk)=1;
  • μ(Rk∖S)=0\mu(\mathbb{R}^k\setminus S)=0μ(Rk∖S)=0.

When k=0k=0k=0 the sphere is empty, so these two conditions contradict each other and no measure has the property.


Uniform measure on the sphere sphereUnif. Write λ\lambdaλ for Lebesgue measure on Rk\mathbb{R}^kRk. The surface measure ς\varsigmaς on SSS assigns to a set A⊆SA\subseteq SA⊆S the value

ς(A)=k⋅λ({rx:0<r<1, x∈A}).\varsigma(A)=k\cdot\lambda\big(\{r x: 0<r<1,\ x\in A\}\big).ς(A)=k⋅λ({rx:0<r<1, x∈A}).

The measure σk\sigma_kσk​ is defined by normalising ς\varsigmaς and then viewing it as a measure on all of Rk\mathbb{R}^kRk:

σk(B)=ς(B∩S)ς(S)for Borel B⊆Rk.\sigma_k(B)=\frac{\varsigma(B\cap S)}{\varsigma(S)}\quad\text{for Borel } B\subseteq\mathbb{R}^k.σk​(B)=ς(S)ς(B∩S)​for Borel B⊆Rk.

The reciprocal here is taken in [0,∞][0,\infty][0,∞].

  • For k≥1k\ge 1k≥1, ς(S)\varsigma(S)ς(S) is positive and finite, so σk\sigma_kσk​ is a probability measure concentrated on SSS.
  • For k=1k=1k=1, S={−1,1}S=\{-1,1\}S={−1,1} and σ1\sigma_1σ1​ puts mass 12\tfrac1221​ on each point.
  • For k=0k=0k=0, SSS is empty and ς\varsigmaς is the zero measure. The convention ∞⋅0=0\infty\cdot 0=0∞⋅0=0 then makes σ0\sigma_0σ0​ the zero measure, which is not a probability measure.

Energy energy. For any real-valued function KKK on Rk×Rk\mathbb{R}^k\times\mathbb{R}^kRk×Rk and any measures μ,ν\mu,\nuμ,ν on Rk\mathbb{R}^kRk,

EK(μ,ν)=∫ ⁣(∫K(u,v) dν(v)) dμ(u).E_K(\mu,\nu)=\int\!\Big(\int K(u,v)\,d\nu(v)\Big)\,d\mu(u).EK​(μ,ν)=∫(∫K(u,v)dν(v))dμ(u).

This is an iterated integral: the inner integral over vvv is taken first, then the outer over uuu. Nothing further is assumed:

  • KKK need not be measurable, symmetric or bounded;
  • μ,ν\mu,\nuμ,ν need not be finite or probability measures.

Integrals follow the convention that a non-integrable function has integral 000. This applies to the inner integral at each fixed uuu, and to the outer integral.


Signed energy signedEnergy. For KKK as above and arbitrary measures ν1,ν2\nu_1,\nu_2ν1​,ν2​,

IK(ν1,ν2)=EK(ν1,ν1)−EK(ν1,ν2)−EK(ν2,ν1)+EK(ν2,ν2).I_K(\nu_1,\nu_2)=E_K(\nu_1,\nu_1)-E_K(\nu_1,\nu_2)-E_K(\nu_2,\nu_1)+E_K(\nu_2,\nu_2).IK​(ν1​,ν2​)=EK​(ν1​,ν1​)−EK​(ν1​,ν2​)−EK​(ν2​,ν1​)+EK​(ν2​,ν2​).

This is a combination of four separately computed energies, each subject to the convention that non-integrable integrals equal 000. It is not defined as an integral against the signed measure ν1−ν2\nu_1-\nu_2ν1​−ν2​.


Pairwise potential pairPotential. For a real ttt and a finite family of points u1,…,uN∈Rku_1,\dots,u_N\in\mathbb{R}^ku1​,…,uN​∈Rk,

Pt(u)=∑1≤i<j≤NGt(ui,uj).P_t(u)=\sum_{1\le i<j\le N}G_t(u_i,u_j).Pt​(u)=1≤i<j≤N∑​Gt​(ui​,uj​).
  • The points are arbitrary: they need not lie on the sphere and may repeat.
  • For N=0N=0N=0 or N=1N=1N=1 the sum is empty and equals 000.

Normalised counting measure normCounting. For points u1,…,uN∈Rku_1,\dots,u_N\in\mathbb{R}^ku1​,…,uN​∈Rk,

μu=1N∑i=1Nδui,\mu_u=\frac1N\sum_{i=1}^N\delta_{u_i},μu​=N1​i=1∑N​δui​​,

where δp\delta_pδp​ is the Dirac mass at ppp. Repeated points accumulate mass. For N=0N=0N=0 the sum is the zero measure, and since ∞⋅0=0\infty\cdot0=0∞⋅0=0, μu\mu_uμu​ is the zero measure.


Weak convergence WeakStarTendsto.* A sequence of measures (μn)n∈N(\mu_n)_{n\in\mathbb{N}}(μn​)n∈N​ on Rk\mathbb{R}^kRk converges to a measure μ\muμ when, for every continuous function f:Rk→Rf:\mathbb{R}^k\to\mathbb{R}f:Rk→R,

∫f dμn  →n→∞  ∫f dμ.\int f\,d\mu_n\;\xrightarrow[n\to\infty]{}\;\int f\,d\mu.∫fdμn​n→∞​∫fdμ.
  • The test functions fff are not required to be bounded or compactly supported.
  • The measures are not required to be finite or probability measures.
  • Each integral of a non-integrable fff is taken to be 000.

Hypergeometric function hyp0F1. For real α\alphaα and zzz,

0F1(;α;z)=∑n=0∞zn(α)n n!,{}_0F_1(;\alpha;z)=\sum_{n=0}^{\infty}\frac{z^n}{(\alpha)_n\,n!},0​F1​(;α;z)=n=0∑∞​(α)n​n!zn​,

where (α)n=α(α+1)⋯(α+n−1)(\alpha)_n=\alpha(\alpha+1)\cdots(\alpha+n-1)(α)n​=α(α+1)⋯(α+n−1) is the rising factorial.

  • The n=0n=0n=0 term is 111, since (α)0=1(\alpha)_0=1(α)0​=1 and z0=1z^0=1z0=1, including when z=0z=0z=0.
  • Division by zero yields 000. So if α\alphaα is a non-positive integer, every term with (α)n=0(\alpha)_n=0(α)n​=0, i.e. n>−αn>-\alphan>−α, is 000 rather than undefined.
  • The sum is an unconditional sum of reals; if the family were not summable, the value would be 000.

Exponential potential expPotential. For a real τ\tauτ, a measure μ\muμ on Rk\mathbb{R}^kRk and u∈Rku\in\mathbb{R}^ku∈Rk,

Uμ(u)=∫exp⁡ ⁣(⟨u,v⟩/τ) dμ(v).U_\mu(u)=\int\exp\!\big(\langle u,v\rangle/\tau\big)\,d\mu(v).Uμ​(u)=∫exp(⟨u,v⟩/τ)dμ(v).
  • There is no sign condition on τ\tauτ.
  • For τ=0\tau=0τ=0 the quotient is 000 by the division convention. The integrand is then 111, and Uμ(u)=μ(Rk)U_\mu(u)=\mu(\mathbb{R}^k)Uμ​(u)=μ(Rk) if that is finite, and 000 otherwise.
  • In general, a non-integrable integrand gives the value 000.
  • Neither μ\muμ nor uuu is restricted to the sphere.

Relaxed objective relaxedObjective. For real τ\tauτ and a measure μ\muμ on Rk\mathbb{R}^kRk,

Rτ(μ)=∫log⁡Uμ(u) dμ(u).R_\tau(\mu)=\int\log U_\mu(u)\,d\mu(u).Rτ​(μ)=∫logUμ​(u)dμ(u).
  • Here log⁡0=0\log 0=0log0=0; in particular this applies at every point where UμU_\muUμ​ takes its junk value 000.
  • A non-integrable integrand gives the value 000.

Positive-pair law IsPosPairLaw. For a measure pdatap_{\mathrm{data}}pdata​ on Rn\mathbb{R}^nRn and a measure pposp_{\mathrm{pos}}ppos​ on Rn×Rn\mathbb{R}^n\times\mathbb{R}^nRn×Rn, the property holds exactly when both of the following are true:

  1. pposp_{\mathrm{pos}}ppos​ is invariant under the swap (x,y)↦(y,x)(x,y)\mapsto(y,x)(x,y)↦(y,x);
  2. the first marginal of pposp_{\mathrm{pos}}ppos​, i.e. its image under (x,y)↦x(x,y)\mapsto x(x,y)↦x, equals pdatap_{\mathrm{data}}pdata​.

Neither measure is required to be a probability measure or to be finite. The two measures must, however, have the same total mass.


Encoder IsEncoder. A map f:Rn→Rmf:\mathbb{R}^n\to\mathbb{R}^mf:Rn→Rm is an encoder when both of the following are true:

  • fff is Borel measurable;
  • ∥f(x)∥=1\|f(x)\|=1∥f(x)∥=1 for every x∈Rnx\in\mathbb{R}^nx∈Rn.

When m=0m=0m=0 no such map exists.


Contrastive loss contrastiveLoss. The inputs are:

  • measures pdatap_{\mathrm{data}}pdata​ on Rn\mathbb{R}^nRn and pposp_{\mathrm{pos}}ppos​ on Rn×Rn\mathbb{R}^n\times\mathbb{R}^nRn×Rn;
  • any function f:Rn→Rmf:\mathbb{R}^n\to\mathbb{R}^mf:Rn→Rm, not required to be an encoder or measurable;
  • a real τ\tauτ;
  • a natural number MMM.

The loss is

L(f;τ,M)=∫−log⁡e⟨f(x),f(y)⟩/τe⟨f(x),f(y)⟩/τ+∑i=1Me⟨f(xi−),f(y)⟩/τ  d(ppos⊗pdata⊗M)((x,y),(x1−,…,xM−)).\mathcal{L}(f;\tau,M)=\int -\log\frac{e^{\langle f(x),f(y)\rangle/\tau}}{e^{\langle f(x),f(y)\rangle/\tau}+\sum_{i=1}^{M}e^{\langle f(x^-_i),f(y)\rangle/\tau}}\;d\big(p_{\mathrm{pos}}\otimes p_{\mathrm{data}}^{\otimes M}\big)\big((x,y),(x^-_1,\dots,x^-_M)\big).L(f;τ,M)=∫−loge⟨f(x),f(y)⟩/τ+∑i=1M​e⟨f(xi−​),f(y)⟩/τe⟨f(x),f(y)⟩/τ​d(ppos​⊗pdata⊗M​)((x,y),(x1−​,…,xM−​)).

The integral is over the pair (x,y)(x,y)(x,y) drawn from pposp_{\mathrm{pos}}ppos​ together with an independent MMM-tuple of negatives drawn from the MMM-fold product of pdatap_{\mathrm{data}}pdata​. Each negative term pairs f(xi−)f(x^-_i)f(xi−​) with f(y)f(y)f(y).

Edge cases:

  • For M=0M=0M=0 the sum is empty, the ratio is 111 and the integrand is 000, so the loss is 000.
  • For τ=0\tau=0τ=0 every exponent is 000 by the division convention. The integrand is then the constant log⁡(1+M)\log(1+M)log(1+M).
  • No sign condition is placed on τ\tauτ.
  • A non-integrable integrand gives the value 000.

Alignment term alignTerm. For a measure pposp_{\mathrm{pos}}ppos​, any f:Rn→Rmf:\mathbb{R}^n\to\mathbb{R}^mf:Rn→Rm and real τ\tauτ,

A(f;τ)=−1τ∫⟨f(x),f(y)⟩ dppos(x,y).A(f;\tau)=-\frac1\tau\int\langle f(x),f(y)\rangle\,dp_{\mathrm{pos}}(x,y).A(f;τ)=−τ1​∫⟨f(x),f(y)⟩dppos​(x,y).
  • For τ=0\tau=0τ=0 the prefactor is 000, so the term is 000.
  • A non-integrable integrand gives integral 000.

Uniformity term uniformTerm. For a measure pdatap_{\mathrm{data}}pdata​, any fff and real τ\tauτ,

U(f;τ)=∫log⁡(∫e⟨f(x′),f(x)⟩/τ dpdata(x′)) dpdata(x).U(f;\tau)=\int\log\Big(\int e^{\langle f(x'),f(x)\rangle/\tau}\,dp_{\mathrm{data}}(x')\Big)\,dp_{\mathrm{data}}(x).U(f;τ)=∫log(∫e⟨f(x′),f(x)⟩/τdpdata​(x′))dpdata​(x).

The usual conventions apply:

  • log⁡0=0\log 0=0log0=0;
  • division by 000 gives 000;
  • non-integrable integrals are 000.

Alignment loss alignLoss. For a measure pposp_{\mathrm{pos}}ppos​, any fff and any real α\alphaα,

Lalign(f;α)=∫∥f(x)−f(y)∥α dppos(x,y),\mathcal{L}_{\mathrm{align}}(f;\alpha)=\int\|f(x)-f(y)\|^{\alpha}\,dp_{\mathrm{pos}}(x,y),Lalign​(f;α)=∫∥f(x)−f(y)∥αdppos​(x,y),

where the power is the real power.

  • α\alphaα is not required to be positive.
  • At points where f(x)=f(y)f(x)=f(y)f(x)=f(y), the convention gives 0α=00^{\alpha}=00α=0 for α≠0\alpha\neq0α=0 and 00=10^0=100=1.
  • A non-integrable integrand gives the value 000.

Uniformity loss uniformLoss. For a measure pdatap_{\mathrm{data}}pdata​, any fff and any real ttt,

Luniform(f;t)=log⁡∫ ⁣ ⁣∫Gt(f(x),f(y)) dpdata(y) dpdata(x).\mathcal{L}_{\mathrm{uniform}}(f;t)=\log\int\!\!\int G_t\big(f(x),f(y)\big)\,dp_{\mathrm{data}}(y)\,dp_{\mathrm{data}}(x).Luniform​(f;t)=log∫∫Gt​(f(x),f(y))dpdata​(y)dpdata​(x).
  • ttt is not required to be positive.
  • log⁡0=0\log 0=0log0=0.
  • Non-integrable integrals are 000.

Perfect alignment PerfectlyAligned. The property holds exactly when

f(x)=f(y)for ppos-almost every (x,y).f(x)=f(y)\quad\text{for } p_{\mathrm{pos}}\text{-almost every } (x,y).f(x)=f(y)for ppos​-almost every (x,y).

It holds trivially if ppos=0p_{\mathrm{pos}}=0ppos​=0. The map fff is not required to be an encoder.


Perfect uniformity PerfectlyUniform. The property holds exactly when the image of pdatap_{\mathrm{data}}pdata​ under fff equals σm\sigma_mσm​:

f#pdata=σm,i.e.pdata(f−1(B))=σm(B) for every Borel B⊆Rm,f_\#p_{\mathrm{data}}=\sigma_m,\qquad \text{i.e.}\quad p_{\mathrm{data}}(f^{-1}(B))=\sigma_m(B)\ \text{for every Borel } B\subseteq\mathbb{R}^m,f#​pdata​=σm​,i.e.pdata​(f−1(B))=σm​(B) for every Borel B⊆Rm,

where σm\sigma_mσm​ is the uniform measure on the sphere defined above.

  • The image measure is defined to be the zero measure when fff is not (almost-everywhere) measurable.
  • For m=0m=0m=0, σ0\sigma_0σ0​ is the zero measure. The condition then holds exactly when f#pdataf_\#p_{\mathrm{data}}f#​pdata​ is the zero measure. For measurable fff, this means pdata=0p_{\mathrm{data}}=0pdata​=0.
  • pdatap_{\mathrm{data}}pdata​ is not separately required to be a probability measure.
Human review
  • Endorsed by Shuze Chen · Nov 23, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Xeroth · Nov 23, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me