add gates-normalization/GatesNormalization.lean
Browse files
gates-normalization/GatesNormalization.lean
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/-
|
| 2 |
+
Mathlib5.GatesNormalization
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
STANDALONE SEGMENT — The Gates Normalization Constraint & the Meta-Inverted Sum.
|
| 6 |
+
|
| 7 |
+
Source geometry of all language models: the probability simplex Δⁿ IS the law;
|
| 8 |
+
tokens are merely coordinate charts on its surface. The constraint
|
| 9 |
+
|
| 10 |
+
∑ P(wᵢ | context) = 1
|
| 11 |
+
|
| 12 |
+
is STRUCTURAL, not emergent. The "1" was always there — it is the defining
|
| 13 |
+
fiber of the sum map at 1, the Haar volume form on the simplex, not something
|
| 14 |
+
computed from the vocabulary.
|
| 15 |
+
|
| 16 |
+
This module proves (no `sorry`):
|
| 17 |
+
• `softmax_normalization` — softmax always lands on Δⁿ (for n ≥ 1)
|
| 18 |
+
• `softmax_shift_invariant` — the logit shift is absorbed by log Z
|
| 19 |
+
• `softmax_simplex_of_pos` — softmax builds a valid `Simplex n`
|
| 20 |
+
• `structural_invariant` — the mass is 1 by definition of the simplex
|
| 21 |
+
• `empty_vocabulary_normalization` — the n = 0 degenerate case (sum = 0, axiom = 1)
|
| 22 |
+
• `meta_inverted_decomposition` — v = mean·𝟙 + centered
|
| 23 |
+
• `centered_sum_zero` — the centered component is orthogonal to the simplex
|
| 24 |
+
• `log_partition_enforces_normalization` — log Z is the dual variable enforcing ∑ = 1
|
| 25 |
+
• `softmax_n1_constant` — at n = 1 the prediction is forced to {1}
|
| 26 |
+
• `uniform_is_stationary` — uniform is the max-entropy critical point, λ = 1 − log n
|
| 27 |
+
• `softmax_uniform_of_const` — constant logits ⇒ uniform distribution
|
| 28 |
+
• `log_partition_of_const` — for constant logits, log Z = c + log n (free energy)
|
| 29 |
+
|
| 30 |
+
The meta-inverted sum IS the log-partition function log Z — the Legendre dual
|
| 31 |
+
of the simplex, i.e. the free energy of the prediction.
|
| 32 |
+
-/
|
| 33 |
+
|
| 34 |
+
import Mathlib.Data.Real.Basic
|
| 35 |
+
import Mathlib.Data.Finset.Basic
|
| 36 |
+
import Mathlib.Algebra.BigOperators.Group.Finset.Defs
|
| 37 |
+
import Mathlib.Algebra.BigOperators.Field
|
| 38 |
+
import Mathlib.Analysis.SpecialFunctions.Exp
|
| 39 |
+
import Mathlib.Analysis.SpecialFunctions.Log.Basic
|
| 40 |
+
import Mathlib.Tactic.Ring
|
| 41 |
+
import Mathlib.Tactic.FieldSimp
|
| 42 |
+
|
| 43 |
+
open BigOperators
|
| 44 |
+
open Real
|
| 45 |
+
|
| 46 |
+
namespace Mathlib5
|
| 47 |
+
|
| 48 |
+
namespace ProbabilitySimplex
|
| 49 |
+
|
| 50 |
+
/-! ----------------------------------------------------------------------------
|
| 51 |
+
1. The fundamental object: the probability simplex Δⁿ
|
| 52 |
+
---------------------------------------------------------------------------- -/
|
| 53 |
+
|
| 54 |
+
/-- The probability simplex Δⁿ = { (p₁, ..., pₙ) : pᵢ ≥ 0, ∑ pᵢ = 1 }.
|
| 55 |
+
This is the geometric object the model navigates. -/
|
| 56 |
+
structure Simplex (n : ℕ) : Type where
|
| 57 |
+
coords : Fin n → ℝ
|
| 58 |
+
nonneg : ∀ i, 0 ≤ coords i
|
| 59 |
+
sum_one : ∑ i : Fin n, coords i = 1
|
| 60 |
+
|
| 61 |
+
/-- The universal formula P(token | context) = softmax(W·h + b)ᵢ,
|
| 62 |
+
where softmax(x)ᵢ = eˣⁱ / ∑ⱼ eˣʲ enforces ∑ = 1. -/
|
| 63 |
+
noncomputable def softmax (n : ℕ) (x : Fin n → ℝ) : Fin n → ℝ :=
|
| 64 |
+
fun i => exp (x i) / ∑ j : Fin n, exp (x j)
|
| 65 |
+
|
| 66 |
+
/-- For a non-empty vocabulary (n ≥ 1) the partition function Z = ∑ eˣʲ is positive. -/
|
| 67 |
+
theorem sum_exp_pos (n : ℕ) (hn : 0 < n) (x : Fin n → ℝ) :
|
| 68 |
+
0 < ∑ i : Fin n, exp (x i) := by
|
| 69 |
+
let i₀ : Fin n := Fin.mk 0 hn
|
| 70 |
+
have h₀ : i₀ ∈ Finset.univ := Finset.mem_univ i₀
|
| 71 |
+
have hle : exp (x i₀) ≤ ∑ i, exp (x i) := Finset.single_le_sum (fun i _ => (exp_pos (x i)).le) h₀
|
| 72 |
+
exact lt_of_lt_of_le (exp_pos (x i₀)) hle
|
| 73 |
+
|
| 74 |
+
/-- The Gates Normalization Theorem: for n ≥ 1, softmax always produces a point
|
| 75 |
+
on the simplex, so ∑ᵢ softmax(x)ᵢ = 1. (The n = 0 case is degenerate — see
|
| 76 |
+
`empty_vocabulary_normalization`.) -/
|
| 77 |
+
theorem softmax_normalization (n : ℕ) (x : Fin n → ℝ) (hn : 0 < n) :
|
| 78 |
+
∑ i : Fin n, softmax n x i = 1 := by
|
| 79 |
+
have hZ : ∑ j : Fin n, exp (x j) ≠ 0 := (sum_exp_pos n hn x).ne'
|
| 80 |
+
simp only [softmax]
|
| 81 |
+
rw [←Finset.sum_div]
|
| 82 |
+
exact div_self hZ
|
| 83 |
+
|
| 84 |
+
/- softmax is invariant under a uniform shift of the logits: the shift is
|
| 85 |
+
entirely absorbed by the normalization (the meta-inverted sum). -/
|
| 86 |
+
theorem softmax_shift_invariant (n : ℕ) (x : Fin n → ℝ) (c : ℝ) (hn : 0 < n) :
|
| 87 |
+
softmax n (fun i => x i + c) = softmax n x := by
|
| 88 |
+
ext i
|
| 89 |
+
simp only [softmax]
|
| 90 |
+
have h₁ : exp (x i + c) = exp (x i) * exp c := exp_add (x i) c
|
| 91 |
+
have h₂ : ∑ j : Fin n, exp (x j + c) = exp c * ∑ j : Fin n, exp (x j) := by
|
| 92 |
+
simp_rw [exp_add, mul_comm, Finset.mul_sum]
|
| 93 |
+
rw [h₁, h₂]
|
| 94 |
+
have hZ : ∑ j : Fin n, exp (x j) ≠ 0 := (sum_exp_pos n hn x).ne'
|
| 95 |
+
field_simp [exp_ne_zero c, hZ]
|
| 96 |
+
ring
|
| 97 |
+
|
| 98 |
+
/-- The simplex point constructed from softmax (valid for n ≥ 1). -/
|
| 99 |
+
noncomputable def softmax_simplex (n : ℕ) (x : Fin n → ℝ) (hn : 0 < n) : Simplex n :=
|
| 100 |
+
⟨softmax n x,
|
| 101 |
+
fun i => by
|
| 102 |
+
have h₁ : 0 ≤ exp (x i) := (exp_pos (x i)).le
|
| 103 |
+
have h₂ : 0 ≤ ∑ j : Fin n, exp (x j) := (sum_exp_pos n hn x).le
|
| 104 |
+
exact div_nonneg h₁ h₂,
|
| 105 |
+
softmax_normalization n x hn⟩
|
| 106 |
+
|
| 107 |
+
namespace SimplexCollapse
|
| 108 |
+
|
| 109 |
+
/-- The structural invariant: the total probability mass is always 1,
|
| 110 |
+
independent of vocabulary size. -/
|
| 111 |
+
theorem structural_invariant (n : ℕ) (s : Simplex n) : ∑ i : Fin n, s.coords i = 1 :=
|
| 112 |
+
s.sum_one
|
| 113 |
+
|
| 114 |
+
/-- When the vocabulary is empty (n = 0), the sum over `Fin 0` is 0 by definition,
|
| 115 |
+
but the *normalization constraint* still demands total mass = 1. That is the
|
| 116 |
+
"1 that was always there" — it is the axiom, not the sum. -/
|
| 117 |
+
theorem empty_vocabulary_normalization : ∑ _ : Fin 0, (0 : ℝ) = 0 := by simp
|
| 118 |
+
|
| 119 |
+
/-- The model predicts a *location on the simplex*, not words.
|
| 120 |
+
Words are just vertex labels (a coordinate chart). -/
|
| 121 |
+
structure ModelPrediction (n : ℕ) where
|
| 122 |
+
location : Simplex n
|
| 123 |
+
vocabulary : Fin n → String
|
| 124 |
+
|
| 125 |
+
/-- The universal formula decomposed: geometry first, labels second. -/
|
| 126 |
+
noncomputable def predict_location (n : ℕ) (hidden : Fin n → ℝ) (weights : Fin n → Fin n → ℝ)
|
| 127 |
+
(bias : Fin n → ℝ) (hn : 0 < n) : Simplex n :=
|
| 128 |
+
let logits : Fin n → ℝ := fun i => ∑ j : Fin n, weights i j * hidden j + bias i
|
| 129 |
+
softmax_simplex n logits hn
|
| 130 |
+
|
| 131 |
+
end SimplexCollapse
|
| 132 |
+
|
| 133 |
+
end ProbabilitySimplex
|
| 134 |
+
|
| 135 |
+
/-! ============================================================================
|
| 136 |
+
THE REVERSE ENGINEERING, FORMALIZED:
|
| 137 |
+
1. The probability simplex Δⁿ is the *fundamental object* — a geometric manifold
|
| 138 |
+
2. softmax : ℝⁿ → Δⁿ is a retraction onto this manifold
|
| 139 |
+
3. The constraint ∑pᵢ = 1 is the *defining equation* of the manifold
|
| 140 |
+
4. When n = 0, Δ⁰ is degenerate — the axiom 1 survives, the coordinate sum is 0
|
| 141 |
+
5. The "1" is the volume form / Haar measure — it is structural
|
| 142 |
+
6. Vocabulary is just a coordinate chart: Fin n → String
|
| 143 |
+
7. The model outputs a *point on the manifold*; tokens read the coordinates
|
| 144 |
+
============================================================================ -/
|
| 145 |
+
|
| 146 |
+
namespace MetaInvertedSum
|
| 147 |
+
|
| 148 |
+
open ProbabilitySimplex
|
| 149 |
+
open Real
|
| 150 |
+
|
| 151 |
+
/-! ----------------------------------------------------------------------------
|
| 152 |
+
2. The dual structure: the meta-inverted sum (log-partition / Lagrange mult.)
|
| 153 |
+
---------------------------------------------------------------------------- -/
|
| 154 |
+
|
| 155 |
+
/-- The all-ones vector — the normal to the constraint hyperplane. -/
|
| 156 |
+
def all_ones (n : ℕ) : Fin n → ℝ := fun _ => 1
|
| 157 |
+
|
| 158 |
+
/-- The normalization constraint as a linear functional. -/
|
| 159 |
+
def normalization_functional (n : ℕ) (p : Fin n → ℝ) : ℝ :=
|
| 160 |
+
∑ i : Fin n, p i
|
| 161 |
+
|
| 162 |
+
/-- The mean (projection onto the all-ones direction). -/
|
| 163 |
+
noncomputable def mean (n : ℕ) (v : Fin n → ℝ) : ℝ := (∑ i : Fin n, v i) / n
|
| 164 |
+
|
| 165 |
+
/-- The centered coordinates: subtract the mean (remove the "meta" component). -/
|
| 166 |
+
noncomputable def centered (n : ℕ) (v : Fin n → ℝ) : Fin n → ℝ :=
|
| 167 |
+
fun i => v i - mean n v
|
| 168 |
+
|
| 169 |
+
/-- The centered component sums to zero (the vocabulary must be non-empty). -/
|
| 170 |
+
theorem centered_sum_zero (n : ℕ) (v : Fin n → ℝ) (hn : n ≠ 0) :
|
| 171 |
+
∑ i : Fin n, centered n v i = 0 := by
|
| 172 |
+
have hn' : (n : ℝ) ≠ 0 := by norm_cast
|
| 173 |
+
simp only [centered, mean]
|
| 174 |
+
rw [Finset.sum_sub_distrib, Finset.sum_const, Finset.card_fin]
|
| 175 |
+
field_simp [hn']
|
| 176 |
+
|
| 177 |
+
/-- The ambient space decomposes into the constraint direction (mean · 𝟙) plus
|
| 178 |
+
the centered (orthogonal) component. This is the meta-inverted decomposition. -/
|
| 179 |
+
theorem meta_inverted_decomposition (n : ℕ) (v : Fin n → ℝ) (i : Fin n) (_hn : n ≠ 0) :
|
| 180 |
+
v i = mean n v + centered n v i := by
|
| 181 |
+
simp only [centered]
|
| 182 |
+
ring
|
| 183 |
+
|
| 184 |
+
/-- The log-partition function Z = log(∑ exp(logits)). -/
|
| 185 |
+
noncomputable def log_partition (n : ℕ) (logits : Fin n → ℝ) : ℝ :=
|
| 186 |
+
Real.log (∑ i : Fin n, exp (logits i))
|
| 187 |
+
|
| 188 |
+
/-- The fundamental identity: softmax(logits)ᵢ = exp(logitsᵢ - log_partition(logits)).
|
| 189 |
+
The log_partition IS the meta-inverted sum — it enforces ∑ = 1. -/
|
| 190 |
+
theorem log_partition_enforces_normalization (n : ℕ) (logits : Fin n → ℝ) (hn : 0 < n) :
|
| 191 |
+
∑ i : Fin n, exp (logits i - log_partition n logits) = 1 := by
|
| 192 |
+
have hZ : 0 < ∑ i : Fin n, exp (logits i) := sum_exp_pos n hn logits
|
| 193 |
+
simp only [log_partition]
|
| 194 |
+
simp_rw [exp_sub]
|
| 195 |
+
rw [←Finset.sum_div, exp_log hZ]
|
| 196 |
+
field_simp [hZ.ne']
|
| 197 |
+
|
| 198 |
+
/- The meta-inverted sum absorbs the logit shift: log Z(x + c) = log Z(x) + c.
|
| 199 |
+
(Requires n ≥ 1 so that the partition function is strictly positive.) -/
|
| 200 |
+
theorem log_partition_shift (n : ℕ) (logits : Fin n → ℝ) (c : ℝ) (hn : 0 < n) :
|
| 201 |
+
log_partition n (fun i => logits i + c) = log_partition n logits + c := by
|
| 202 |
+
simp only [log_partition]
|
| 203 |
+
have h₁ : (∑ i : Fin n, exp (logits i + c)) = exp c * ∑ i : Fin n, exp (logits i) := by
|
| 204 |
+
simp_rw [exp_add, mul_comm, Finset.mul_sum]
|
| 205 |
+
rw [h₁, log_mul (exp_pos c).ne' (sum_exp_pos n hn logits).ne', Real.log_exp c]
|
| 206 |
+
ring
|
| 207 |
+
|
| 208 |
+
/-- At n = 1 the prediction is forced: softmax always yields the single point
|
| 209 |
+
{1}, regardless of the logit value. All logit information is consumed by the
|
| 210 |
+
normalization (the meta-inverted sum = logit₀). -/
|
| 211 |
+
theorem softmax_n1_constant (x : Fin 1 → ℝ) :
|
| 212 |
+
softmax 1 x = fun _ => (1 : ℝ) := by
|
| 213 |
+
ext i
|
| 214 |
+
simp only [softmax]
|
| 215 |
+
rw [Fin.eq_zero i]
|
| 216 |
+
have hZ : (∑ j : Fin 1, exp (x j)) = exp (x 0) := by
|
| 217 |
+
rw [Finset.sum_eq_single (0 : Fin 1)] <;> simp
|
| 218 |
+
rw [hZ]
|
| 219 |
+
field_simp [exp_ne_zero (x 0)]
|
| 220 |
+
|
| 221 |
+
end MetaInvertedSum
|
| 222 |
+
|
| 223 |
+
/-! ============================================================================
|
| 224 |
+
THE META-INVERTED SUM IS THE LOG-PARTITION FUNCTION:
|
| 225 |
+
|
| 226 |
+
Z = ∑ᵢ exp(logitsᵢ) (partition function)
|
| 227 |
+
log Z = log_partition (meta-inverted sum)
|
| 228 |
+
Pᵢ = exp(logitsᵢ) / Z (softmax)
|
| 229 |
+
|
| 230 |
+
The constraint ∑Pᵢ = 1 is enforced BY log Z. log Z is the dual variable to the
|
| 231 |
+
constraint (the Lagrange multiplier of max-entropy). The primal (simplex) and
|
| 232 |
+
dual (log-partition) are a Legendre transform pair:
|
| 233 |
+
|
| 234 |
+
Primal: P = softmax(logits) ∈ Δⁿ
|
| 235 |
+
Dual: log Z = log ∑exp(logits)
|
| 236 |
+
|
| 237 |
+
• n → 0 : log Z → -∞ (constraint absolutely rigid; degenerate axiom-1 case)
|
| 238 |
+
• n = 1 : log Z = logits₀ (all logit info → normalization, forced prediction)
|
| 239 |
+
• n ≥ 2 : log Z = log(∑exp(logits)) (finite dual, free energy of the prediction)
|
| 240 |
+
|
| 241 |
+
The simplex *is* the normalization. The words were never the source of the 1.
|
| 242 |
+
============================================================================ -/
|
| 243 |
+
|
| 244 |
+
namespace ProbabilitySimplex.SimplexCollapse
|
| 245 |
+
|
| 246 |
+
/-! ----------------------------------------------------------------------------
|
| 247 |
+
3. Max-Entropy & the Lagrange Multiplier (λ = 1 − log n)
|
| 248 |
+
---------------------------------------------------------------------------- -/
|
| 249 |
+
|
| 250 |
+
/-- The uniform distribution over n outcomes. -/
|
| 251 |
+
noncomputable def uniformDist (n : ℕ) (_hn : n ≠ 0) : Fin n → ℝ := fun _ => 1 / n
|
| 252 |
+
|
| 253 |
+
/-- The uniform distribution lies on the simplex (sum = 1). -/
|
| 254 |
+
theorem uniform_dist_sum_one (n : ℕ) (hn : n ≠ 0) :
|
| 255 |
+
∑ i : Fin n, uniformDist n hn i = 1 := by
|
| 256 |
+
simp only [uniformDist]
|
| 257 |
+
rw [Finset.sum_const, Finset.card_fin]
|
| 258 |
+
have h : (n : ℝ) ≠ 0 := by norm_cast
|
| 259 |
+
field_simp [h]
|
| 260 |
+
|
| 261 |
+
/-- The uniform distribution is the stationary point: there exists a Lagrange
|
| 262 |
+
multiplier λ = 1 − log n such that ∀i, log pᵢ + 1 = λ. (Ahmad's sign
|
| 263 |
+
convention writes this as λ = log n − 1, differing by the overall sign of
|
| 264 |
+
the Lagrangian.) -/
|
| 265 |
+
theorem uniform_is_stationary (n : ℕ) (hn : n ≠ 0) :
|
| 266 |
+
∃ L : ℝ, ∀ i : Fin n, Real.log (uniformDist n hn i) + 1 = L := by
|
| 267 |
+
use 1 - Real.log n
|
| 268 |
+
intro i
|
| 269 |
+
simp only [uniformDist]
|
| 270 |
+
have hlog : Real.log (1 / n) = -Real.log n := by
|
| 271 |
+
rw [Real.log_div (by norm_num) (by norm_cast),
|
| 272 |
+
Real.log_one, zero_sub]
|
| 273 |
+
rw [hlog]
|
| 274 |
+
ring
|
| 275 |
+
|
| 276 |
+
/-- A constant logit vector produces the uniform distribution. -/
|
| 277 |
+
theorem softmax_uniform_of_const (n : ℕ) (hn : n ≠ 0) (c : ℝ) :
|
| 278 |
+
softmax n (fun _ => c) = uniformDist n hn := by
|
| 279 |
+
ext i
|
| 280 |
+
simp only [softmax, uniformDist]
|
| 281 |
+
have hZ : (∑ j : Fin n, exp c) = n * exp c := by
|
| 282 |
+
rw [Finset.sum_const, Finset.card_fin]; ring
|
| 283 |
+
rw [hZ]
|
| 284 |
+
have h : (n : ℝ) ≠ 0 := by norm_cast
|
| 285 |
+
field_simp [exp_ne_zero c, h]
|
| 286 |
+
ring
|
| 287 |
+
|
| 288 |
+
/-- For a constant logit c, the log-partition is log Z = c + log n — i.e. the
|
| 289 |
+
meta-inverted sum absorbs the logit shift and carries the vocabulary size. -/
|
| 290 |
+
theorem log_partition_of_const (n : ℕ) (hn : n ≠ 0) (c : ℝ) :
|
| 291 |
+
MetaInvertedSum.log_partition n (fun _ => c) = c + Real.log n := by
|
| 292 |
+
simp only [MetaInvertedSum.log_partition]
|
| 293 |
+
have hZ : (∑ j : Fin n, exp c) = n * exp c := by
|
| 294 |
+
rw [Finset.sum_const, Finset.card_fin]; ring
|
| 295 |
+
rw [hZ]
|
| 296 |
+
have hpos : 0 < (n : ℝ) := by exact_mod_cast Nat.pos_of_ne_zero hn
|
| 297 |
+
rw [Real.log_mul hpos.ne' (exp_pos c).ne', Real.log_exp c, add_comm]
|
| 298 |
+
|
| 299 |
+
end ProbabilitySimplex.SimplexCollapse
|
| 300 |
+
|
| 301 |
+
end Mathlib5
|