Fully connected networks with Gaussian initialization and the NNGP kernel
DefinitionLesHouchesWidth_GaussianMLPFully connected networks with Gaussian initialization, shared by the whole series.
Fix a depth and widths (a sequence ), constants and a function . In the notation of Lectures 1–3, , and .
- Parameters. One standard Gaussian coordinate per bias and per weight, all independent (
stdGaussianParams). The biases are and the weights are . Thus and , independent (eq. (119)). - Preactivations (eq. (118)): and for (
mlpZ; layer is the input). - Gaussian pair average (eq. (15)): for (
gaussPairAvg). - NNGP kernel (eqs. (13)–(14), (120)): and (
nngpKernel). - Uniform widths and output vectors.
uniformWidths n0 nOut L Nhas input width , output width and all hidden widths equal to .outputVectoris the vector on finitely many inputs.blockDiagCovis the covariance . - Pairings.
pairings Nis the set of perfect matchings of , encoded as fixed-point-free involutions.
Formalization Note Layers are indexed as in Lectures 4–5, so and of Lecture 1 are and here. uses Mathlib's multivariateGaussian, which is a point mass when the matrix is not positive semidefinite.
import Mathlib
/-!
# Fully connected networks with Gaussian initialization (Les Houches lectures)
Shared definitions for the series formalizing Bahri–Hanin–Brossollet–Erba–Keup–Pacelli–Simon,
*Les Houches Lectures on Deep Learning at Large & Infinite Width*, arXiv:2309.01592v3.
Indexing follows Lectures 4–5 (Section 4.2, eqs. (118)–(119)): widths `n 0, …, n (L+1)`,
preactivations `z^{(1)}, …, z^{(L+1)}`, and
`z^{(1)} = b^{(1)} + W^{(1)} x`, `z^{(ℓ+1)} = b^{(ℓ+1)} + W^{(ℓ+1)} σ(z^{(ℓ)})` for `ℓ ≥ 1`,
with independent `b^{(ℓ)}_i ∼ N(0, C_b)` and `W^{(ℓ)}_{ij} ∼ N(0, C_W / n_{ℓ-1})`.
(In the notation of Lectures 1–3, `z^l = z^{(l+1)}`, `σ_b^2 = C_b`, `σ_w^2 = C_W`, `φ = σ`.)
-/
namespace LesHouchesWidth
open MeasureTheory ProbabilityTheory
/-- Index set of the parameters of a network of depth `L` with widths `n 0, …, n (L+1)`:
`inl ⟨ℓ, i⟩` is the bias `b^{(ℓ+1)}_i` and `inr ⟨ℓ, (i, j)⟩` is the weight `W^{(ℓ+1)}_{ij}`
(`ℓ ≤ L`, `i < n (ℓ+1)`, `j < n ℓ`). -/
abbrev ParamIndex (n : ℕ → ℕ) (L : ℕ) : Type :=
(Σ ℓ : Fin (L + 1), Fin (n (ℓ.val + 1))) ⊕
(Σ ℓ : Fin (L + 1), Fin (n (ℓ.val + 1)) × Fin (n ℓ.val))
/-- A configuration of standardized parameters (one real number per bias and weight). -/
abbrev Params (n : ℕ → ℕ) (L : ℕ) : Type := ParamIndex n L → ℝ
/-- The law of the standardized parameters: i.i.d. standard Gaussians `N(0, 1)`. -/
noncomputable def stdGaussianParams (n : ℕ → ℕ) (L : ℕ) : Measure (Params n L) :=
Measure.pi fun _ => gaussianReal 0 1
instance (n : ℕ → ℕ) (L : ℕ) : IsProbabilityMeasure (stdGaussianParams n L) := by
unfold stdGaussianParams; infer_instance
/-- The bias `b^{(ℓ+1)}_i = C_b^{1/2} θ_{b,ℓ,i}` (so `b^{(ℓ+1)}_i ∼ N(0, C_b)`; `0` if `ℓ > L`). -/
noncomputable def mlpBias {n : ℕ → ℕ} {L : ℕ} (Cb : ℝ) (θ : Params n L) (ℓ : ℕ)
(i : Fin (n (ℓ + 1))) : ℝ :=
if h : ℓ < L + 1 then Real.sqrt Cb * θ (Sum.inl ⟨⟨ℓ, h⟩, i⟩) else 0
/-- The weight `W^{(ℓ+1)}_{ij} = (C_W / n_ℓ)^{1/2} θ_{W,ℓ,i,j}`
(so `W^{(ℓ+1)}_{ij} ∼ N(0, C_W / n_ℓ)`; `0` if `ℓ > L`). -/
noncomputable def mlpWeight {n : ℕ → ℕ} {L : ℕ} (CW : ℝ) (θ : Params n L) (ℓ : ℕ)
(i : Fin (n (ℓ + 1))) (j : Fin (n ℓ)) : ℝ :=
if h : ℓ < L + 1 then Real.sqrt (CW / (n ℓ : ℝ)) * θ (Sum.inr ⟨⟨ℓ, h⟩, (i, j)⟩) else 0
/-- Preactivations in the indexing of Lectures 4–5: `mlpZ … 0 = x` (the input) and, for
`ℓ ≥ 0`, `mlpZ … (ℓ+1) = z^{(ℓ+1)}(x)`, where
`z^{(1)}_i = b^{(1)}_i + ∑_j W^{(1)}_{ij} x_j` and
`z^{(ℓ+1)}_i = b^{(ℓ+1)}_i + ∑_j W^{(ℓ+1)}_{ij} σ(z^{(ℓ)}_j)` for `ℓ ≥ 1` (eq. (118)). -/
noncomputable def mlpZ {n : ℕ → ℕ} {L : ℕ} (Cb CW : ℝ) (σ : ℝ → ℝ) (θ : Params n L)
(x : Fin (n 0) → ℝ) : (ℓ : ℕ) → Fin (n ℓ) → ℝ
| 0 => x
| ℓ + 1 => fun i => mlpBias Cb θ ℓ i +
∑ j, mlpWeight CW θ ℓ i j * (if ℓ = 0 then mlpZ Cb CW σ θ x ℓ j else σ (mlpZ Cb CW σ θ x ℓ j))
/-- `F_σ(Σ₁₁, Σ₁₂, Σ₂₂) = E[σ(u₁) σ(u₂)]` for `(u₁, u₂) ∼ N(0, Σ)` with
`Σ = [[Σ₁₁, Σ₁₂], [Σ₁₂, Σ₂₂]]` (eq. (15)). -/
noncomputable def gaussPairAvg (σ : ℝ → ℝ) (s11 s12 s22 : ℝ) : ℝ :=
∫ u, σ (u 0) * σ (u 1) ∂(multivariateGaussian 0 !![s11, s12; s12, s22])
/-- The infinite-width (NNGP) kernel `K^{(ℓ)}(x, x')` of eq. (120) (equivalently eqs. (13)–(14)):
`K^{(1)}(x, x') = C_b + C_W (x · x') / n_0` and
`K^{(ℓ+1)}(x, x') = C_b + C_W F_σ(K^{(ℓ)}(x, x), K^{(ℓ)}(x, x'), K^{(ℓ)}(x', x'))` for `ℓ ≥ 1`.
The value at `ℓ = 0` is a placeholder `0` and is never used. -/
noncomputable def nngpKernel (Cb CW : ℝ) (σ : ℝ → ℝ) {n0 : ℕ} :
ℕ → (Fin n0 → ℝ) → (Fin n0 → ℝ) → ℝ
| 0, _, _ => 0
| 1, x, x' => Cb + CW * (∑ j, x j * x' j) / (n0 : ℝ)
| ℓ + 2, x, x' => Cb + CW * gaussPairAvg σ (nngpKernel Cb CW σ (ℓ + 1) x x)
(nngpKernel Cb CW σ (ℓ + 1) x x') (nngpKernel Cb CW σ (ℓ + 1) x' x')
/-- Widths of a network of depth `L` whose hidden layers `1, …, L` all have width `N`, with input
width `n0` and output width `nOut`. -/
def uniformWidths (n0 nOut L N : ℕ) : ℕ → ℕ :=
fun ℓ => if ℓ = 0 then n0 else if ℓ = L + 1 then nOut else N
theorem uniformWidths_last (n0 nOut L N : ℕ) : uniformWidths n0 nOut L N (L + 1) = nOut := by
simp [uniformWidths]
/-- The vector of outputs `(z^{(L+1)}_i(x_a))_{i, a}` (`i < nOut`, `a < m`) of the network on
finitely many inputs `x_1, …, x_m`, as an element of Euclidean space indexed by `(i, a)`;
`h : n (L+1) = nOut` identifies the output layer with `Fin nOut`. -/
noncomputable def outputVector {n : ℕ → ℕ} {L m nOut : ℕ} (Cb CW : ℝ) (σ : ℝ → ℝ)
(h : n (L + 1) = nOut) (xs : Fin m → Fin (n 0) → ℝ) (θ : Params n L) :
EuclideanSpace ℝ (Fin nOut × Fin m) :=
(WithLp.equiv 2 _).symm fun ia => mlpZ Cb CW σ θ (xs ia.2) (L + 1) (Fin.cast h.symm ia.1)
/-- The limiting covariance `δ_{ij} K(x_a, x_b)` on the index set `(i, a)`. -/
def blockDiagCov {k m : ℕ} (K : Fin m → Fin m → ℝ) : Matrix (Fin k × Fin m) (Fin k × Fin m) ℝ :=
Matrix.of fun ia jb => if ia.1 = jb.1 then K ia.2 jb.2 else 0
/-- The pairings of `{0, …, N-1}`: fixed-point-free involutions `π` (each `k` is paired with
`π k ≠ k`, and `π (π k) = k`). -/
def pairings (N : ℕ) : Finset (Equiv.Perm (Fin N)) :=
Finset.univ.filter fun π => ∀ k, π (π k) = k ∧ π k ≠ k
end LesHouchesWidth
Read-back
What the Lean code literally says, in plain math · Aristotle (Harmonic) - non-blind, same agent as drafter
Non-blind read-back. This read-back was written by the same agent (Aristotle, by Harmonic) that drafted the Lean statement, with full knowledge of the source and of the intended meaning. It is not independent testimony and should not be treated as a blind audit; an independent read-back is still recommended before launch.
The file defines, in namespace LesHouchesWidth, the following objects for a width sequence and a depth .
- Parameter index set. It is the disjoint union of bias indices , , , and weight indices , , , . A parameter configuration assigns a real number to each index.
stdGaussianParams n Lis the product of standard normal laws over all indices. It is registered as a probability measure. - Bias and weight.
mlpBias Cb θ ℓ ifor , and otherwise.mlpWeight CW θ ℓ i jfor , and otherwise.Real.sqrtof a negative number is , so a negative or silently gives zero biases or weights. If then . - Preactivations.
mlpZ Cb CW σ θ xhas layer equal to , and layer with coordinates . Here when and when . No hypothesis on is made. - Pair average.
gaussPairAvg σ s11 s12 s22is \int\sigma(u_0)\sigma(u_1)\,d\mathcal N\big(0,\begin{psmallmatrix}s_{11}&s_{12}\\s_{12}&s_{22}\end{psmallmatrix}\big)(u) on . If the matrix is not positive semidefinite, Mathlib'smultivariateGaussianis the point mass at , and the value is then . If the integrand is not integrable, the value is . - Kernel.
nngpKernel Cb CW σ ℓ x x'is for (a placeholder). For it is , where is the dimension of the inputs, and division by gives . For it is . - Uniform widths.
uniformWidths n0 nOut L N ℓis if , if , and otherwise, including for , which is never used. The lemmauniformWidths_lastrecords that the value at is . - Output vector. Given a proof that , and inputs ,
outputVector Cb CW σ h xs θis the Euclidean vector indexed by pairs , , , with entries . - Block covariance.
blockDiagCov Kis the matrix with entry at when , and otherwise. - Pairings.
pairings Nis the set of permutations of with and for all . It is empty when is odd, and for it contains only the identity.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.