Latent classes, contrastive and supervised losses, and collision quantities
DefinitionContrastiveUnsupervised_BasicObjects of Section 2, Sections 4-6 and Appendices A-B of Arora, Khandeparkar, Khodak, Plevrakis and Saunshi (2019).
- Latent classes. A countable set of latent classes carries a probability distribution , and each class carries a probability distribution on the data space . A representation is a measurable with .
- Losses. For a vector : the hinge loss , the logistic loss , and the hinge loss with margin , . Both losses (hinge, including with margin, and logistic) of the empty vector are .
- Supervised tasks (eqs. 3-4, Definitions 2.1-2.2). A task is a finite set of distinct classes with a label distribution ; . . The mean classifier has rows , giving . The averages and over -way tasks draw conditioned on being distinct, with uniform .
- Unsupervised loss (eqs. 1, 2, 5, 6). and . with and i.i.d. is the average of the same loss over training tuples, and the training set is drawn from .
- Complexity. , where lists all coordinates of at all points of the training set. , which for is . is pointwise separable: it has a countable subset such that every is a pointwise limit of a sequence in .
- Class collision (Section 4.2). and . is the one-negative loss with , . is the covariance of , , and .
- negatives (Appendix B.1). For : and is the set of distinct classes. and . ; for this is the loss conditioned on . A task is conditioned on . and .
- Sub-Gaussian margin (Section 5.2). is -sub-Gaussian in every direction if, for every class and unit , for all . The margin from the proof of Lemma 5.1 is for and for .
- Blocks (eq. 12). , where , and .
Formalization Note The paper's formulas and presuppose a discrete , 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 . Pointwise separability of is the standard measurability convention for suprema over a function class. The paper does not state it.
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
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Conventions used throughout. is a set with a σ-algebra, and is an arbitrary set of "classes". No finiteness, countability or measurability is assumed of . is a natural number, possibly .
Several conventions recur and are stated once here:
- An integral of a function that is not integrable is defined to be .
- A sum over an infinite set means unconditional summation, and it is defined to be if the family is not summable.
- Real division by gives .
- for .
- , and for .
- A real supremum of a set that is empty or unbounded above is . A real infimum of a set that is unbounded below is .
- Measures are not assumed finite or probability unless stated.
- denotes an indicator.
Rep d. with its standard Euclidean inner product and Euclidean norm . For this is the zero space.
hingeLoss. For a finite index set and a vector , define
Only the coordinates indexed by matter.
logisticLoss. For a finite index set and a vector , define
For this equals .
stdLoss. Given a Boolean flag, is when the flag is "true" and when it is "false". In both cases the value is when .
marginHingeLoss. For a real number (any sign, not required positive), define
If , the quotient is taken to be , so the value is for every nonempty . If , the direction of the effect of is reversed.
IsLatentClassModel. A family of measures on and a function form a latent class model when all three conditions hold:
- every is a probability measure;
- for all ;
- the family is unconditionally summable with sum exactly . Consequently only countably many are nonzero.
No condition links and .
IsBoundedRep. A map is bounded by a real number when is measurable (for the Borel σ-algebra on ) and for every . If this is impossible unless is empty.
classMean. The class mean is
a vector-valued integral with respect to the arbitrary measure . It is if is not -integrable.
supLoss. The inputs are:
- a loss , which is an arbitrary function taking a finite set of classes and a vector indexed by to a real number;
- measures ;
- a finite set ;
- an arbitrary weight function , not required to be a distribution;
- a scoring function assigning to each and class a real score .
The supervised loss is
Each integral is if its integrand is not integrable. For the value is .
repSupLoss. For , define
Here the infimum ranges over all assignments of a vector to every class, including classes outside . If the set of these values is unbounded below, the infimum is .
meanSupLoss. The mean-classifier loss is with the specific scores , where is the class mean above. Class means are if is not -integrable.
uniformOn. For a finite set , this is the constant function on all of , including classes not in . If , it is the constant .
tupleWeight. For a tuple of classes, the weight is . For the empty product is .
avgSupLoss. For , the sums run over all tuples with pairwise distinct entries:
In each term:
- is the infimum-over- supervised loss above;
- is the uniform weight on the task .
Edge cases:
- Each sum is if not summable.
- The whole quotient is when the denominator is , for instance when fewer than classes have nonzero .
- Ordered tuples are counted, so every task appears once per ordering.
avgMeanSupLoss. This is identical to the previous definition, except that is replaced by the mean-classifier loss with scores . It has the same distinctness condition, the same normalisation, and the same conventions.
simMeasure. The similar-pair measure on is
Here:
- , so negative weights are truncated to ;
- the sum is a sum of measures over the possibly uncountable set ;
- is a probability measure only if, for example, the latent-class conditions hold.
negMeasure. The negative-sample measure on is
with negative truncated to .
tupleMeasure. This is the measure on tuples obtained as , with coordinates regrouped. The pair is distributed by , and the negatives are distributed by the -fold product of . For the negative factor is the unit point mass on the single empty tuple.
contrastScores. For a tuple , the -th contrast score is
unsupLoss. The unsupervised loss is
Here:
- is the hinge or logistic loss according to the flag;
- the loss is applied to all scores;
- the integral is if the integrand is not integrable;
- for the integrand is identically , so ;
- no normalisation is applied, so the total mass of the measure is not forced to be .
empUnsupLoss. For a list of tuples, the empirical loss is
with hinge or logistic loss according to the flag. For the value is .
sampleMeasure. The training-set measure is the -fold product of the tuple measure , on lists of tuples. The tuples are independent copies.
tuplePoints. A tuple is listed as points indexed :
- position is ;
- position is ;
- position is for .
rademacherAvg. For a set of maps and , write for the -th point of tuple . The average is
That is, it averages, uniformly over all sign patterns, the supremum over of the signed sum of all coordinates of at all points of all tuples. Each supremum is if or if the values are unbounded above.
genError. For reals and naturals , define
using the natural logarithm. Edge cases:
- makes both quotients , so .
- gives and .
- gives and .
- makes the square-root argument negative, so the second term is .
- gives .
- No sign conditions are imposed on , or .
IsPointwiseSeparable. A set of maps is pointwise separable when there is a countable subset with the following property: for every there is a sequence in with in for every .
collisionProb. The collision probability is . It is if the family is not summable.
classTupleMeasure. For a class and classes , this is the product measure
on tuples . Under it, and are drawn independently from , and each independently from .
condUnsupLoss. For classes and a subset , define
All negatives are sampled, but only the scores with index in enter the hinge or logistic loss. The value is if , and if the integrand is not integrable.
unsupLossEq. With one negative sample, define
where are i.i.d. from and . The value is if or if the sum is not summable.
classCov. The class covariance is the matrix with entries
where , which is itself if is not integrable. Entries are when not integrable. is not required to be a probability measure.
specNorm. For a real matrix , the spectral norm is its operator norm as a linear map with Euclidean norms, .
intraclassDeviation. The intraclass deviation is
The value is if or the sum is not summable. Each norm integral is if is not integrable.
IsSubGaussianRep. The condition holds when, for every class (including classes with ) and every unit vector (), the real function satisfies both of the following under , for all :
- is -integrable;
- .
Here (or if not integrable). Only matters, so the sign of is irrelevant. For there are no unit vectors and the condition holds vacuously.
subGaussianMargin. For reals , define
Edge cases:
- A negative square-root argument gives and hence .
- gives and .
- gives .
- No sign conditions are imposed on .
blockMeasure. For classes and , this is the product measure on under which:
- and each are drawn from ;
- each is drawn from ;
- all draws are independent.
blockUnsupLoss. The block loss is
Here:
- is the single-coordinate hinge loss or logistic loss , according to the flag;
- and range independently and may coincide;
- the double sum is iterated: the inner sum over , then the outer sum over , each if not summable;
- for , , so the inner product is and the integrand is the constant (for both losses).
collisionIdx. For and , the collision index set is
tupleClasses. The set of distinct classes of a tuple is .
tupleProb. For a property of , define
The sums are iterated (the inner sum over ), and each is if not summable. No normalisation is applied.
tauK. , the weighted mass, as in tupleProb, of tuples where some equals . For , .
tauAll. , again weighted as in tupleProb. For the condition is vacuous, so .
unsupLossNeq. Define
The conditional loss samples and , but applies the loss only to the scores of the non-colliding negatives. The condition means at least one differs from . For the condition never holds, so the value is . The value is also whenever the denominator is .
rhoPlusMin. For a finite set of classes, define
where is the set of distinct classes of the tuple and is the weighted mass of tupleProb. Every ratio is if the denominator is . .
pMax. For a finite set and , , and .
IsTaskDistFamily. A family , indexed by finite sets , satisfies this condition when, for every nonempty finite :
- for all , including , whose values are otherwise unconstrained;
- .
Nothing is required for .
weightedTaskMeanSupLoss. With and , define
In each term:
- ;
- is the mean-classifier supervised loss with label weights and the general loss .
Edge cases:
- The ratio is when .
- The whole expression is if or if a sum is not summable.
- For , always holds, and .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.