Content-style latent model, augmentation process, block-identifiability and SSL objectives
DefinitionContentStyle_BasicObjects of Sections 3 and 4 of von Kügelgen et al. (NeurIPS 2021).
- Latent space. A latent is with content and style . The representation space is , an open, simply connected subset of , . Observations live in .
- Generative process (eqs. (2)–(3), Assumptions 3.1–3.2). A model consists of a probability density on , a probability distribution on the subsets , and for each a conditional density on , which is a probability density in for every (the image of under ). The pair is drawn as , , , , , and the augmented latent stays in . With a mixing map , the views are and . denotes the law of , and the law of .
- Conditions of Theorem 4.2. (i) is smooth on , injective, and its inverse is smooth on the observation space . A function on a subset of is smooth if near each point it agrees with a function on an open set. (ii) is smooth on and almost everywhere on . (iii) Every style index lies in some with for which is smooth on , and for every , on an open set containing .
- Block-identifiability (Definition 4.1). An inferred content map on observations block-identifies the content if there is a map , injective on , with for all .
- Objectives. For a smooth bijection , (eq. (4)). For , (eq. (5)). Here is the differential entropy of a law .
Formalization Note Latents are pairs in , and observations are points of ; maps are total functions, and only their values on and enter the statements. Expectations under the generative process are nonnegative extended-real integrals, so they are always defined. The differential entropy takes values in . It is when the law has no density or is not measurable, which makes . The inverse is the inverse of on (Function.invFunOn). "Invertible " in Definition 4.1 is formalized as injectivity on the content space , which is the invertibility the paper's proofs establish (as a map from onto its image).
import Mathlib
/-!
von Kügelgen, Sharma, Gresele, Brendel, Schölkopf, Besserve and Locatello,
*Self-Supervised Learning with Data Augmentations Provably Isolates Content from Style*,
NeurIPS 2021, arXiv:2106.04619v4: the latent space, the content-preserving augmentation process
(Section 3, eqs. (2)-(3), Assumptions 3.1-3.2), the regularity conditions (i)-(iii) of Theorem 4.2,
block-identifiability (Definition 4.1) and the objectives of eqs. (4)-(5) (Section 4).
-/
open MeasureTheory
open scoped ContDiff
noncomputable section
namespace ContentStyle
/-- Section 3: the latent space `ℝ^n = ℝ^{n_c} × ℝ^{n_s}`; a latent is `z = (c, s)` with content
`c = z_{1:n_c}` and style `s = z_{(n_c+1):n}`. -/
abbrev Latent (nc ns : ℕ) : Type := (Fin nc → ℝ) × (Fin ns → ℝ)
/-- Section 3: the ambient space `ℝ^d` of the observation space `𝒳 ⊆ ℝ^d`. -/
abbrev Obs (d : ℕ) : Type := Fin d → ℝ
/-- Section 3: `Z = C × S` is an open, simply connected subset of `ℝ^n`. -/
def IsLatentSpace {nc ns : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ)) : Prop :=
IsOpen (C ×ˢ S) ∧ SimplyConnectedSpace (C ×ˢ S : Set (Latent nc ns))
/-- Assumption 3.2: the coordinates `s_A` of a style vector on the subset `A` of style indices. -/
def restr {ns : ℕ} (A : Finset (Fin ns)) (s : Fin ns → ℝ) : A → ℝ := fun i => s i
/-- Assumption 3.2: the subspace `S_A` of changing style variables, the image of `S` under
`s ↦ s_A`. -/
def projA {ns : ℕ} (A : Finset (Fin ns)) (S : Set (Fin ns → ℝ)) : Set (A → ℝ) := restr A '' S
/-- Assumption 3.2: the augmented style `s̃` with `s̃_{A^c} = s_{A^c}` and `s̃_A = t`. -/
def merge {ns : ℕ} (A : Finset (Fin ns)) (s : Fin ns → ℝ) (t : A → ℝ) : Fin ns → ℝ :=
fun i => if h : i ∈ A then t ⟨i, h⟩ else s i
/-- Section 3, eqs. (2)-(3): the latent part of a generative model of pairs: the density `p_z`,
the distribution `p_A` on subsets `A ⊆ {1, …, n_s}` of changing style variables, and for each `A`
the conditional density `q A s_A s̃_A = p(s̃_A | s_A)` of Assumption 3.2. -/
structure LatentModel (nc ns : ℕ) where
/-- The latent density `p_z`. -/
pz : Latent nc ns → ℝ
/-- The distribution `p_A` on subsets of style indices. -/
pA : Finset (Fin ns) → ℝ
/-- The conditional density `p(s̃_A | s_A)`. -/
q : (A : Finset (Fin ns)) → (A → ℝ) → (A → ℝ) → ℝ
/-- Section 3, Assumptions 3.1 and 3.2: `p_z` is a probability density on `Z = C × S`; `p_A` is a
probability distribution on the subsets of `{1, …, n_s}`; for each `A` and `s_A ∈ S_A`,
`p(· | s_A)` is a probability density on `ℝ^A`; and the augmented latent `z̃ = (c, s̃)` stays in `Z`
(the conditional `p_{z̃|z}` lives on `Z × Z`). -/
def IsAugModel {nc ns : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) : Prop :=
Measurable M.pz ∧ (∀ z ∈ C ×ˢ S, 0 ≤ M.pz z) ∧
(∫⁻ z in C ×ˢ S, ENNReal.ofReal (M.pz z)) = 1 ∧
(∀ A, 0 ≤ M.pA A) ∧ (∑ A, M.pA A) = 1 ∧
∀ A : Finset (Fin ns), Measurable (Function.uncurry (M.q A)) ∧
(∀ a ∈ projA A S, ∀ t, 0 ≤ M.q A a t) ∧
(∀ a ∈ projA A S, (∫⁻ t, ENNReal.ofReal (M.q A a t)) = 1) ∧
(∀ s ∈ S, ∀ t, merge A s t ∉ S → M.q A (restr A s) t = 0)
/-- Eqs. (2)-(3): `E[φ(z, z̃)]` for the pair `(z, z̃)` drawn by `z ∼ p_z`, `A ∼ p_A`,
`s̃_A ∼ p(· | s_A)`, `c̃ = c`, `s̃_{A^c} = s_{A^c}` (as an extended nonnegative integral). -/
def pairLIntegral {nc ns : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) (φ : Latent nc ns × Latent nc ns → ENNReal) : ENNReal :=
∑ A : Finset (Fin ns), ENNReal.ofReal (M.pA A) *
∫⁻ z in C ×ˢ S, ENNReal.ofReal (M.pz z) *
∫⁻ t : A → ℝ, ENNReal.ofReal (M.q A (restr A z.2) t) * φ (z, (z.1, merge A z.2 t))
/-- Eqs. (2)-(3): the probability that the latent pair `(z, z̃)` lies in `E`. -/
def pairProb {nc ns : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) (E : Set (Latent nc ns × Latent nc ns)) : ENNReal :=
pairLIntegral C S M (fun w => E.indicator (fun _ => 1) w)
/-- Eqs. (2)-(3): the probability that the observed pair `(x, x̃) = (f(z), f(z̃))` lies in `E`
(the law `p_{x,x̃}` of original and augmented views). -/
def obsPairProb {nc ns d : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) (f : Latent nc ns → Obs d) (E : Set (Obs d × Obs d)) : ENNReal :=
pairLIntegral C S M (fun w => E.indicator (fun _ => 1) (f w.1, f w.2))
/-- Eq. (2): the law of the latent `z ∼ p_z` on `Z`. -/
def latentLaw {nc ns : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) : Measure (Latent nc ns) :=
(volume.restrict (C ×ˢ S)).withDensity (fun z => ENNReal.ofReal (M.pz z))
/-- Section 3: a function on a subset `s` of a finite-dimensional space is smooth if near each
point of `s` it agrees on `s` with a `C^∞` function on an open set (for open `s` this is
`ContDiffOn ℝ ∞`; for the submanifold `𝒳 ⊆ ℝ^d` it is smoothness on `𝒳`). -/
def SmoothOnSet {E F : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [NormedAddCommGroup F]
[NormedSpace ℝ F] (s : Set E) (g : E → F) : Prop :=
∀ x ∈ s, ∃ U : Set E, IsOpen U ∧ x ∈ U ∧
∃ G : E → F, ContDiffOn ℝ ∞ G U ∧ Set.EqOn g G (U ∩ s)
/-- Theorem 4.2 (i): `f : Z → 𝒳 = f(Z)` is smooth and invertible with smooth inverse
(a diffeomorphism onto `𝒳`). -/
def CondI {nc ns d : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(f : Latent nc ns → Obs d) : Prop :=
SmoothOnSet (C ×ˢ S) f ∧ Set.InjOn f (C ×ˢ S) ∧
SmoothOnSet (f '' (C ×ˢ S)) (Function.invFunOn f (C ×ˢ S))
/-- Theorem 4.2 (ii): `p_z` is a smooth density on `Z` with `p_z(z) > 0` almost everywhere. -/
def CondII {nc ns : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) : Prop :=
ContDiffOn ℝ ∞ M.pz (C ×ˢ S) ∧ ∀ᵐ z ∂(volume.restrict (C ×ˢ S)), 0 < M.pz z
/-- Theorem 4.2 (iii): for every style index `l` there is `A ∋ l` with `p_A(A) > 0` such that
`p(s̃_A | s_A)` is smooth in `(s_A, s̃_A)` on `S_A × S_A` and, for every `s_A ∈ S_A`,
`p(· | s_A) > 0` on some open set containing `s_A`. -/
def CondIII {nc ns : ℕ} (S : Set (Fin ns → ℝ)) (M : LatentModel nc ns) : Prop :=
∀ l : Fin ns, ∃ A : Finset (Fin ns), l ∈ A ∧ 0 < M.pA A ∧
ContDiffOn ℝ ∞ (Function.uncurry (M.q A)) (projA A S ×ˢ projA A S) ∧
∀ a ∈ projA A S, ∃ O : Set (A → ℝ), IsOpen O ∧ a ∈ O ∧ ∀ t ∈ O, 0 < M.q A a t
/-- Definition 4.1: the true content `c = f⁻¹(x)_{1:n_c}` is block-identified by the inferred
content `ĉ(x)` if `ĉ = h(c)` for an invertible `h` (here: `h` injective on the content space
`C`), i.e. `ĉ(f(c, s)) = h(c)` for all `(c, s) ∈ Z`. -/
def BlockIdentifies {nc ns d : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(f : Latent nc ns → Obs d) (chat : Obs d → (Fin nc → ℝ)) : Prop :=
∃ h : (Fin nc → ℝ) → (Fin nc → ℝ), Set.InjOn h C ∧ ∀ z ∈ C ×ˢ S, chat (f z) = h z.1
/-- Squared Euclidean distance `‖a - b‖₂²`. -/
def sqDist {m : ℕ} (a b : Fin m → ℝ) : ℝ := ∑ i, (a i - b i) ^ 2
/-- Theorem 4.3: `g : 𝒳 → Z` is smooth and invertible (a smooth bijection of `𝒳 = f(Z)` onto `Z`). -/
def IsSmoothInvertible {nc ns d : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(f : Latent nc ns → Obs d) (g : Obs d → Latent nc ns) : Prop :=
SmoothOnSet (f '' (C ×ˢ S)) g ∧ Set.BijOn g (f '' (C ×ˢ S)) (C ×ˢ S)
/-- Eq. (4): `L_Align(g) = E_{(x, x̃)} ‖g(x)_{1:n_c} - g(x̃)_{1:n_c}‖₂²`. -/
def alignLoss {nc ns d : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) (f : Latent nc ns → Obs d) (g : Obs d → Latent nc ns) : ENNReal :=
pairLIntegral C S M (fun w => ENNReal.ofReal (sqDist (g (f w.1)).1 (g (f w.2)).1))
/-- Theorem 4.4: the open unit cube `(0,1)^{n_c}`. -/
def cube (m : ℕ) : Set (Fin m → ℝ) := Set.univ.pi (fun _ => Set.Ioo (0 : ℝ) 1)
open Classical in
/-- Theorem 4.4: differential entropy `H = -∫ p log p` of a law `μ` on `ℝ^m` with density
`p = dμ/dλ`, in `[-∞, ∞]`; it is `-∞` when `μ` has no density. -/
def diffEntropy {m : ℕ} (μ : Measure (Fin m → ℝ)) : EReal :=
if μ ≪ volume then
((∫⁻ x, ENNReal.ofReal (Real.negMulLog (μ.rnDeriv volume x).toReal) : ENNReal) : EReal) -
((∫⁻ x, ENNReal.ofReal (-Real.negMulLog (μ.rnDeriv volume x).toReal) : ENNReal) : EReal)
else ⊥
open Classical in
/-- Eq. (5): `L_AlignMaxEnt(g) = E_{(x, x̃)} ‖g(x) - g(x̃)‖₂² - H(g(x))` for `g : 𝒳 → (0,1)^{n_c}`,
where `g(x)` has the law of `g(f(z))`, `z ∼ p_z` (the entropy is `-∞`, so the loss `+∞`, when
`g ∘ f` is not a random variable). -/
def alignMaxEntLoss {nc ns d : ℕ} (C : Set (Fin nc → ℝ)) (S : Set (Fin ns → ℝ))
(M : LatentModel nc ns) (f : Latent nc ns → Obs d) (g : Obs d → (Fin nc → ℝ)) : EReal :=
((pairLIntegral C S M (fun w => ENNReal.ofReal (sqDist (g (f w.1)) (g (f w.2)))) : ENNReal) :
EReal) -
(if AEMeasurable (fun z => g (f z)) (latentLaw C S M) then
diffEntropy ((latentLaw C S M).map (fun z => g (f z)))
else ⊥)
end ContentStyle
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Latent. For natural numbers (either may be ), is the set of pairs with and . Coordinates of are indexed by and coordinates of by . Its topology and Lebesgue measure are those of the product . When or , the corresponding factor is a single point, and Lebesgue measure on it is the unit point mass.
Obs. For a natural number , , with coordinates indexed by . When this is a single point.
IsLatentSpace. For sets and , holds exactly when both of the following hold:
- the product is an open subset of ;
- , with the subspace topology, is a simply connected space.
Simple connectedness includes path-connectedness, hence non-emptiness. So the predicate forces and , and therefore both and are open. None of the other declarations below takes this predicate as a hypothesis.
restr. For a finite set of style indices and a style vector , is the family . When , is a single point and is that point.
projA. For and ,
the image of under restriction to the coordinates in . If then . If and , is the single point of .
merge. For , and , is the vector with
In words, the -coordinates of are overwritten by and the others are kept. For the result is .
LatentModel. A latent model for is a bare triple of real-valued functions with no conditions imposed:
- ;
- , which assigns a real number to every subset ;
- for each , a function , written .
Negativity, non-measurability and non-normalization are all allowed at this level.
IsAugModel. Write and let denote Lebesgue measure. For , and a latent model , asserts all of the following.
- is Borel measurable on all of .
- for every . No sign condition is imposed outside .
- , as an integral in .
- for every , and . The sum runs over all subsets, including .
- For every :
- (a) is jointly measurable on ;
- (b) for every and every , ;
- (c) for every , , where Lebesgue measure on is the unit point mass when , so then at the single point;
- (d) for every and every , if then .
Since sees only , condition (d) forces as soon as some with has . No conditions are placed on for . The content set enters only through conditions 2 and 3. If is Lebesgue-null and , condition 3 cannot hold.
pairLIntegral. For as above and any function from pairs of latents to (no measurability required), the quantity is
Here and the integrals are lower Lebesgue integrals in . The second argument of is the pair : the same content , and a style that agrees with off and equals on . For , . No hypothesis such as is assumed, and negative parts of are replaced by .
pairProb. For a set of latent pairs, , where if and otherwise. The value lies in . It equals a probability only when the weights are normalized, e.g. under . need not be measurable.
obsPairProb. For a map and a set , , where if and otherwise. The value lies in . No regularity of or measurability of is required.
latentLaw. The measure on given by
That is, Lebesgue measure restricted to , with density . Its total mass is , which equals only if that normalization is separately assumed.
SmoothOnSet. For real normed spaces , a set and a map , means:
for every there exist an open set with and a map that is on , such that for all .
Values of off are irrelevant. If the condition holds vacuously. Here means infinitely differentiable (not analytic).
CondI. For and , asserts three things:
- holds.
- is injective on .
- holds.
In item 3, is the choice-based inverse. It sends to some with , which is unique by item 2. Points outside the image go to an unspecified value, which plays no role. Smoothness of the inverse is in the local-extension sense of : near each point of it agrees on with a map that is on an open subset of . Nothing requires to be a submanifold, and nothing requires to be open.
CondII. asserts two things:
- is on the set , in the sense of differentiability within that set;
- for Lebesgue-almost every .
If is Lebesgue-null, the second clause is vacuous.
CondIII. asserts that for every style index there exists satisfying all of the following:
- ;
- ;
- is on the set , in the within-set sense;
- for every there is an open set with such that for all .
The set need not lie inside , and different may use different sets . When the condition holds vacuously.
BlockIdentifies. For , and , means: there exists a function that is injective on and satisfies
No continuity, measurability or surjectivity of is required, and need not have an inverse defined beyond injectivity on . The condition is automatically true if , or if .
sqDist. For ,
which equals when .
IsSmoothInvertible. For , and , the predicate asserts two things:
- holds.
- is a bijection from onto : it maps into , is injective there, and every point of is hit.
Nothing is said about off , about smoothness of 's inverse, or about itself, which is not assumed injective here.
alignLoss. For as above and , write for the first (content) block of . Then
That is the weighted integral, defined above, of the squared Euclidean distance between the content blocks of applied to and to . It is not assumed that is smooth, invertible, or measurable.
cube. , the open unit cube. For it is the single point of .
diffEntropy. For any measure on (not necessarily finite or a probability), let be its Radon–Nikodym derivative with respect to Lebesgue measure, taken as a real number, with value wherever the derivative is . Let , with . Then
valued in the extended reals . Each integral lies in . If both integrals are , the difference evaluates to . For , is the unit point mass.
alignMaxEntLoss. For and , with defined on all of and not required to take values in , the loss is the extended real number
where the first term lies in and
Here is , and is its pushforward to . Since is normalized only if separately assumed, this pushforward need not be a probability measure.
The subtraction is in , so the boundary cases resolve as follows:
- if , the loss is ;
- if the first term is and , the loss is .
The cube is not referenced in this definition.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.