Discriminative softmax models, linear similarity and diversity matrices
DefinitionLinearIdentifiability_BasicObjects of Sections 2-3 and Appendix C of Roeder, Metz and Kingma (2020).
- Data model (Section 2.1). is an empirical (finitely supported) probability distribution over inputs , targets and finite candidate sets . A pair occurs in the data if , and then . The input support is the set of with and the target support is the set of with .
- Canonical discriminative form (eq. 1). For a representation and a context map ,
- Equality of models (eq. 4). means that the models and give the same for every that occurs in the data and every .
- Linear similarity (Definition 1, eq. 3). and are linearly similar if for all and for all , for invertible matrices . For a parameterisation of , means that and are linearly similar.
- Diversity matrices. For tuples , is the matrix with columns (Section 3.2). For tuples , the matrix has columns (Appendix C.1).
- Augmented representations (Appendix C, eqs. 10-12). , and .
Formalization Note The data distribution is a PMF on with finite support; only its support enters the statements. The parameter space appears only in Definition 1: the theorems quantify directly over the four functions , which is equivalent to quantifying over all parameterisations and all . A vector of is a function , and a matrix is invertible when its determinant is a unit.
import Mathlib
/-!
Roeder, Metz and Kingma, *On Linear Identifiability of Learned Representations*,
arXiv:2007.00810v3 (2020): the data model of Section 2.1, the canonical discriminative form
(eq. (1)) of Section 2.2, the linear similarity relation of Definition 1 (Section 3.2), the matrices
of the diversity conditions (Section 3.2 and Appendix C.1), and the augmented representations of
Appendix C, eqs. (9)-(12).
-/
open Matrix
noncomputable section
namespace LinearIdentifiability
variable {X Y : Type*} {M : ℕ}
/-- Section 2.1: `p_D(x, S) > 0`, i.e. the pair `(x, S)` occurs in the data distribution
`p_D(x, y, S)` (some target `y` has positive probability together with `x` and `S`). -/
def IsSupported (pD : PMF (X × Y × Finset Y)) (x : X) (S : Finset Y) : Prop :=
∃ y, pD (x, y, S) ≠ 0
/-- Section 2.1: `p_D` is an empirical distribution (finitely supported) over inputs `x`, targets
`y` and finite candidate sets `S`, and `S` is the set of possible values of `y` given `x`:
`p_D(y | x, S) > 0 ⇔ y ∈ S` whenever `p_D(x, S) > 0`. -/
def IsDataDistribution (pD : PMF (X × Y × Finset Y)) : Prop :=
pD.support.Finite ∧ ∀ x S, IsSupported pD x S → ∀ y, (pD (x, y, S) ≠ 0 ↔ y ∈ S)
/-- Section 2.1: the inputs `x` that occur in the data, `p_D(x) > 0`. -/
def inputSupport (pD : PMF (X × Y × Finset Y)) : Set X :=
{x | ∃ S, IsSupported pD x S}
/-- Section 2.1: the targets `y` that occur in the data, `p_D(y) > 0`. -/
def targetSupport (pD : PMF (X × Y × Finset Y)) : Set Y :=
{y | ∃ x S, pD (x, y, S) ≠ 0}
/-- Section 2.2, eq. (1): the canonical discriminative form
`p_θ(y | x, S) = exp(f(x)ᵀ g(y)) / ∑_{y' ∈ S} exp(f(x)ᵀ g(y'))`, for a representation
`f : X → ℝ^M` and a context map `g : Y → ℝ^M`. -/
def modelProb (f : X → Fin M → ℝ) (g : Y → Fin M → ℝ) (x : X) (S : Finset Y) (y : Y) : ℝ :=
Real.exp (f x ⬝ᵥ g y) / ∑ y' ∈ S, Real.exp (f x ⬝ᵥ g y')
/-- Section 3.1-3.2, eq. (4): `p' = p*`, the two models `(f', g')` and `(f*, g*)` of the form (1)
assign the same conditional probability `p(y | x, S)` to every `(x, S)` occurring in the data and
every `y ∈ S`. -/
def ModelsAgreeOnData (pD : PMF (X × Y × Finset Y)) (f' : X → Fin M → ℝ) (g' : Y → Fin M → ℝ)
(fstar : X → Fin M → ℝ) (gstar : Y → Fin M → ℝ) : Prop :=
∀ x S, IsSupported pD x S → ∀ y ∈ S, modelProb f' g' x S y = modelProb fstar gstar x S y
/-- Definition 1, eq. (3), on representation functions: `f' = A f*` and `g' = B g*` for invertible
`M × M` matrices `A` and `B`. -/
def LinearlySimilar (f' fstar : X → Fin M → ℝ) (g' gstar : Y → Fin M → ℝ) : Prop :=
∃ A B : Matrix (Fin M) (Fin M) ℝ, IsUnit A.det ∧ IsUnit B.det ∧
(∀ x, f' x = A *ᵥ fstar x) ∧ (∀ y, g' y = B *ᵥ gstar y)
/-- Definition 1, eq. (3): the relation `θ' ∼_L θ*` on the parameter space `Θ`, for a
parameterisation `θ ↦ (f_θ, g_θ)`. -/
def LinSim {Θ : Type*} (f : Θ → X → Fin M → ℝ) (g : Θ → Y → Fin M → ℝ) (θ' θstar : Θ) : Prop :=
LinearlySimilar (f θ') (f θstar) (g θ') (g θstar)
/-- Section 3.2 (diversity condition): the `M × M` matrix `L` whose `i`-th column is
`g(y_A^{(i)}) - g(y_B^{(i)})`, for tuples `(y_A^{(i)}, y_B^{(i)}) = (a i, b i)`. -/
def diffMatrix (g : Y → Fin M → ℝ) (a b : Fin M → Y) : Matrix (Fin M) (Fin M) ℝ :=
fun j i => g (a i) j - g (b i) j
/-- Appendix C, eq. (12): the log-normaliser `Z(x, S) = log ∑_{y' ∈ S} exp(f(x)ᵀ g(y'))`. -/
def logNormalizer (f : X → Fin M → ℝ) (g : Y → Fin M → ℝ) (x : X) (S : Finset Y) : ℝ :=
Real.log (∑ y' ∈ S, Real.exp (f x ⬝ᵥ g y'))
/-- Appendix C, eq. (10): the augmented representation `f̃(x, S) = [-Z(x, S); f(x)] ∈ ℝ^{M+1}`. -/
def augF (f : X → Fin M → ℝ) (g : Y → Fin M → ℝ) (x : X) (S : Finset Y) : Fin (M + 1) → ℝ :=
vecCons (-logNormalizer f g x S) (f x)
/-- Appendix C, eq. (11): the augmented context map `g̃(y) = [1; g(y)] ∈ ℝ^{M+1}`. -/
def augG (g : Y → Fin M → ℝ) (y : Y) : Fin (M + 1) → ℝ :=
vecCons 1 (g y)
/-- Appendix C.1 (diversity condition for `g`): the `(M+1) × (M+1)` matrix whose `i`-th column is
`f̃(x^{(i)}, S^{(i)})`, `i = 0, …, M`. -/
def augFMatrix (f : X → Fin M → ℝ) (g : Y → Fin M → ℝ) (xs : Fin (M + 1) → X)
(Ss : Fin (M + 1) → Finset Y) : Matrix (Fin (M + 1)) (Fin (M + 1)) ℝ :=
fun j i => augF f g (xs i) (Ss i) j
end LinearIdentifiability
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Setting (common to all declarations below). and are arbitrary types: is the space of inputs and the space of targets . No structure, finiteness or nonemptiness is assumed of them. is an arbitrary natural number, and is allowed. Vectors in have coordinates indexed by . is the standard dot product, which is when . A "probability mass function" on is a function that:
- is defined on the triples , where is the set of finite subsets ;
- takes values in ;
- has values summing to .
IsSupported. Given a probability mass function on triples with , , a finite subset of , an input and a finite set , the pair is called supported exactly when there exists some with
The witness ranges over all of ; nothing requires .
IsDataDistribution. A probability mass function on triples (with , , a finite subset of ) is called a data distribution exactly when both of the following hold:
- The set of triples with is finite.
- For every and every finite such that is supported, i.e. for some , and for every :
Consequences:
- For a supported pair , every element of receives positive mass and no element outside does.
- In particular, is nonempty for every supported pair.
- No condition is imposed on unsupported pairs . On those, is zero for every by definition.
The condition is stated directly on the joint masses , not on a conditional distribution.
inputSupport. For a probability mass function on triples (, , a finite subset of ), the input support is the set
These are the inputs for which some pair is supported, i.e. which carry positive marginal mass.
targetSupport. For a probability mass function on triples (, , a finite subset of ), the target support is the set
These are the targets carrying positive marginal mass. No requirement that is built in here.
modelProb. Given:
- a representation ;
- a context map ;
- an input ;
- a finite set ;
- a target ;
the model probability is the real number
Edge cases:
- is not required to lie in . If the formula is still evaluated, and the result can exceed .
- If , the denominator is and the value is defined to be , by the convention that division by zero yields .
- If , every dot product is and the value is , which is when .
ModelsAgreeOnData. Given:
- a probability mass function on triples (, , a finite subset of );
- two pairs of maps and ;
the models agree on the data exactly when, for every and every finite such that for some , and for every ,
Scope of the condition:
- is not required to satisfy the data-distribution condition.
- Nothing is required for pairs outside the support.
- Nothing is required for targets .
LinearlySimilar. Given maps and , the pair is linearly similar to exactly when there exist real matrices and with and such that
Notes:
- The equalities are required on all of and all of , not only on any data support.
- and are independent invertible matrices; no relation between them (such as ) is imposed.
- When the condition holds trivially for all maps.
LinSim. Given:
- a parameter type , which is arbitrary;
- parameterised maps and ;
two parameters satisfy exactly when there exist real matrices with and such that
As with LinearlySimilar, and are unrelated, and when every pair of parameters is related.
diffMatrix. Given:
- a context map ;
- two -tuples of targets and in ;
this is the real matrix with entries
Its -th column is the vector . The definition only builds the matrix: it does not assert invertibility, distinctness of and , or membership of the targets in any support. For it is the empty matrix.
logNormalizer. Given , , an input and a finite set , the log-normaliser is
If the sum is and the value is defined to be , by the convention .
augF. Given , , and a finite , the augmented representation is the vector in (coordinates indexed ) given by
The coordinates are as follows:
- Coordinate is .
- Coordinates are .
When , coordinate equals .
augG. Given and , the augmented context vector in (coordinates indexed ) is
Coordinate is , and coordinates are .
augFMatrix. Given:
- and ;
- an -tuple of inputs ;
- an -tuple of finite sets ;
this is the real matrix whose entry is the -th coordinate of , for . Equivalently, its -th column is
In particular, row holds the negated log-normalisers and rows hold the vectors . The definition only builds the matrix: it does not assert invertibility, does not require the pairs to be supported by any distribution, and does not require the sets to be nonempty. An empty puts in row of column .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.