Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Latent classes, contrastive and supervised losses, and collision quantities

Definition
ContrastiveUnsupervised_Basic

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

contrastive-learninggeneralization-boundslatent-classesrademacher-complexityrepresentation-learning

Objects of Section 2, Sections 4-6 and Appendices A-B of Arora, Khandeparkar, Khodak, Plevrakis and Saunshi (2019).

  1. Latent classes. A countable set C\mathcal CC of latent classes carries a probability distribution ρ\rhoρ, and each class ccc carries a probability distribution Dc\mathcal D_cDc​ on the data space X\mathcal XX. A representation is a measurable f:X→Rdf:\mathcal X\to\mathbb R^df:X→Rd with ∥f(⋅)∥≤R\|f(\cdot)\|\le R∥f(⋅)∥≤R.
  2. Losses. For a vector (vi)i∈s(v_i)_{i\in s}(vi​)i∈s​: the hinge loss ℓ(v)=max⁡{0,1+max⁡i(−vi)}\ell(v)=\max\{0,1+\max_i(-v_i)\}ℓ(v)=max{0,1+maxi​(−vi​)}, the logistic loss ℓ(v)=log⁡2(1+∑ie−vi)\ell(v)=\log_2(1+\sum_ie^{-v_i})ℓ(v)=log2​(1+∑i​e−vi​), and the hinge loss with margin γ\gammaγ, ℓγ(v)=max⁡{0,1+max⁡i(−vi)/γ}\ell_\gamma(v)=\max\{0,1+\max_i(-v_i)/\gamma\}ℓγ​(v)=max{0,1+maxi​(−vi​)/γ}. Both losses (hinge, including with margin, and logistic) of the empty vector are 000.
  3. Supervised tasks (eqs. 3-4, Definitions 2.1-2.2). A task T\mathcal TT is a finite set of distinct classes with a label distribution DT\mathcal D_{\mathcal T}DT​; Lsup(T,g)=∑c∈TDT(c) Ex∼Dc ℓ({g(x)c−g(x)c′}c′≠c)L_{sup}(\mathcal T,g)=\sum_{c\in\mathcal T}\mathcal D_{\mathcal T}(c)\,\mathbb E_{x\sim\mathcal D_c}\,\ell(\{g(x)_c-g(x)_{c'}\}_{c'\ne c})Lsup​(T,g)=∑c∈T​DT​(c)Ex∼Dc​​ℓ({g(x)c​−g(x)c′​}c′=c​). Lsup(T,f)=inf⁡WLsup(T,Wf)L_{sup}(\mathcal T,f)=\inf_WL_{sup}(\mathcal T,Wf)Lsup​(T,f)=infW​Lsup​(T,Wf). The mean classifier has rows μc=Ex∼Dcf(x)\mu_c=\mathbb E_{x\sim\mathcal D_c}f(x)μc​=Ex∼Dc​​f(x), giving Lsupμ(T,f)L^\mu_{sup}(\mathcal T,f)Lsupμ​(T,f). The averages Lsup(f)L_{sup}(f)Lsup​(f) and Lsupμ(f)L^\mu_{sup}(f)Lsupμ​(f) over (k+1)(k+1)(k+1)-way tasks draw c1,…,ck+1∼ρk+1c_1,\dots,c_{k+1}\sim\rho^{k+1}c1​,…,ck+1​∼ρk+1 conditioned on being distinct, with uniform DT\mathcal D_{\mathcal T}DT​.
  4. Unsupervised loss (eqs. 1, 2, 5, 6). Dsim(x,x+)=Ec∼ρDc(x)Dc(x+)\mathcal D_{sim}(x,x^+)=\mathbb E_{c\sim\rho}\mathcal D_c(x)\mathcal D_c(x^+)Dsim​(x,x+)=Ec∼ρ​Dc​(x)Dc​(x+) and Dneg=Ec∼ρDc\mathcal D_{neg}=\mathbb E_{c\sim\rho}\mathcal D_cDneg​=Ec∼ρ​Dc​. Lun(f)=E ℓ({f(x)⊤(f(x+)−f(xi−))}i=1k)L_{un}(f)=\mathbb E\,\ell(\{f(x)^\top(f(x^+)-f(x_i^-))\}_{i=1}^k)Lun​(f)=Eℓ({f(x)⊤(f(x+)−f(xi−​))}i=1k​) with (x,x+)∼Dsim(x,x^+)\sim\mathcal D_{sim}(x,x+)∼Dsim​ and xi−∼Dnegx_i^-\sim\mathcal D_{neg}xi−​∼Dneg​ i.i.d. L^un\widehat L_{un}Lun​ is the average of the same loss over MMM training tuples, and the training set is drawn from (Dsim×Dnegk)M(\mathcal D_{sim}\times\mathcal D_{neg}^k)^M(Dsim​×Dnegk​)M.
  5. Complexity. RS(F)=Eσ∼{±1}(k+2)dMsup⁡f∈F⟨σ,f∣S⟩\mathcal R_{\mathcal S}(\mathcal F)=\mathbb E_{\sigma\sim\{\pm1\}^{(k+2)dM}}\sup_{f\in\mathcal F}\langle\sigma,f_{|\mathcal S}\rangleRS​(F)=Eσ∼{±1}(k+2)dM​supf∈F​⟨σ,f∣S​⟩, where f∣Sf_{|\mathcal S}f∣S​ lists all coordinates of fff at all points of the training set. GenM=K(Rk RS(F)/M+(R2+log⁡k)log⁡(1/δ)/M)Gen_M=K\big(R\sqrt k\,\mathcal R_{\mathcal S}(\mathcal F)/M+(R^2+\log k)\sqrt{\log(1/\delta)/M}\big)GenM​=K(Rk​RS​(F)/M+(R2+logk)log(1/δ)/M​), which for k=1k=1k=1 is K(R RS(F)/M+R2log⁡(1/δ)/M)K\big(R\,\mathcal R_{\mathcal S}(\mathcal F)/M+R^2\sqrt{\log(1/\delta)/M}\big)K(RRS​(F)/M+R2log(1/δ)/M​). F\mathcal FF is pointwise separable: it has a countable subset F0\mathcal F_0F0​ such that every f∈Ff\in\mathcal Ff∈F is a pointwise limit of a sequence in F0\mathcal F_0F0​.
  6. Class collision (Section 4.2). τ=∑cρ(c)2\tau=\sum_c\rho(c)^2τ=∑c​ρ(c)2 and ν(c)=ρ(c)2/τ\nu(c)=\rho(c)^2/\tauν(c)=ρ(c)2/τ. Lun=(f)L^=_{un}(f)Lun=​(f) is the one-negative loss with x,x+,x−∼Dc3x,x^+,x^-\sim\mathcal D_c^3x,x+,x−∼Dc3​, c∼νc\sim\nuc∼ν. Σ(f,c)\Sigma(f,c)Σ(f,c) is the covariance of f(x)f(x)f(x), x∼Dcx\sim\mathcal D_cx∼Dc​, and s(f)=Ec∼ν[∥Σ(f,c)∥2 Ex∼Dc∥f(x)∥]s(f)=\mathbb E_{c\sim\nu}\big[\sqrt{\|\Sigma(f,c)\|_2}\,\mathbb E_{x\sim\mathcal D_c}\|f(x)\|\big]s(f)=Ec∼ν​[∥Σ(f,c)∥2​​Ex∼Dc​​∥f(x)∥].
  7. kkk negatives (Appendix B.1). For (c+,c1−,…,ck−)∼ρk+1(c^+,c_1^-,\dots,c_k^-)\sim\rho^{k+1}(c+,c1−​,…,ck−​)∼ρk+1: I+={i:ci−=c+}I^+=\{i:c_i^-=c^+\}I+={i:ci−​=c+} and QQQ is the set of distinct classes. τk=P[I+≠∅]\tau_k=P[I^+\ne\emptyset]τk​=P[I+=∅] and τ′=P[ci−=c+ ∀i]\tau'=P[c_i^-=c^+\ \forall i]τ′=P[ci−​=c+ ∀i]. Lun≠(f)=E[ℓ({f(x)⊤(f(x+)−f(xi−))}i∉I+)∣∣I+∣<k]L^{\ne}_{un}(f)=\mathbb E[\ell(\{f(x)^\top(f(x^+)-f(x_i^-))\}_{i\notin I^+})\mid|I^+|<k]Lun=​(f)=E[ℓ({f(x)⊤(f(x+)−f(xi−​))}i∈/I+​)∣∣I+∣<k]; for k=1k=1k=1 this is the loss conditioned on c+≠c−c^+\ne c^-c+=c−. A task T∼D\mathcal T\sim\mathcal DT∼D is QQQ conditioned on I+=∅I^+=\emptysetI+=∅. ρmin+(T)=min⁡c∈TP(c+=c∣Q=T,I+=∅)\rho^+_{min}(\mathcal T)=\min_{c\in\mathcal T}P(c^+=c\mid Q=\mathcal T,I^+=\emptyset)ρmin+​(T)=minc∈T​P(c+=c∣Q=T,I+=∅) and pmax(T)=max⁡cDT(c)p_{max}(\mathcal T)=\max_c\mathcal D_{\mathcal T}(c)pmax​(T)=maxc​DT​(c).
  8. Sub-Gaussian margin (Section 5.2). fff is σ2\sigma^2σ2-sub-Gaussian in every direction if, for every class ccc and unit uuu, Ex∼Dceλu⊤(f(x)−μc)≤eλ2σ2/2\mathbb E_{x\sim\mathcal D_c}e^{\lambda u^\top(f(x)-\mu_c)}\le e^{\lambda^2\sigma^2/2}Ex∼Dc​​eλu⊤(f(x)−μc​)≤eλ2σ2/2 for all λ∈R\lambda\in\mathbb Rλ∈R. The margin from the proof of Lemma 5.1 is γ=1+2Rσ2log⁡R+log⁡(3/ε)\gamma=1+2R\sigma\sqrt{2\log R+\log(3/\varepsilon)}γ=1+2Rσ2logR+log(3/ε)​ for R≥1R\ge1R≥1 and γ=1+2Rσlog⁡(3/ε)\gamma=1+2R\sigma\sqrt{\log(3/\varepsilon)}γ=1+2Rσlog(3/ε)​ for R<1R<1R<1.
  9. Blocks (eq. 12). Lunblock(f)=E ℓ(f(x)⊤(1b∑if(xi+)−1b∑if(xi−)))L^{block}_{un}(f)=\mathbb E\,\ell\big(f(x)^\top(\frac1b\sum_if(x_i^+)-\frac1b\sum_if(x_i^-))\big)Lunblock​(f)=Eℓ(f(x)⊤(b1​∑i​f(xi+​)−b1​∑i​f(xi−​))), where x,x1+,…,xb+∼Dc+x,x_1^+,\dots,x_b^+\sim\mathcal D_{c^+}x,x1+​,…,xb+​∼Dc+​, x1−,…,xb−∼Dc−x_1^-,\dots,x_b^-\sim\mathcal D_{c^-}x1−​,…,xb−​∼Dc−​ and c+,c−∼ρ2c^+,c^-\sim\rho^2c+,c−∼ρ2.

Formalization Note The paper's formulas τ=∑cρ(c)2\tau=\sum_c\rho(c)^2τ=∑c​ρ(c)2 and ν(c)∝ρ(c)2\nu(c)\propto\rho(c)^2ν(c)∝ρ(c)2 presuppose a discrete ρ\rhoρ, so the class set is a countable type and expectations over classes are sums. Conditional expectations are written as ratios of sums. The spectral norm is the operator norm on Euclidean space. Sub-Gaussianity uses Mathlib's HasSubgaussianMGF with parameter σ2\sigma^2σ2. Pointwise separability of F\mathcal FF is the standard measurability convention for suprema over a function class. The paper does not state it.

Definition code
import Mathlib

/-!
Arora, Khandeparkar, Khodak, Plevrakis and Saunshi, *A Theoretical Analysis of Contrastive
Unsupervised Representation Learning*, arXiv:1902.09229v1 (2019): latent classes, similar pairs
and negative samples (Section 2, eqs. (1)-(3)), supervised and unsupervised losses
(Definitions 2.1-2.3, eqs. (4)-(6)), the Rademacher average, collision probability, class
collision losses and intraclass deviation of Section 4 (eqs. (8)-(9)), the sub-Gaussian margin quantities of
Section 5.2, the block loss of Section 6.3 (eq. (12)) and the k-negative-sample quantities of
Appendix B.1.
-/

open MeasureTheory Filter Topology

noncomputable section

namespace ContrastiveUnsupervised

/-- Section 2: representations take values in `ℝ^d` with the Euclidean norm and inner product. -/
abbrev Rep (d : ℕ) := EuclideanSpace ℝ (Fin d)

/-- Section 2 ("Evaluation Metric for Representations"): the hinge loss
`ℓ(v) = max{0, 1 + max_i (-v_i)}` of the vector `(v_i)_{i ∈ s}`; it is `0` for an empty index set
(the convention used in the proof of Theorem B.1). -/
def hingeLoss {ι : Type*} (s : Finset ι) (v : ι → ℝ) : ℝ :=
  if h : s.Nonempty then max 0 (1 + s.sup' h (fun i => -v i)) else 0

/-- Section 2 ("Evaluation Metric for Representations"): the logistic loss
`ℓ(v) = log₂(1 + ∑_i exp(-v_i))` of the vector `(v_i)_{i ∈ s}`. -/
def logisticLoss {ι : Type*} (s : Finset ι) (v : ι → ℝ) : ℝ :=
  Real.logb 2 (1 + ∑ i ∈ s, Real.exp (-v i))

/-- Section 2: the two losses considered by the paper; `hinge = true` selects the hinge loss and
`hinge = false` the logistic loss. -/
def stdLoss (hinge : Bool) {ι : Type*} (s : Finset ι) (v : ι → ℝ) : ℝ :=
  if hinge then hingeLoss s v else logisticLoss s v

/-- Section 5.2 and Appendix B.2: the hinge loss with margin `γ`,
`ℓ_γ(v) = max{0, 1 + max_i (-v_i) / γ}` (for one coordinate, `ℓ_γ(x) = (1 - x/γ)_+`). -/
def marginHingeLoss (γ : ℝ) {ι : Type*} (s : Finset ι) (v : ι → ℝ) : ℝ :=
  if h : s.Nonempty then max 0 (1 + s.sup' h (fun i => -v i) / γ) else 0

variable {X : Type*} [MeasurableSpace X] {C : Type*} {d : ℕ}

/-- Section 2 ("Latent Classes"): every latent class `c` carries a probability distribution
`D_c` on the data space `X`, and `ρ` is a probability distribution on the classes. -/
def IsLatentClassModel (D : C → Measure X) (ρ : C → ℝ) : Prop :=
  (∀ c, IsProbabilityMeasure (D c)) ∧ (∀ c, 0 ≤ ρ c) ∧ HasSum ρ 1

/-- Section 2: a representation function `f : X → ℝ^d` with `‖f(·)‖ ≤ R` (measurable, the
standing convention for taking expectations). -/
def IsBoundedRep (f : X → Rep d) (R : ℝ) : Prop :=
  Measurable f ∧ ∀ x, ‖f x‖ ≤ R

/-- Definition 2.1: the mean `μ_c = E_{x ∼ D_c}[f(x)]` of the representations of class `c`. -/
def classMean (D : C → Measure X) (f : X → Rep d) (c : C) : Rep d :=
  ∫ x, f x ∂(D c)

open Classical in
/-- Eq. (3) and Section 2: the supervised loss `L_sup(T, g) = E_{(x,c) ∼ D_T} ℓ({g(x)_c - g(x)_{c'}}_{c' ≠ c})`
of a classifier `g` (coordinates indexed by classes) on the task `T`, where
`D_T(x, c) = D_c(x) p(c)` and `p` is the label distribution of the task. -/
def supLoss (ℓ : Finset C → (C → ℝ) → ℝ) (D : C → Measure X) (T : Finset C) (p : C → ℝ)
    (g : X → C → ℝ) : ℝ :=
  ∑ c ∈ T, p c * ∫ x, ℓ (T.erase c) (fun c' => g x c - g x c') ∂(D c)

/-- Eq. (4): the supervised loss of a representation `f` on the task `T`, with the best linear
classifier `g(x) = W f(x)` (row `W c` for class `c`): `L_sup(T, f) = inf_W L_sup(T, W f)`. -/
def repSupLoss (ℓ : Finset C → (C → ℝ) → ℝ) (D : C → Measure X) (T : Finset C) (p : C → ℝ)
    (f : X → Rep d) : ℝ :=
  ⨅ W : C → Rep d, supLoss ℓ D T p (fun x c => inner ℝ (W c) (f x))

/-- Definition 2.1: the loss `L^μ_sup(T, f)` of the mean classifier `W^μ`, whose row for class `c`
is the class mean `μ_c`. -/
def meanSupLoss (ℓ : Finset C → (C → ℝ) → ℝ) (D : C → Measure X) (T : Finset C) (p : C → ℝ)
    (f : X → Rep d) : ℝ :=
  supLoss ℓ D T p (fun x c => inner ℝ (classMean D f c) (f x))

/-- Section 2: the uniform label distribution `D_T(c) = 1/|T|` on a task `T`. -/
def uniformOn (T : Finset C) : C → ℝ := fun _ => 1 / (T.card : ℝ)

/-- The probability `∏_i ρ(c_i)` of a tuple of classes drawn i.i.d. from `ρ`. -/
def tupleWeight (ρ : C → ℝ) {n : ℕ} (c : Fin n → C) : ℝ := ∏ i, ρ (c i)

open Classical in
/-- Definition 2.2: the average supervised loss on `(k+1)`-way tasks,
`L_sup(f) = E_{(c_i) ∼ ρ^{k+1}}[L_sup({c_i}, f) | c_i ≠ c_j]`, with uniform label distributions. -/
def avgSupLoss (ℓ : Finset C → (C → ℝ) → ℝ) (D : C → Measure X) (ρ : C → ℝ) (k : ℕ)
    (f : X → Rep d) : ℝ :=
  (∑' c : Fin (k + 1) → C, if Function.Injective c then
      tupleWeight ρ c * repSupLoss ℓ D (Finset.univ.image c) (uniformOn (Finset.univ.image c)) f
    else 0) /
  (∑' c : Fin (k + 1) → C, if Function.Injective c then tupleWeight ρ c else 0)

open Classical in
/-- Definition 2.2: the average supervised loss of the mean classifier on `(k+1)`-way tasks,
`L^μ_sup(f) = E_{(c_i) ∼ ρ^{k+1}}[L^μ_sup({c_i}, f) | c_i ≠ c_j]`, with uniform label
distributions. -/
def avgMeanSupLoss (ℓ : Finset C → (C → ℝ) → ℝ) (D : C → Measure X) (ρ : C → ℝ) (k : ℕ)
    (f : X → Rep d) : ℝ :=
  (∑' c : Fin (k + 1) → C, if Function.Injective c then
      tupleWeight ρ c * meanSupLoss ℓ D (Finset.univ.image c) (uniformOn (Finset.univ.image c)) f
    else 0) /
  (∑' c : Fin (k + 1) → C, if Function.Injective c then tupleWeight ρ c else 0)

/-- Eq. (1): the distribution of similar pairs, `D_sim(x, x⁺) = E_{c ∼ ρ} D_c(x) D_c(x⁺)`. -/
def simMeasure (D : C → Measure X) (ρ : C → ℝ) : Measure (X × X) :=
  Measure.sum (fun c => ENNReal.ofReal (ρ c) • (D c).prod (D c))

/-- Eq. (2): the distribution of negative samples, `D_neg(x⁻) = E_{c ∼ ρ} D_c(x⁻)`. -/
def negMeasure (D : C → Measure X) (ρ : C → ℝ) : Measure X :=
  Measure.sum (fun c => ENNReal.ofReal (ρ c) • D c)

/-- Definition 2.3: the law `D_sim × D_neg^k` of one training tuple `(x, x⁺, x⁻_1, …, x⁻_k)`
(regrouped from `((x, x⁺), x⁻)` to `(x, (x⁺, x⁻))`). -/
def tupleMeasure (D : C → Measure X) (ρ : C → ℝ) (k : ℕ) : Measure (X × X × (Fin k → X)) :=
  ((simMeasure D ρ).prod (Measure.pi fun _ : Fin k => negMeasure D ρ)).map
    MeasurableEquiv.prodAssoc

/-- Definition 2.3: the contrast scores `f(x)ᵀ(f(x⁺) - f(x⁻_i))`, `i = 1, …, k`, of a tuple. -/
def contrastScores (f : X → Rep d) {k : ℕ} (z : X × X × (Fin k → X)) : Fin k → ℝ :=
  fun i => inner ℝ (f z.1) (f z.2.1 - f (z.2.2 i))

/-- Definition 2.3, eq. (5): the unsupervised (contrastive) loss with `k` negative samples,
`L_un(f) = E[ℓ({f(x)ᵀ(f(x⁺) - f(x⁻_i))}_{i=1}^k)]`, `(x, x⁺) ∼ D_sim`, `x⁻_i ∼ D_neg` i.i.d. -/
def unsupLoss (hinge : Bool) (D : C → Measure X) (ρ : C → ℝ) (k : ℕ) (f : X → Rep d) : ℝ :=
  ∫ z, stdLoss hinge Finset.univ (contrastScores f z) ∂(tupleMeasure D ρ k)

/-- Definition 2.3, eq. (6): the empirical unsupervised loss on `M` training tuples,
`L̂_un(f) = (1/M) ∑_j ℓ({f(x_j)ᵀ(f(x⁺_j) - f(x⁻_{ji}))}_{i=1}^k)`. -/
def empUnsupLoss (hinge : Bool) (k : ℕ) (f : X → Rep d) {M : ℕ}
    (S : Fin M → X × X × (Fin k → X)) : ℝ :=
  (1 / (M : ℝ)) * ∑ j, stdLoss hinge Finset.univ (contrastScores f (S j))

/-- Section 4 and Appendix A.3: the law `(D_sim × D_neg^k)^M` of the training set
`S = {(x_j, x⁺_j, x⁻_{j1}, …, x⁻_{jk})}_{j=1}^M`. -/
def sampleMeasure (D : C → Measure X) (ρ : C → ℝ) (k M : ℕ) :
    Measure (Fin M → X × X × (Fin k → X)) :=
  Measure.pi fun _ : Fin M => tupleMeasure D ρ k

/-- The points `(x, x⁺, x⁻_1, …, x⁻_k)` of a training tuple, listed as positions `0, …, k+1`. -/
def tuplePoints {k : ℕ} (z : X × X × (Fin k → X)) : Fin (k + 2) → X :=
  Fin.cons (α := fun _ => X) z.1 (Fin.cons (α := fun _ => X) z.2.1 z.2.2)

/-- Section 4.1 and Appendix A.3: the Rademacher average
`R_S(F) = E_{σ ∼ {±1}^{(k+2)dM}}[sup_{f ∈ F} ⟨σ, f_{|S}⟩]`, where `f_{|S}` lists the coordinates
`f_t` of `f` at every point of every training tuple (`σ = true` stands for `+1`). -/
def rademacherAvg (F : Set (X → Rep d)) {k M : ℕ} (S : Fin M → X × X × (Fin k → X)) : ℝ :=
  (1 / (2 : ℝ) ^ (M * (k + 2) * d)) *
    ∑ σ : Fin M → Fin (k + 2) → Fin d → Bool,
      ⨆ f : F, ∑ j, ∑ p, ∑ t, (if σ j p t then (1 : ℝ) else -1) * (f.1 (tuplePoints (S j) p)) t

/-- Section 4.1 and Appendix A.3: the generalization error term
`Gen_M = K (R √k R_S(F)/M + (R² + log k) √(log(1/δ)/M))`, with the constant `K` of the `O(·)`
made explicit; for `k = 1` this is `K (R R_S(F)/M + R² √(log(1/δ)/M))`. -/
def genError (K R : ℝ) (k M : ℕ) (rad δ : ℝ) : ℝ :=
  K * (R * Real.sqrt k * rad / M + (R ^ 2 + Real.log k) * Real.sqrt (Real.log (1 / δ) / M))

/-- Standing measurability convention for the function class (pointwise separability, the usual
condition making suprema over `F` countable suprema): a countable `F₀ ⊆ F` such that every
`f ∈ F` is a pointwise limit of a sequence in `F₀`. -/
def IsPointwiseSeparable (F : Set (X → Rep d)) : Prop :=
  ∃ F₀ ⊆ F, F₀.Countable ∧ ∀ f ∈ F, ∃ u : ℕ → X → Rep d, (∀ n, u n ∈ F₀) ∧
    ∀ x, Tendsto (fun n => u n x) atTop (𝓝 (f x))

/-- Section 4.1: `τ = P_{c, c' ∼ ρ²}(c = c') = ∑_c ρ(c)²`, the probability that two independent
classes coincide. -/
def collisionProb (ρ : C → ℝ) : ℝ := ∑' c, ρ c ^ 2

/-- The law of `(x, x⁺, x⁻_1, …, x⁻_k)` given the classes: `x, x⁺ ∼ D_{c⁺}`, `x⁻_i ∼ D_{c⁻_i}`,
all independent. -/
def classTupleMeasure (D : C → Measure X) (k : ℕ) (cp : C) (cn : Fin k → C) :
    Measure (X × X × (Fin k → X)) :=
  (D cp).prod ((D cp).prod (Measure.pi fun i => D (cn i)))

/-- Section 4.2 and Appendix A.1: the unsupervised loss given the classes `c⁺, c⁻_1, …, c⁻_k`,
keeping only the negative samples with index in `I`:
`E[ℓ({f(x)ᵀ(f(x⁺) - f(x⁻_i))}_{i ∈ I})]`. -/
def condUnsupLoss (hinge : Bool) (D : C → Measure X) (k : ℕ) (f : X → Rep d) (cp : C)
    (cn : Fin k → C) (I : Finset (Fin k)) : ℝ :=
  ∫ z, stdLoss hinge I (contrastScores f z) ∂(classTupleMeasure D k cp cn)

/-- Section 4.2: `L^=_un(f) = E_{c ∼ ν, x, x⁺, x⁻ ∼ D_c³}[ℓ(f(x)ᵀ(f(x⁺) - f(x⁻)))]` with
`ν(c) ∝ ρ(c)²` (one negative sample). -/
def unsupLossEq (hinge : Bool) (D : C → Measure X) (ρ : C → ℝ) (f : X → Rep d) : ℝ :=
  (∑' c, ρ c ^ 2 * condUnsupLoss hinge D 1 f c (fun _ => c) Finset.univ) / collisionProb ρ

/-- Section 4.2: `Σ(f, c)`, the covariance matrix of `f(x)` for `x ∼ D_c`. -/
def classCov (D : C → Measure X) (f : X → Rep d) (c : C) : Matrix (Fin d) (Fin d) ℝ :=
  fun i j => ∫ x, (f x i - classMean D f c i) * (f x j - classMean D f c j) ∂(D c)

/-- Section 4.2: the spectral norm `‖A‖₂` (operator norm on Euclidean space). -/
def specNorm (A : Matrix (Fin d) (Fin d) ℝ) : ℝ :=
  ‖LinearMap.toContinuousLinearMap (Matrix.toEuclideanLin A)‖

/-- Section 4.2, eq. (9): the intraclass deviation
`s(f) = E_{c ∼ ν}[√‖Σ(f, c)‖₂ E_{x ∼ D_c}‖f(x)‖]` with `ν(c) ∝ ρ(c)²`. -/
def intraclassDeviation (D : C → Measure X) (ρ : C → ℝ) (f : X → Rep d) : ℝ :=
  (∑' c, ρ c ^ 2 * (Real.sqrt (specNorm (classCov D f c)) * ∫ x, ‖f x‖ ∂(D c))) /
    collisionProb ρ

/-- Section 5.2 (footnote 6, Appendix A.2): `f(X)`, `X ∼ D_c`, is `σ²`-sub-Gaussian in every
direction for every class `c`: for every unit `u`, `E[exp(λ uᵀ(f(X) - μ_c))] ≤ exp(λ²σ²/2)` for
all real `λ`. -/
def IsSubGaussianRep (D : C → Measure X) (f : X → Rep d) (σ : ℝ) : Prop :=
  ∀ c, ∀ u : Rep d, ‖u‖ = 1 →
    ProbabilityTheory.HasSubgaussianMGF (fun x => inner ℝ u (f x - classMean D f c))
      (Real.toNNReal (σ ^ 2)) (D c)

/-- Appendix A.2 (proof of Lemma 5.1): the margin
`γ = 1 + 2Rσ √(2 log R + log(3/ε))` for `R ≥ 1`, and `γ = 1 + 2Rσ √(log(3/ε))` for `R < 1`
(footnote 7). -/
def subGaussianMargin (R σ ε : ℝ) : ℝ :=
  if 1 ≤ R then 1 + 2 * R * σ * Real.sqrt (2 * Real.log R + Real.log (3 / ε))
  else 1 + 2 * R * σ * Real.sqrt (Real.log (3 / ε))

/-- Section 6.3: the law of a positive block `x, x⁺_1, …, x⁺_b ∼ D_{c⁺}` and a negative block
`x⁻_1, …, x⁻_b ∼ D_{c⁻}`, all independent. -/
def blockMeasure (D : C → Measure X) (b : ℕ) (cp cn : C) :
    Measure (X × (Fin b → X) × (Fin b → X)) :=
  (D cp).prod ((Measure.pi fun _ : Fin b => D cp).prod (Measure.pi fun _ : Fin b => D cn))

/-- Section 6.3, eq. (12): the block loss
`L^block_un(f) = E[ℓ(f(x)ᵀ((1/b)∑_i f(x⁺_i) - (1/b)∑_i f(x⁻_i)))]`, `c⁺, c⁻ ∼ ρ²`. -/
def blockUnsupLoss (hinge : Bool) (D : C → Measure X) (ρ : C → ℝ) (b : ℕ) (f : X → Rep d) : ℝ :=
  ∑' cp, ∑' cn, ρ cp * ρ cn *
    ∫ z, stdLoss hinge (Finset.univ : Finset (Fin 1)) (fun _ => inner ℝ (f z.1)
        ((1 / (b : ℝ)) • ∑ i, f (z.2.1 i) - (1 / (b : ℝ)) • ∑ i, f (z.2.2 i)))
      ∂(blockMeasure D b cp cn)

open Classical in
/-- Appendix B.1: `I⁺(c⁻_1, …, c⁻_k) = {i ∈ [k] : c⁻_i = c⁺}`. -/
def collisionIdx {k : ℕ} (cp : C) (cn : Fin k → C) : Finset (Fin k) :=
  Finset.univ.filter (fun i => cn i = cp)

open Classical in
/-- Appendix B.1: `Q(c⁺, c⁻_1, …, c⁻_k)`, the set of distinct classes of the tuple. -/
def tupleClasses {k : ℕ} (cp : C) (cn : Fin k → C) : Finset C :=
  insert cp (Finset.univ.image cn)

open Classical in
/-- The probability of an event about `(c⁺, c⁻_1, …, c⁻_k) ∼ ρ^{k+1}`. -/
def tupleProb (ρ : C → ℝ) (k : ℕ) (E : C → (Fin k → C) → Prop) : ℝ :=
  ∑' cp, ∑' cn : Fin k → C, if E cp cn then ρ cp * tupleWeight ρ cn else 0

/-- Appendix B.1: `τ_k = P[I⁺ ≠ ∅]` for `(c⁺, c⁻_1, …, c⁻_k) ∼ ρ^{k+1}`. -/
def tauK (ρ : C → ℝ) (k : ℕ) : ℝ :=
  tupleProb ρ k (fun cp cn => (collisionIdx cp cn).Nonempty)

/-- Appendix B.1: `τ' = P[c⁺ = c⁻_i for all i]`. -/
def tauAll (ρ : C → ℝ) (k : ℕ) : ℝ :=
  tupleProb ρ k (fun cp cn => ∀ i, cn i = cp)

open Classical in
/-- Section 4.2 (`k = 1`) and Appendix B.1: `L^≠_un(f)`, the unsupervised loss conditioned on
`|I⁺| < k` with the negative samples of index in `I⁺` removed,
`E[ℓ({f(x)ᵀ(f(x⁺) - f(x⁻_i))}_{i ∉ I⁺}) | |I⁺| < k]`. -/
def unsupLossNeq (hinge : Bool) (D : C → Measure X) (ρ : C → ℝ) (k : ℕ) (f : X → Rep d) : ℝ :=
  (∑' cp, ∑' cn : Fin k → C, if (collisionIdx cp cn).card < k then
      ρ cp * tupleWeight ρ cn *
        condUnsupLoss hinge D k f cp cn (Finset.univ.filter (fun i => cn i ≠ cp))
    else 0) /
  tupleProb ρ k (fun cp cn => (collisionIdx cp cn).card < k)

open Classical in
/-- Appendix B.1: `ρ⁺_min(T) = min_{c ∈ T} P(c⁺ = c | Q = T, I⁺ = ∅)`. -/
def rhoPlusMin (ρ : C → ℝ) (k : ℕ) (T : Finset C) : ℝ :=
  if h : T.Nonempty then
    T.inf' h (fun c =>
      tupleProb ρ k (fun cp cn => cp = c ∧ tupleClasses cp cn = T ∧ collisionIdx cp cn = ∅) /
        tupleProb ρ k (fun cp cn => tupleClasses cp cn = T ∧ collisionIdx cp cn = ∅))
  else 0

/-- Appendix B.1: `p_max(T) = max_{c ∈ T} D_T(c)`. -/
def pMax (T : Finset C) (p : C → ℝ) : ℝ :=
  if h : T.Nonempty then T.sup' h p else 0

/-- Appendix B.1: a label distribution `D_T` on every nonempty task `T`. -/
def IsTaskDistFamily (pT : Finset C → C → ℝ) : Prop :=
  ∀ T : Finset C, T.Nonempty → (∀ c, 0 ≤ pT T c) ∧ ∑ c ∈ T, pT T c = 1

open Classical in
/-- Appendix B.1: `E_{T ∼ 𝒟}[ρ⁺_min(T)/p_max(T) · L^μ_sup(T, f)]`, where `T ∼ 𝒟` is the set of
distinct classes of `(c⁺, c⁻_1, …, c⁻_k) ∼ ρ^{k+1}` conditioned on `I⁺ = ∅` (Section 6.1), and
the task `T` has label distribution `D_T = pT T`. -/
def weightedTaskMeanSupLoss (ℓ : Finset C → (C → ℝ) → ℝ) (D : C → Measure X) (ρ : C → ℝ)
    (k : ℕ) (pT : Finset C → C → ℝ) (f : X → Rep d) : ℝ :=
  (∑' cp, ∑' cn : Fin k → C, if collisionIdx cp cn = ∅ then
      ρ cp * tupleWeight ρ cn *
        (rhoPlusMin ρ k (tupleClasses cp cn) /
            pMax (tupleClasses cp cn) (pT (tupleClasses cp cn)) *
          meanSupLoss ℓ D (tupleClasses cp cn) (pT (tupleClasses cp cn)) f)
    else 0) /
  tupleProb ρ k (fun cp cn => collisionIdx cp cn = ∅)

end ContrastiveUnsupervised
Source
Arora, Khandeparkar, Khodak, Plevrakis and Saunshi, A Theoretical Analysis of Contrastive Unsupervised Representation Learning, arXiv:1902.09229v1 (2019), Section 2 (eqs. (1)-(6), Definitions 2.1-2.3), Sections 4.1-4.2 (eqs. (8)-(9)), Section 5.2, Section 6.3 (eq. (12)), Appendices A.1-A.3 and B.1
Read-back

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

Conventions used throughout. XXX is a set with a σ-algebra, and CCC is an arbitrary set of "classes". No finiteness, countability or measurability is assumed of CCC. ddd is a natural number, possibly 000.

Several conventions recur and are stated once here:

  • An integral ∫⋅ dμ\int \cdot \, d\mu∫⋅dμ of a function that is not integrable is defined to be 000.
  • A sum ∑c∈C\sum_{c\in C}∑c∈C​ over an infinite set means unconditional summation, and it is defined to be 000 if the family is not summable.
  • Real division by 000 gives 000.
  • t=0\sqrt{t}=0t​=0 for t<0t<0t<0.
  • ln⁡0=0\ln 0 = 0ln0=0, and ln⁡t=ln⁡∣t∣\ln t=\ln|t|lnt=ln∣t∣ for t<0t<0t<0.
  • A real supremum of a set that is empty or unbounded above is 000. A real infimum of a set that is unbounded below is 000.
  • Measures are not assumed finite or probability unless stated.
  • 1[⋅]\mathbb{1}[\cdot]1[⋅] denotes an indicator.

Rep d. Rd\mathbb{R}^dRd with its standard Euclidean inner product ⟨⋅,⋅⟩\langle\cdot,\cdot\rangle⟨⋅,⋅⟩ and Euclidean norm ∥⋅∥\|\cdot\|∥⋅∥. For d=0d=0d=0 this is the zero space.

hingeLoss. For a finite index set sss and a vector v=(vi)v=(v_i)v=(vi​), define

ℓhinge(s,v)={max⁡{0,  1+max⁡i∈s(−vi)}=max⁡{0, 1−min⁡i∈svi},s≠∅,0,s=∅.\ell_{\text{hinge}}(s,v)=\begin{cases}\max\bigl\{0,\;1+\max_{i\in s}(-v_i)\bigr\} = \max\{0,\,1-\min_{i\in s}v_i\}, & s\neq\emptyset,\\ 0, & s=\emptyset.\end{cases}ℓhinge​(s,v)={max{0,1+maxi∈s​(−vi​)}=max{0,1−mini∈s​vi​},0,​s=∅,s=∅.​

Only the coordinates indexed by sss matter.

logisticLoss. For a finite index set sss and a vector vvv, define

ℓlog(s,v)=log⁡2(1+∑i∈se−vi).\ell_{\text{log}}(s,v)=\log_2\Bigl(1+\sum_{i\in s}e^{-v_i}\Bigr).ℓlog​(s,v)=log2​(1+i∈s∑​e−vi​).

For s=∅s=\emptysets=∅ this equals log⁡21=0\log_2 1=0log2​1=0.

stdLoss. Given a Boolean flag, ℓ(s,v)\ell(s,v)ℓ(s,v) is ℓhinge(s,v)\ell_{\text{hinge}}(s,v)ℓhinge​(s,v) when the flag is "true" and ℓlog(s,v)\ell_{\text{log}}(s,v)ℓlog​(s,v) when it is "false". In both cases the value is 000 when s=∅s=\emptysets=∅.

marginHingeLoss. For a real number γ\gammaγ (any sign, not required positive), define

ℓγ(s,v)={max⁡{0,  1+max⁡i∈s(−vi)γ},s≠∅,0,s=∅.\ell_\gamma(s,v)=\begin{cases}\max\Bigl\{0,\;1+\dfrac{\max_{i\in s}(-v_i)}{\gamma}\Bigr\}, & s\ne\emptyset,\\ 0,& s=\emptyset.\end{cases}ℓγ​(s,v)=⎩⎨⎧​max{0,1+γmaxi∈s​(−vi​)​},0,​s=∅,s=∅.​

If γ=0\gamma=0γ=0, the quotient is taken to be 000, so the value is 111 for every nonempty sss. If γ<0\gamma<0γ<0, the direction of the effect of vvv is reversed.

IsLatentClassModel. A family of measures (Dc)c∈C(D_c)_{c\in C}(Dc​)c∈C​ on XXX and a function ρ:C→R\rho:C\to\mathbb{R}ρ:C→R form a latent class model when all three conditions hold:

  • every DcD_cDc​ is a probability measure;
  • ρ(c)≥0\rho(c)\ge 0ρ(c)≥0 for all ccc;
  • the family (ρ(c))c∈C(\rho(c))_{c\in C}(ρ(c))c∈C​ is unconditionally summable with sum exactly 111. Consequently only countably many ρ(c)\rho(c)ρ(c) are nonzero.

No condition links ρ\rhoρ and DDD.

IsBoundedRep. A map f:X→Rdf:X\to\mathbb{R}^df:X→Rd is bounded by a real number RRR when fff is measurable (for the Borel σ-algebra on Rd\mathbb{R}^dRd) and ∥f(x)∥≤R\|f(x)\|\le R∥f(x)∥≤R for every x∈Xx\in Xx∈X. If R<0R<0R<0 this is impossible unless XXX is empty.

classMean. The class mean is

μc=∫Xf(x) dDc(x)∈Rd,\mu_c=\int_X f(x)\,dD_c(x)\in\mathbb{R}^d,μc​=∫X​f(x)dDc​(x)∈Rd,

a vector-valued integral with respect to the arbitrary measure DcD_cDc​. It is 000 if fff is not DcD_cDc​-integrable.

supLoss. The inputs are:

  • a loss ℓ\ellℓ, which is an arbitrary function taking a finite set of classes and a vector indexed by CCC to a real number;
  • measures DcD_cDc​;
  • a finite set T⊆CT\subseteq CT⊆C;
  • an arbitrary weight function p:C→Rp:C\to\mathbb{R}p:C→R, not required to be a distribution;
  • a scoring function ggg assigning to each xxx and class ccc a real score g(x)cg(x)_cg(x)c​.

The supervised loss is

Lsup⁡(T,p,g)=∑c∈Tp(c)∫Xℓ(T∖{c},  (g(x)c−g(x)c′)c′) dDc(x).L_{\sup}(T,p,g)=\sum_{c\in T}p(c)\int_X \ell\bigl(T\setminus\{c\},\;(g(x)_c-g(x)_{c'})_{c'}\bigr)\,dD_c(x).Lsup​(T,p,g)=c∈T∑​p(c)∫X​ℓ(T∖{c},(g(x)c​−g(x)c′​)c′​)dDc​(x).

Each integral is 000 if its integrand is not integrable. For T=∅T=\emptysetT=∅ the value is 000.

repSupLoss. For f:X→Rdf:X\to\mathbb{R}^df:X→Rd, define

Lsup⁡(T,p,f)=inf⁡W  Lsup⁡(T,p,  gW),gW(x)c=⟨Wc,f(x)⟩.L_{\sup}(T,p,f)=\inf_{W}\;L_{\sup}\bigl(T,p,\;g_W\bigr),\qquad g_W(x)_c=\langle W_c,f(x)\rangle.Lsup​(T,p,f)=Winf​Lsup​(T,p,gW​),gW​(x)c​=⟨Wc​,f(x)⟩.

Here the infimum ranges over all assignments W:C→RdW:C\to\mathbb{R}^dW:C→Rd of a vector to every class, including classes outside TTT. If the set of these values is unbounded below, the infimum is 000.

meanSupLoss. The mean-classifier loss is Lsup⁡(T,p,g)L_{\sup}(T,p,g)Lsup​(T,p,g) with the specific scores g(x)c=⟨μc,f(x)⟩g(x)_c=\langle \mu_c,f(x)\rangleg(x)c​=⟨μc​,f(x)⟩, where μc=∫f dDc\mu_c=\int f\,dD_cμc​=∫fdDc​ is the class mean above. Class means are 000 if fff is not DcD_cDc​-integrable.

uniformOn. For a finite set TTT, this is the constant function c↦1/∣T∣c\mapsto 1/|T|c↦1/∣T∣ on all of CCC, including classes not in TTT. If T=∅T=\emptysetT=∅, it is the constant 000.

tupleWeight. For a tuple (c1,…,cn)(c_1,\dots,c_n)(c1​,…,cn​) of classes, the weight is ∏i=1nρ(ci)\prod_{i=1}^n\rho(c_i)∏i=1n​ρ(ci​). For n=0n=0n=0 the empty product is 111.

avgSupLoss. For k∈Nk\in\mathbb{N}k∈N, the sums run over all tuples c=(c0,…,ck)∈Ck+1\mathbf c=(c_0,\dots,c_k)\in C^{k+1}c=(c0​,…,ck​)∈Ck+1 with pairwise distinct entries:

Lsup⁡(f)=∑c injective(∏i=0kρ(ci)) Lsup⁡({c0,…,ck}, u, f)∑c injective∏i=0kρ(ci).L_{\sup}(f)=\frac{\displaystyle\sum_{\mathbf c\ \text{injective}}\Bigl(\prod_{i=0}^k\rho(c_i)\Bigr)\,L_{\sup}\bigl(\{c_0,\dots,c_k\},\,u,\,f\bigr)}{\displaystyle\sum_{\mathbf c\ \text{injective}}\prod_{i=0}^k\rho(c_i)}.Lsup​(f)=c injective∑​i=0∏k​ρ(ci​)c injective∑​(i=0∏k​ρ(ci​))Lsup​({c0​,…,ck​},u,f)​.

In each term:

  • Lsup⁡(T,u,f)L_{\sup}(T,u,f)Lsup​(T,u,f) is the infimum-over-WWW supervised loss above;
  • u≡1/(k+1)u\equiv 1/(k+1)u≡1/(k+1) is the uniform weight on the task T={c0,…,ck}T=\{c_0,\dots,c_k\}T={c0​,…,ck​}.

Edge cases:

  • Each sum is 000 if not summable.
  • The whole quotient is 000 when the denominator is 000, for instance when fewer than k+1k+1k+1 classes have nonzero ρ\rhoρ.
  • Ordered tuples are counted, so every task appears once per ordering.

avgMeanSupLoss. This is identical to the previous definition, except that Lsup⁡({c0,…,ck},u,f)L_{\sup}(\{c_0,\dots,c_k\},u,f)Lsup​({c0​,…,ck​},u,f) is replaced by the mean-classifier loss with scores ⟨μc,f(x)⟩\langle\mu_c,f(x)\rangle⟨μc​,f(x)⟩. It has the same distinctness condition, the same normalisation, and the same 000 conventions.

simMeasure. The similar-pair measure on X×XX\times XX×X is

Dsim=∑c∈Cρ(c)+ (Dc⊗Dc).D_{\text{sim}}=\sum_{c\in C}\rho(c)^+\,(D_c\otimes D_c).Dsim​=c∈C∑​ρ(c)+(Dc​⊗Dc​).

Here:

  • ρ(c)+=max⁡{ρ(c),0}\rho(c)^+=\max\{\rho(c),0\}ρ(c)+=max{ρ(c),0}, so negative weights are truncated to 000;
  • the sum is a sum of measures over the possibly uncountable set CCC;
  • DsimD_{\text{sim}}Dsim​ is a probability measure only if, for example, the latent-class conditions hold.

negMeasure. The negative-sample measure on XXX is

Dneg=∑c∈Cρ(c)+ Dc,D_{\text{neg}}=\sum_{c\in C}\rho(c)^+\,D_c,Dneg​=c∈C∑​ρ(c)+Dc​,

with negative ρ(c)\rho(c)ρ(c) truncated to 000.

tupleMeasure. This is the measure on tuples (x,x+,(x1−,…,xk−))(x,x^+,(x^-_1,\dots,x^-_k))(x,x+,(x1−​,…,xk−​)) obtained as Dsim⊗Dneg⊗kD_{\text{sim}}\otimes D_{\text{neg}}^{\otimes k}Dsim​⊗Dneg⊗k​, with coordinates regrouped. The pair (x,x+)(x,x^+)(x,x+) is distributed by DsimD_{\text{sim}}Dsim​, and the kkk negatives are distributed by the kkk-fold product of DnegD_{\text{neg}}Dneg​. For k=0k=0k=0 the negative factor is the unit point mass on the single empty tuple.

contrastScores. For a tuple z=(x,x+,x1−,…,xk−)z=(x,x^+,x^-_1,\dots,x^-_k)z=(x,x+,x1−​,…,xk−​), the iii-th contrast score is

si(z)=⟨f(x), f(x+)−f(xi−)⟩,i=1,…,k.s_i(z)=\bigl\langle f(x),\,f(x^+)-f(x^-_i)\bigr\rangle,\qquad i=1,\dots,k.si​(z)=⟨f(x),f(x+)−f(xi−​)⟩,i=1,…,k.

unsupLoss. The unsupervised loss is

Lun(f)=∫ℓ({1,…,k}, (si(z))i) d(Dsim⊗Dneg⊗k)(z).L_{\text{un}}(f)=\int \ell\bigl(\{1,\dots,k\},\,(s_i(z))_{i}\bigr)\,d\bigl(D_{\text{sim}}\otimes D_{\text{neg}}^{\otimes k}\bigr)(z).Lun​(f)=∫ℓ({1,…,k},(si​(z))i​)d(Dsim​⊗Dneg⊗k​)(z).

Here:

  • ℓ\ellℓ is the hinge or logistic loss according to the flag;
  • the loss is applied to all kkk scores;
  • the integral is 000 if the integrand is not integrable;
  • for k=0k=0k=0 the integrand is identically 000, so Lun=0L_{\text{un}}=0Lun​=0;
  • no normalisation is applied, so the total mass of the measure is not forced to be 111.

empUnsupLoss. For a list S=(z1,…,zM)S=(z_1,\dots,z_M)S=(z1​,…,zM​) of tuples, the empirical loss is

L^un(f)=1M∑j=1Mℓ({1,…,k},(si(zj))i),\widehat L_{\text{un}}(f)=\frac1M\sum_{j=1}^M \ell\bigl(\{1,\dots,k\},(s_i(z_j))_i\bigr),Lun​(f)=M1​j=1∑M​ℓ({1,…,k},(si​(zj​))i​),

with hinge or logistic loss according to the flag. For M=0M=0M=0 the value is 000.

sampleMeasure. The training-set measure is the MMM-fold product of the tuple measure Dsim⊗Dneg⊗kD_{\text{sim}}\otimes D_{\text{neg}}^{\otimes k}Dsim​⊗Dneg⊗k​, on lists of MMM tuples. The tuples are independent copies.

tuplePoints. A tuple (x,x+,x1−,…,xk−)(x,x^+,x^-_1,\dots,x^-_k)(x,x+,x1−​,…,xk−​) is listed as k+2k+2k+2 points indexed 0,…,k+10,\dots,k+10,…,k+1:

  • position 000 is xxx;
  • position 111 is x+x^+x+;
  • position 1+i1+i1+i is xi−x^-_ixi−​ for i=1,…,ki=1,\dots,ki=1,…,k.

rademacherAvg. For a set FFF of maps X→RdX\to\mathbb{R}^dX→Rd and S=(z1,…,zM)S=(z_1,\dots,z_M)S=(z1​,…,zM​), write yj,py_{j,p}yj,p​ for the ppp-th point of tuple zjz_jzj​. The average is

RS(F)=12M(k+2)d∑σ∈{±1}M×(k+2)×d  sup⁡f∈F  ∑j=1M∑p=0k+1∑t=1dσj,p,t f(yj,p)t.\mathcal R_S(F)=\frac{1}{2^{M(k+2)d}}\sum_{\sigma\in\{\pm1\}^{M\times(k+2)\times d}}\;\sup_{f\in F}\;\sum_{j=1}^M\sum_{p=0}^{k+1}\sum_{t=1}^d\sigma_{j,p,t}\,f(y_{j,p})_t.RS​(F)=2M(k+2)d1​σ∈{±1}M×(k+2)×d∑​f∈Fsup​j=1∑M​p=0∑k+1​t=1∑d​σj,p,t​f(yj,p​)t​.

That is, it averages, uniformly over all sign patterns, the supremum over FFF of the signed sum of all coordinates of fff at all points of all tuples. Each supremum is 000 if F=∅F=\emptysetF=∅ or if the values are unbounded above.

genError. For reals K,R,rad,δK,R,\mathrm{rad},\deltaK,R,rad,δ and naturals k,Mk,Mk,M, define

Gen=K(Rk radM+(R2+ln⁡k)ln⁡(1/δ)M),\mathrm{Gen}=K\left(\frac{R\sqrt{k}\,\mathrm{rad}}{M}+(R^2+\ln k)\sqrt{\frac{\ln(1/\delta)}{M}}\right),Gen=K(MRk​rad​+(R2+lnk)Mln(1/δ)​​),

using the natural logarithm. Edge cases:

  • M=0M=0M=0 makes both quotients 000, so Gen=0\mathrm{Gen}=0Gen=0.
  • k=0k=0k=0 gives ln⁡0=0\ln 0=0ln0=0 and 0=0\sqrt0=00​=0.
  • δ=0\delta=0δ=0 gives 1/δ=01/\delta=01/δ=0 and ln⁡0=0\ln 0=0ln0=0.
  • δ>1\delta>1δ>1 makes the square-root argument negative, so the second term is 000.
  • δ<0\delta<0δ<0 gives ln⁡(1/δ)=ln⁡∣1/δ∣\ln(1/\delta)=\ln|1/\delta|ln(1/δ)=ln∣1/δ∣.
  • No sign conditions are imposed on KKK, RRR or rad\mathrm{rad}rad.

IsPointwiseSeparable. A set FFF of maps X→RdX\to\mathbb{R}^dX→Rd is pointwise separable when there is a countable subset F0⊆FF_0\subseteq FF0​⊆F with the following property: for every f∈Ff\in Ff∈F there is a sequence u1,u2,…u_1,u_2,\dotsu1​,u2​,… in F0F_0F0​ with un(x)→f(x)u_n(x)\to f(x)un​(x)→f(x) in Rd\mathbb{R}^dRd for every x∈Xx\in Xx∈X.

collisionProb. The collision probability is τ=∑c∈Cρ(c)2\tau=\sum_{c\in C}\rho(c)^2τ=∑c∈C​ρ(c)2. It is 000 if the family is not summable.

classTupleMeasure. For a class c+c^+c+ and classes c1−,…,ck−c^-_1,\dots,c^-_kc1−​,…,ck−​, this is the product measure

Dc+⊗Dc+⊗Dc1−⊗⋯⊗Dck−D_{c^+}\otimes D_{c^+}\otimes D_{c^-_1}\otimes\cdots\otimes D_{c^-_k}Dc+​⊗Dc+​⊗Dc1−​​⊗⋯⊗Dck−​​

on tuples (x,x+,x1−,…,xk−)(x,x^+,x^-_1,\dots,x^-_k)(x,x+,x1−​,…,xk−​). Under it, xxx and x+x^+x+ are drawn independently from Dc+D_{c^+}Dc+​, and each xi−x^-_ixi−​ independently from Dci−D_{c^-_i}Dci−​​.

condUnsupLoss. For classes c+,c1−,…,ck−c^+,c^-_1,\dots,c^-_kc+,c1−​,…,ck−​ and a subset I⊆{1,…,k}I\subseteq\{1,\dots,k\}I⊆{1,…,k}, define

Lun(f∣c+,c−;I)=∫ℓ(I, (si(z))i) d(Dc+⊗Dc+⊗⨂iDci−)(z).L_{\text{un}}(f\mid c^+,c^-;I)=\int \ell\bigl(I,\,(s_i(z))_i\bigr)\,d\bigl(D_{c^+}\otimes D_{c^+}\otimes\textstyle\bigotimes_i D_{c^-_i}\bigr)(z).Lun​(f∣c+,c−;I)=∫ℓ(I,(si​(z))i​)d(Dc+​⊗Dc+​⊗⨂i​Dci−​​)(z).

All kkk negatives are sampled, but only the scores with index in III enter the hinge or logistic loss. The value is 000 if I=∅I=\emptysetI=∅, and 000 if the integrand is not integrable.

unsupLossEq. With one negative sample, define

Lun=(f)=1τ∑c∈Cρ(c)2∫ℓ(⟨f(x),f(x+)−f(x−)⟩) d(Dc⊗Dc⊗Dc),L^{=}_{\text{un}}(f)=\frac{1}{\tau}\sum_{c\in C}\rho(c)^2\int \ell\bigl(\langle f(x),f(x^+)-f(x^-)\rangle\bigr)\,d(D_c\otimes D_c\otimes D_c),Lun=​(f)=τ1​c∈C∑​ρ(c)2∫ℓ(⟨f(x),f(x+)−f(x−)⟩)d(Dc​⊗Dc​⊗Dc​),

where x,x+,x−x,x^+,x^-x,x+,x− are i.i.d. from DcD_cDc​ and τ=∑cρ(c)2\tau=\sum_c\rho(c)^2τ=∑c​ρ(c)2. The value is 000 if τ=0\tau=0τ=0 or if the sum is not summable.

classCov. The class covariance is the d×dd\times dd×d matrix with entries

Σ(f,c)ij=∫X(f(x)i−(μc)i)(f(x)j−(μc)j) dDc(x),\Sigma(f,c)_{ij}=\int_X\bigl(f(x)_i-(\mu_c)_i\bigr)\bigl(f(x)_j-(\mu_c)_j\bigr)\,dD_c(x),Σ(f,c)ij​=∫X​(f(x)i​−(μc​)i​)(f(x)j​−(μc​)j​)dDc​(x),

where μc=∫f dDc\mu_c=\int f\,dD_cμc​=∫fdDc​, which is itself 000 if fff is not integrable. Entries are 000 when not integrable. DcD_cDc​ is not required to be a probability measure.

specNorm. For a real d×dd\times dd×d matrix AAA, the spectral norm ∥A∥2\|A\|_2∥A∥2​ is its operator norm as a linear map Rd→Rd\mathbb{R}^d\to\mathbb{R}^dRd→Rd with Euclidean norms, sup⁡∥u∥≤1∥Au∥\sup_{\|u\|\le1}\|Au\|sup∥u∥≤1​∥Au∥.

intraclassDeviation. The intraclass deviation is

s(f)=1τ∑c∈Cρ(c)2 ∥Σ(f,c)∥2  ∫X∥f(x)∥ dDc(x),τ=∑cρ(c)2.s(f)=\frac{1}{\tau}\sum_{c\in C}\rho(c)^2\,\sqrt{\|\Sigma(f,c)\|_2}\;\int_X\|f(x)\|\,dD_c(x),\qquad\tau=\sum_c\rho(c)^2.s(f)=τ1​c∈C∑​ρ(c)2∥Σ(f,c)∥2​​∫X​∥f(x)∥dDc​(x),τ=c∑​ρ(c)2.

The value is 000 if τ=0\tau=0τ=0 or the sum is not summable. Each norm integral is 000 if ∥f∥\|f\|∥f∥ is not integrable.

IsSubGaussianRep. The condition holds when, for every class c∈Cc\in Cc∈C (including classes with ρ(c)=0\rho(c)=0ρ(c)=0) and every unit vector u∈Rdu\in\mathbb{R}^du∈Rd (∥u∥=1\|u\|=1∥u∥=1), the real function Y(x)=⟨u, f(x)−μc⟩Y(x)=\langle u,\,f(x)-\mu_c\rangleY(x)=⟨u,f(x)−μc​⟩ satisfies both of the following under DcD_cDc​, for all λ∈R\lambda\in\mathbb{R}λ∈R:

  • eλYe^{\lambda Y}eλY is DcD_cDc​-integrable;
  • ∫eλY dDc≤exp⁡ ⁣(λ2σ2/2)\displaystyle\int e^{\lambda Y}\,dD_c\le \exp\!\bigl(\lambda^2\sigma^2/2\bigr)∫eλYdDc​≤exp(λ2σ2/2).

Here μc=∫f dDc\mu_c=\int f\,dD_cμc​=∫fdDc​ (or 000 if not integrable). Only σ2\sigma^2σ2 matters, so the sign of σ\sigmaσ is irrelevant. For d=0d=0d=0 there are no unit vectors and the condition holds vacuously.

subGaussianMargin. For reals R,σ,εR,\sigma,\varepsilonR,σ,ε, define

γ={1+2Rσ2ln⁡R+ln⁡(3/ε),R≥1,1+2Rσln⁡(3/ε),R<1.\gamma=\begin{cases}1+2R\sigma\sqrt{2\ln R+\ln(3/\varepsilon)},& R\ge1,\\[2pt] 1+2R\sigma\sqrt{\ln(3/\varepsilon)},& R<1.\end{cases}γ={1+2Rσ2lnR+ln(3/ε)​,1+2Rσln(3/ε)​,​R≥1,R<1.​

Edge cases:

  • A negative square-root argument gives ⋅=0\sqrt{\cdot}=0⋅​=0 and hence γ=1\gamma=1γ=1.
  • ε=0\varepsilon=0ε=0 gives 3/ε=03/\varepsilon=03/ε=0 and ln⁡0=0\ln0=0ln0=0.
  • ε<0\varepsilon<0ε<0 gives ln⁡(3/ε)=ln⁡∣3/ε∣\ln(3/\varepsilon)=\ln|3/\varepsilon|ln(3/ε)=ln∣3/ε∣.
  • No sign conditions are imposed on R,σ,εR,\sigma,\varepsilonR,σ,ε.

blockMeasure. For classes c+,c−c^+,c^-c+,c− and b∈Nb\in\mathbb{N}b∈N, this is the product measure on (x,(x1+,…,xb+),(x1−,…,xb−))(x,(x^+_1,\dots,x^+_b),(x^-_1,\dots,x^-_b))(x,(x1+​,…,xb+​),(x1−​,…,xb−​)) under which:

  • xxx and each xi+x^+_ixi+​ are drawn from Dc+D_{c^+}Dc+​;
  • each xi−x^-_ixi−​ is drawn from Dc−D_{c^-}Dc−​;
  • all draws are independent.

blockUnsupLoss. The block loss is

Lunblock(f)=∑c+∈C∑c−∈Cρ(c+)ρ(c−)∫ℓ(⟨f(x),  1b∑i=1bf(xi+)−1b∑i=1bf(xi−)⟩) d(block measure for c+,c−).L^{\text{block}}_{\text{un}}(f)=\sum_{c^+\in C}\sum_{c^-\in C}\rho(c^+)\rho(c^-)\int \ell\Bigl(\Bigl\langle f(x),\;\frac1b\sum_{i=1}^b f(x^+_i)-\frac1b\sum_{i=1}^b f(x^-_i)\Bigr\rangle\Bigr)\,d(\text{block measure for }c^+,c^-).Lunblock​(f)=c+∈C∑​c−∈C∑​ρ(c+)ρ(c−)∫ℓ(⟨f(x),b1​i=1∑b​f(xi+​)−b1​i=1∑b​f(xi−​)⟩)d(block measure for c+,c−).

Here:

  • ℓ\ellℓ is the single-coordinate hinge loss max⁡{0,1−v}\max\{0,1-v\}max{0,1−v} or logistic loss log⁡2(1+e−v)\log_2(1+e^{-v})log2​(1+e−v), according to the flag;
  • c+c^+c+ and c−c^-c− range independently and may coincide;
  • the double sum is iterated: the inner sum over c−c^-c−, then the outer sum over c+c^+c+, each 000 if not summable;
  • for b=0b=0b=0, 1/b=01/b=01/b=0, so the inner product is 000 and the integrand is the constant 111 (for both losses).

collisionIdx. For c+c^+c+ and (c1−,…,ck−)(c^-_1,\dots,c^-_k)(c1−​,…,ck−​), the collision index set is

I+={ i∈{1,…,k}:ci−=c+ }.I^+=\{\,i\in\{1,\dots,k\}: c^-_i=c^+\,\}.I+={i∈{1,…,k}:ci−​=c+}.

tupleClasses. The set of distinct classes of a tuple is Q={c+}∪{c1−,…,ck−}Q=\{c^+\}\cup\{c^-_1,\dots,c^-_k\}Q={c+}∪{c1−​,…,ck−​}.

tupleProb. For a property EEE of (c+,c1−,…,ck−)(c^+,c^-_1,\dots,c^-_k)(c+,c1−​,…,ck−​), define

P(E)=∑c+∈C  ∑(ci−)∈Ck1[E(c+,c−)]  ρ(c+)∏i=1kρ(ci−).P(E)=\sum_{c^+\in C}\;\sum_{(c^-_i)\in C^k}\mathbb{1}[E(c^+,c^-)]\;\rho(c^+)\prod_{i=1}^k\rho(c^-_i).P(E)=c+∈C∑​(ci−​)∈Ck∑​1[E(c+,c−)]ρ(c+)i=1∏k​ρ(ci−​).

The sums are iterated (the inner sum over CkC^kCk), and each is 000 if not summable. No normalisation is applied.

tauK. τk=P(I+≠∅)\tau_k=P\bigl(I^+\neq\emptyset\bigr)τk​=P(I+=∅), the weighted mass, as in tupleProb, of tuples where some ci−c^-_ici−​ equals c+c^+c+. For k=0k=0k=0, τk=0\tau_k=0τk​=0.

tauAll. τ′=P(ci−=c+ for all i)\tau'=P\bigl(c^-_i=c^+\text{ for all }i\bigr)τ′=P(ci−​=c+ for all i), again weighted as in tupleProb. For k=0k=0k=0 the condition is vacuous, so τ′=∑cρ(c)\tau'=\sum_c\rho(c)τ′=∑c​ρ(c).

unsupLossNeq. Define

Lun≠(f)=∑c+∑c−∈Ck1[∣I+∣<k] ρ(c+)∏iρ(ci−)  Lun(f∣c+,c−;{i:ci−≠c+})P(∣I+∣<k).L^{\neq}_{\text{un}}(f)=\frac{\displaystyle\sum_{c^+}\sum_{c^-\in C^k}\mathbb{1}\bigl[|I^+|<k\bigr]\,\rho(c^+)\prod_i\rho(c^-_i)\;L_{\text{un}}\bigl(f\mid c^+,c^-;\{i:c^-_i\neq c^+\}\bigr)}{P\bigl(|I^+|<k\bigr)}.Lun=​(f)=P(∣I+∣<k)c+∑​c−∈Ck∑​1[∣I+∣<k]ρ(c+)i∏​ρ(ci−​)Lun​(f∣c+,c−;{i:ci−​=c+})​.

The conditional loss Lun(f∣c+,c−;I)L_{\text{un}}(f\mid c^+,c^-;I)Lun​(f∣c+,c−;I) samples x,x+∼Dc+x,x^+\sim D_{c^+}x,x+∼Dc+​ and xi−∼Dci−x^-_i\sim D_{c^-_i}xi−​∼Dci−​​, but applies the loss only to the scores of the non-colliding negatives. The condition ∣I+∣<k|I^+|<k∣I+∣<k means at least one ci−c^-_ici−​ differs from c+c^+c+. For k=0k=0k=0 the condition never holds, so the value is 0/0=00/0=00/0=0. The value is also 000 whenever the denominator is 000.

rhoPlusMin. For a finite set TTT of classes, define

ρmin⁡+(T)=min⁡c∈TP(c+=c,  Q=T,  I+=∅)P(Q=T,  I+=∅),\rho^+_{\min}(T)=\min_{c\in T}\frac{P\bigl(c^+=c,\;Q=T,\;I^+=\emptyset\bigr)}{P\bigl(Q=T,\;I^+=\emptyset\bigr)},ρmin+​(T)=c∈Tmin​P(Q=T,I+=∅)P(c+=c,Q=T,I+=∅)​,

where QQQ is the set of distinct classes of the tuple and PPP is the weighted mass of tupleProb. Every ratio is 000 if the denominator is 000. ρmin⁡+(∅)=0\rho^+_{\min}(\emptyset)=0ρmin+​(∅)=0.

pMax. For a finite set TTT and p:C→Rp:C\to\mathbb{R}p:C→R, pmax⁡(T)=max⁡c∈Tp(c)p_{\max}(T)=\max_{c\in T}p(c)pmax​(T)=maxc∈T​p(c), and pmax⁡(∅)=0p_{\max}(\emptyset)=0pmax​(∅)=0.

IsTaskDistFamily. A family pT:C→Rp_T:C\to\mathbb{R}pT​:C→R, indexed by finite sets T⊆CT\subseteq CT⊆C, satisfies this condition when, for every nonempty finite TTT:

  • pT(c)≥0p_T(c)\ge0pT​(c)≥0 for all c∈Cc\in Cc∈C, including c∉Tc\notin Tc∈/T, whose values are otherwise unconstrained;
  • ∑c∈TpT(c)=1\sum_{c\in T}p_T(c)=1∑c∈T​pT​(c)=1.

Nothing is required for T=∅T=\emptysetT=∅.

weightedTaskMeanSupLoss. With Q={c+,c1−,…,ck−}Q=\{c^+,c^-_1,\dots,c^-_k\}Q={c+,c1−​,…,ck−​} and I+={i:ci−=c+}I^+=\{i:c^-_i=c^+\}I+={i:ci−​=c+}, define

∑c+∑c−∈Ck1[I+=∅] ρ(c+)∏iρ(ci−)  ρmin⁡+(Q)pmax⁡(Q)  Lsup⁡μ(Q, pQ, f)P(I+=∅).\frac{\displaystyle\sum_{c^+}\sum_{c^-\in C^k}\mathbb{1}[I^+=\emptyset]\,\rho(c^+)\prod_i\rho(c^-_i)\;\frac{\rho^+_{\min}(Q)}{p_{\max}(Q)}\;L^{\mu}_{\sup}(Q,\,p_Q,\,f)}{P\bigl(I^+=\emptyset\bigr)}.P(I+=∅)c+∑​c−∈Ck∑​1[I+=∅]ρ(c+)i∏​ρ(ci−​)pmax​(Q)ρmin+​(Q)​Lsupμ​(Q,pQ​,f)​.

In each term:

  • pmax⁡(Q)=max⁡c∈QpQ(c)p_{\max}(Q)=\max_{c\in Q}p_Q(c)pmax​(Q)=maxc∈Q​pQ​(c);
  • Lsup⁡μ(Q,pQ,f)=∑c∈QpQ(c)∫ℓ(Q∖{c},(⟨μc,f(x)⟩−⟨μc′,f(x)⟩)c′) dDcL^{\mu}_{\sup}(Q,p_Q,f)=\sum_{c\in Q}p_Q(c)\int\ell\bigl(Q\setminus\{c\},(\langle\mu_c,f(x)\rangle-\langle\mu_{c'},f(x)\rangle)_{c'}\bigr)\,dD_cLsupμ​(Q,pQ​,f)=∑c∈Q​pQ​(c)∫ℓ(Q∖{c},(⟨μc​,f(x)⟩−⟨μc′​,f(x)⟩)c′​)dDc​ is the mean-classifier supervised loss with label weights pQp_QpQ​ and the general loss ℓ\ellℓ.

Edge cases:

  • The ratio ρmin⁡+/pmax⁡\rho^+_{\min}/p_{\max}ρmin+​/pmax​ is 000 when pmax⁡(Q)=0p_{\max}(Q)=0pmax​(Q)=0.
  • The whole expression is 000 if P(I+=∅)=0P(I^+=\emptyset)=0P(I+=∅)=0 or if a sum is not summable.
  • For k=0k=0k=0, I+=∅I^+=\emptysetI+=∅ always holds, and Q={c+}Q=\{c^+\}Q={c+}.
Human review
  • Endorsed by Shuze Chen · Nov 24, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Xeroth · Nov 24, 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