Unconstrained feature model with regularized cross-entropy: objective, nuclear norm, Simplex ETF Gram matrix, landscape notions
DefinitionNeuralCollapseUFM_BasicObjects of Sections 2.2 and 3 and Appendices A-C of Zhu, Ding, Zhou, Li, You, Sulam and Qu (2021).
- Cross-entropy (eq. 3). For logits and the one-hot label , .
- Norms. , , is the spectral (operator ) norm, and is the nuclear norm.
- Simplex ETF (Definition A.1). is the all-ones matrix, and is the Gram matrix of the standard -Simplex ETF .
- Unconstrained feature model (eqs. 4, 14, 15). There are classes with samples each, . The parameters are a classifier with rows , features and a bias . The averaged cross-entropy of a logit matrix with columns is , and the objective is
- Gradient of . .
- Constant of Lemma B.5. .
- Convex counterpart (eq. 37). .
- Landscape notions. A critical point is a point where every directional derivative of vanishes. A global minimizer minimizes over all parameters. A strict saddle (footnote 3) is a critical point at which the Hessian has a negative eigenvalue, that is, for some direction .
- Collapsed solution (Theorem 3.1). is collapsed if there are with for all , with or , for all , for all , and
Formalization Note The parameters are the triple with the product topology. Column of is the feature . The nuclear norm is the sum of the square roots of the eigenvalues of (the roots of its characteristic polynomial, with multiplicity). The gradient is written out explicitly. The collapsed form is stated with multiplied by instead of normalizing by , so that it is defined when (see Theorem 3.1).
import Mathlib
/-!
Zhu, Ding, Zhou, Li, You, Sulam and Qu, *A Geometric Analysis of Neural Collapse with
Unconstrained Features*, arXiv:2105.02375v1 (2021): the cross-entropy loss (eq. (3)), the
unconstrained feature model with weight decay (eq. (4), restated as eqs. (14)-(15)), the Simplex
ETF (Definition A.1), the nuclear norm (Lemma A.3), the constant `c_2` of Lemma B.5, the
convex counterpart (37), and the notions of critical point, local and global minimizer and
strict saddle used in Section 3 (footnote 3, p. 4).
-/
open Matrix
noncomputable section
namespace NeuralCollapseUFM
variable {K d n : ℕ}
/-- Eq. (3), p. 7: the cross-entropy loss of a logit vector `z ∈ ℝ^K` for the one-hot label
`y_k`, `L_CE(z, y_k) = -log(exp(z_k) / ∑_i exp(z_i))`. -/
def ceLoss (z : Fin K → ℝ) (k : Fin K) : ℝ :=
-Real.log (Real.exp (z k) / ∑ i, Real.exp (z i))
/-- Squared Euclidean norm `‖v‖₂²` of a vector (Notations, p. 6). -/
def sqNorm {m : Type*} [Fintype m] (v : m → ℝ) : ℝ := ∑ i, v i ^ 2
/-- Squared Frobenius norm `‖A‖_F²` of a real matrix (Notations, p. 6). -/
def frobSq {m p : Type*} [Fintype m] [Fintype p] (A : Matrix m p ℝ) : ℝ :=
∑ i, ∑ j, A i j ^ 2
/-- The all-ones matrix `1_K 1_K^T` (Notations, p. 6). -/
def onesMat (K : ℕ) : Matrix (Fin K) (Fin K) ℝ := Matrix.of fun _ _ => 1
/-- Definition A.1, p. 28: the Gram matrix `M^T M = K/(K-1) (I_K - (1/K) 1_K 1_K^T)` of the
standard `K`-Simplex ETF `M = sqrt(K/(K-1)) (I_K - (1/K) 1_K 1_K^T)`. -/
def etfGram (K : ℕ) : Matrix (Fin K) (Fin K) ℝ :=
((K : ℝ) / ((K : ℝ) - 1)) • ((1 : Matrix (Fin K) (Fin K) ℝ) - (1 / (K : ℝ)) • onesMat K)
/-- Section 2.2, p. 8: the parameters `(W, H, b)` of the unconstrained feature model, with
classifier `W ∈ ℝ^{K×d}` (rows `w^k`), features `H = [h_{1,1} ⋯ h_{K,n}] ∈ ℝ^{d×N}`, `N = nK`,
whose column `(k, i)` is the feature `h_{k,i}` of the `i`-th sample of class `k`, and bias
`b ∈ ℝ^K`. -/
abbrev Params (K d n : ℕ) : Type :=
Matrix (Fin K) (Fin d) ℝ × Matrix (Fin d) (Fin K × Fin n) ℝ × (Fin K → ℝ)
/-- Eq. (14), p. 30: the logit matrix `W H + b 1^T ∈ ℝ^{K×N}`; its column `(k, i)` is
`W h_{k,i} + b`. -/
def logits (W : Matrix (Fin K) (Fin d) ℝ) (H : Matrix (Fin d) (Fin K × Fin n) ℝ)
(b : Fin K → ℝ) : Matrix (Fin K) (Fin K × Fin n) ℝ :=
W * H + Matrix.of fun j _ => b j
/-- Eq. (15), p. 30: the averaged cross-entropy
`g(Z) = (1/N) ∑_{k=1}^K ∑_{i=1}^n L_CE(z_{k,i}, y_k)` of a logit matrix `Z ∈ ℝ^{K×N}`, `N = Kn`,
whose column `(k, i)` is `z_{k,i}`. -/
def ceRisk (Z : Matrix (Fin K) (Fin K × Fin n) ℝ) : ℝ :=
(1 / ((K : ℝ) * n)) * ∑ k, ∑ i, ceLoss (fun j => Z j (k, i)) k
/-- Eq. (4), p. 8 (restated as eq. (14), p. 30): the regularized cross-entropy objective
`f(W, H, b) = g(W H + b 1^T) + (λ_W/2) ‖W‖_F² + (λ_H/2) ‖H‖_F² + (λ_b/2) ‖b‖₂²`. -/
def objective (lamW lamH lamb : ℝ) (x : Params K d n) : ℝ :=
ceRisk (logits x.1 x.2.1 x.2.2) + lamW / 2 * frobSq x.1 + lamH / 2 * frobSq x.2.1 +
lamb / 2 * sqNorm x.2.2
/-- Eqs. (17)-(18), p. 32: the gradient `∇g(Z) ∈ ℝ^{K×N}` of the averaged cross-entropy (15),
written out: its entry `(j, (k, i))` is `(1/N) (softmax(z_{k,i})_j - [j = k])`. -/
def ceRiskGrad (Z : Matrix (Fin K) (Fin K × Fin n) ℝ) : Matrix (Fin K) (Fin K × Fin n) ℝ :=
Matrix.of fun j c =>
(1 / ((K : ℝ) * n)) *
(Real.exp (Z j c) / ∑ l, Real.exp (Z l c) - if j = c.1 then 1 else 0)
/-- The spectral (operator) norm `‖A‖` of a real matrix, as the operator norm of
`A : ℓ²(p) → ℓ²(m)` (used in eq. (43), p. 41 and in Lemma C.3). -/
def opNorm {m p : Type*} [Fintype m] [Fintype p] [DecidableEq p] (A : Matrix m p ℝ) : ℝ :=
‖LinearMap.toContinuousLinearMap (Matrix.toEuclideanLin A)‖
/-- Lemma A.3, p. 28: the nuclear norm `‖Z‖_* = ∑_k σ_k(Z)`, computed as the sum of the square
roots of the eigenvalues of `Z Z^T` (the roots of its characteristic polynomial, with
multiplicity). -/
def nuclearNorm {m p : Type*} [Fintype m] [DecidableEq m] [Fintype p] (Z : Matrix m p ℝ) : ℝ :=
(((Z * Zᵀ).charpoly.roots).map Real.sqrt).sum
/-- Lemma B.5, p. 36: the constant
`c_2 = (1/(1+c_1)) log((1+c_1)(K-1)) + (c_1/(1+c_1)) log((1+c_1)/c_1)`. -/
def cTwo (K : ℕ) (c1 : ℝ) : ℝ :=
1 / (1 + c1) * Real.log ((1 + c1) * ((K : ℝ) - 1)) + c1 / (1 + c1) * Real.log ((1 + c1) / c1)
/-- Eq. (37), p. 38: the convex counterpart
`f̃(Z, b) = g(Z + b 1^T) + sqrt(λ_W λ_H) ‖Z‖_* + (λ_b/2) ‖b‖₂²`. -/
def convexObjective (lamW lamH lamb : ℝ) (Z : Matrix (Fin K) (Fin K × Fin n) ℝ)
(b : Fin K → ℝ) : ℝ :=
ceRisk (Z + Matrix.of fun j _ => b j) + Real.sqrt (lamW * lamH) * nuclearNorm Z +
lamb / 2 * sqNorm b
/-- A critical point `∇F(x) = 0` (Section 3.2 and Appendix C, p. 38), written as the vanishing of
every directional derivative `d/dt F(x + t Δ)|_{t=0}`. -/
def IsCriticalPoint {E : Type*} [AddCommGroup E] [Module ℝ E] (F : E → ℝ) (x : E) : Prop :=
∀ Δ : E, HasDerivAt (fun t : ℝ => F (x + t • Δ)) 0 0
/-- A global minimizer of `F` (Theorem 3.1, p. 9). -/
def IsGlobalMinimizer {E : Type*} (F : E → ℝ) (x : E) : Prop :=
∀ y : E, F x ≤ F y
/-- Footnote 3, p. 4: the Hessian of `F` at `x` has a negative eigenvalue, i.e. there is a
direction `Δ` with negative curvature `∇²F(x)[Δ, Δ] = d²/dt² F(x + t Δ)|_{t=0} < 0`. -/
def HasNegativeCurvature {E : Type*} [AddCommGroup E] [Module ℝ E] (F : E → ℝ) (x : E) :
Prop :=
∃ Δ : E, deriv (deriv fun t : ℝ => F (x + t • Δ)) 0 < 0
/-- Footnote 3, p. 4: a strict saddle is a critical point at which the Hessian has at least one
negative eigenvalue. -/
def IsStrictSaddle {E : Type*} [AddCommGroup E] [Module ℝ E] (F : E → ℝ) (x : E) : Prop :=
IsCriticalPoint F x ∧ HasNegativeCurvature F x
/-- Theorem 3.1, eqs. (5)-(6), p. 9, in the form established by its proof (Appendix B,
conditions (a)-(d), p. 31): all rows of `W` have the same norm `w`, `b = b 1` with `b = 0` or
`λ_b = 0`, `h_{k,i} = sqrt(λ_W/(λ_H n)) w^k`, the class-wise means `h̄_i = (1/K) ∑_j h_{j,i}`
vanish, and `W W^T = w² · K/(K-1) (I_K - (1/K) 1_K 1_K^T)`. -/
def IsCollapsedSolution (lamW lamH lamb : ℝ) (x : Params K d n) : Prop :=
∃ w b0 : ℝ,
(∀ k, Real.sqrt (sqNorm (x.1 k)) = w) ∧
(x.2.2 = fun _ => b0) ∧ (b0 = 0 ∨ lamb = 0) ∧
(∀ k i, (fun r => x.2.1 r (k, i)) = Real.sqrt (lamW / (lamH * n)) • x.1 k) ∧
(∀ i, (1 / (K : ℝ)) • (∑ j, fun r => x.2.1 r (j, i)) = 0) ∧
x.1 * x.1ᵀ = w ^ 2 • etfGram K
end NeuralCollapseUFM
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
This bundle contains only definitions and asserts no theorem. Throughout, are arbitrary natural numbers, and each may be . Real division by zero is taken to be . The real logarithm satisfies and for . The real square root satisfies for .
ceLoss (cross-entropy). For a vector and a class index , it is defined as
Because some index exists, , so the denominator is strictly positive and no junk value arises.
sqNorm. For a vector indexed by any finite set , it is . It is when is empty.
frobSq. For a real matrix with finite row set and finite column set , it is .
onesMat. This is the matrix , all of whose entries are .
etfGram. It is the real matrix
computed with real arithmetic.
- For , its diagonal entries are and its off-diagonal entries are .
- For , the factor is taken to be , so .
- For , it is the empty matrix.
Params. A parameter is a triple :
- , whose -th row is written ;
- , a real matrix whose columns are indexed by pairs with , ; column is written ;
- .
logits. For such , it is the matrix , whose entry in row and column is
so that column equals .
ceRisk. For a matrix with column written , it is
The column is thus paired with the label . If or , the sum is empty and ; the prefactor is also .
objective. For arbitrary real numbers (no sign or positivity condition) and a parameter , it is
ceRiskGrad. For a matrix , it is the matrix whose entry in row and column is
where is if and otherwise. This is only an explicit formula. Nothing in the bundle asserts that it is the gradient of .
opNorm. For a real matrix with finite row set and finite column set , it is the operator norm of the linear map from Euclidean to Euclidean , both with the norm:
nuclearNorm. For a real matrix , it is computed as follows:
- Take the characteristic polynomial of the matrix .
- Take its real roots, listed with multiplicity, .
- Define
Since is symmetric positive semidefinite, its characteristic polynomial has real nonnegative roots counted with multiplicity. Zero eigenvalues contribute to the sum.
cTwo. For a natural number and a real number , it is
No restriction is placed on or , so the junk conventions apply:
- makes both coefficients ;
- gives ;
- gives ;
- negative arguments of are replaced by their absolute values.
convexObjective. For arbitrary real , a matrix and , it is
Here is the root-sum defined above, and is whenever .
IsCriticalPoint. For a function on any real vector space (no norm or topology on is used), is a critical point iff, for every direction , the one-variable function
is differentiable at with . This requires every directional derivative at to exist and vanish. It does not require to be differentiable at in any stronger sense.
IsGlobalMinimizer. For any type and , is a global minimizer iff for all . The inequality is non-strict, so minimizers need not be unique.
HasNegativeCurvature. For on a real vector space, this holds at iff there exists a direction with
The derivatives follow the convention that the derivative of a function at a point where it is not differentiable is :
- The inner derivative is that everywhere-defined function, set to at points of non-differentiability.
- The outer derivative must exist at and be strictly negative, since a non-differentiable outer function would yield , which is not .
No twice-differentiability of , Hessian, or eigenvalue appears.
IsStrictSaddle. is a strict saddle of iff is a critical point of in the sense above and has negative curvature at in the sense above. It does not require that fail to be a local minimizer.
IsCollapsedSolution. For arbitrary real and a parameter , this holds iff there exist real numbers and such that all of the following hold:
- Every row has the same Euclidean norm: for every . When , this forces .
- , meaning every entry of equals .
- or .
- For every and ,
where is read as a vector in . The square root is if or if the ratio is ; in those cases every must be . 5. For every ,
- The Gram matrix of the rows of satisfies
For , this says when and when . For , because , it forces and hence .
This predicate is purely structural. It makes no reference to the value of the objective , to minimality, or to criticality.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.