The neural network class and widths
DefinitionDef_CAH21_NeuralNetworksThe class of neural networks (SI Appendix, §1.2.1) and the widths of Theorem 3.
A neural network is a composition
where each is affine in the current vector and in the input (skip connections), and each is of one of two forms:
- there is an index set and a function with for and otherwise;
- there is a function such that, writing with , (eq. (1.13)).
lists the layer widths, the number of affine layers and the number of distinct non-linear maps (counting different and ). Theorem 3 uses
Formalization Note The class is the predicate IsNeuralNet φ D T q (with "at most " distinct maps, where two layers are the same map iff their form, function and index data agree). The functions are arbitrary; the paper additionally builds them from arithmetic operations and an approximate square root, which is not modeled here.
import Mathlib
/-!
Colbrook–Antun–Hansen (arXiv:2101.08286v2), SI Appendix §1.2.1: the class
`𝒩_{D,T,q}` of (complex-valued, feed-forward) neural networks `φ : ℂ^m → ℂ^N`,
`φ(y) = V_T(ρ_{T-1}(⋯ρ_1(V_1(y))))`, where each `V_j(x) = W_j x + R_j y + c_j` is affine
in the current vector `x` and in the input `y` (skip connections), and each `ρ_j` is a
non-linear layer of one of the two forms (i) (a function `f_j` applied entrywise on an
index set `I_j`) or (ii) (the gating map `(x₀, X, Y) ↦ (0, f_j(x₀) X, Y)` with `X ∈ ℂ^{m_j}`),
eq. (1.13). Also the layer-width vector `D_{(n,p)}` of Theorem 3.
-/
namespace ColbrookAntunHansen
/-- A non-linear layer `ρ : ℂ^d → ℂ^d` of form (i) or (ii) (SI §1.2.1). -/
inductive NonlinLayer (d : ℕ) where
/-- Form (i): apply `f` to the entries with index in `I`, leave the others unchanged. -/
| pointwise (I : Finset (Fin d)) (f : ℂ → ℂ)
/-- Form (ii): write `x = (x₀, X, Y)` with `X ∈ ℂ^{k}`; map it to `(0, f(x₀) X, Y)`. -/
| gated (k : ℕ) (hk : k + 1 ≤ d) (f : ℂ → ℂ)
namespace NonlinLayer
/-- The action of a non-linear layer on a vector of `ℂ^d`. -/
def apply {d : ℕ} : NonlinLayer d → (Fin d → ℂ) → (Fin d → ℂ)
| pointwise I f, x => fun i => if i ∈ I then f (x i) else x i
| gated k hk f, x => fun i =>
if i.val = 0 then 0
else if i.val ≤ k then f (x ⟨0, by omega⟩) * x i
else x i
/-- The data identifying a non-linear layer up to its width: its form, its function `f_j`
and its index data (`I_j` for form (i), `m_j` for form (ii)). Two layers are counted
as the same non-linear map in `q` iff their keys agree. -/
def key {d : ℕ} : NonlinLayer d → Bool × (ℂ → ℂ) × Set ℕ × ℕ
| pointwise I f => (false, f, (fun i : Fin d => i.val) '' (I : Set (Fin d)), 0)
| gated k _ f => (true, f, ∅, k)
end NonlinLayer
/-- The hidden vectors of a network with widths `D`: `hidden 0 = V_1(y)` and
`hidden (j+1) = V_{j+2}(ρ_{j+1}(hidden j))`. -/
noncomputable def hiddenVec {m : ℕ} (D : ℕ → ℕ)
(W₀ : Matrix (Fin (D 1)) (Fin m) ℂ)
(W : (j : ℕ) → Matrix (Fin (D (j + 2))) (Fin (D (j + 1))) ℂ)
(R : (j : ℕ) → Matrix (Fin (D (j + 1))) (Fin m) ℂ)
(c : (j : ℕ) → Fin (D (j + 1)) → ℂ)
(σ : (j : ℕ) → NonlinLayer (D (j + 1)))
(y : Fin m → ℂ) : (j : ℕ) → Fin (D (j + 1)) → ℂ
| 0 => W₀.mulVec y + (R 0).mulVec y + c 0
| j + 1 => (W j).mulVec ((σ j).apply (hiddenVec D W₀ W R c σ y j)) + (R (j + 1)).mulVec y + c (j + 1)
/-- `φ ∈ 𝒩_{D,T,q}`: `φ : ℂ^m → ℂ^N` is a neural network with `T ≥ 1` affine layers,
widths `D = (D_0 = m, D_1, …, D_T = N)` and at most `q` different non-linear maps
(counted with their index data `I_j` / `m_j`). The non-linear layers are
`ρ_j = σ (j-1)` for `j = 1, …, T-1`. -/
def IsNeuralNet {m N : ℕ} (φ : (Fin m → ℂ) → (Fin N → ℂ)) (D : ℕ → ℕ) (T q : ℕ) : Prop :=
1 ≤ T ∧ D 0 = m ∧
∃ (hD : D (T - 1 + 1) = N)
(W₀ : Matrix (Fin (D 1)) (Fin m) ℂ)
(W : (j : ℕ) → Matrix (Fin (D (j + 2))) (Fin (D (j + 1))) ℂ)
(R : (j : ℕ) → Matrix (Fin (D (j + 1))) (Fin m) ℂ)
(c : (j : ℕ) → Fin (D (j + 1)) → ℂ)
(σ : (j : ℕ) → NonlinLayer (D (j + 1))),
((fun j => (σ j).key) '' {j | j < T - 1}).ncard ≤ q ∧
∀ y i, φ y i = hiddenVec D W₀ W R c σ y (T - 1) (Fin.cast hD.symm i)
/-- The width vector `D_{(n,p)} = (m, 2N+m, 2(N+m), 2N+m+1, …, 2N+m, 2(N+m), 2N+m+1, N)`
of Theorem 3, in which the block `(2N+m, 2(N+m), 2N+m+1)` is repeated `n p` times;
entry `j` for `j = 0, …, 3np+1`. -/
def firenetDims (m N n p : ℕ) (j : ℕ) : ℕ :=
if j = 0 then m
else if j = 3 * n * p + 1 then N
else if (j - 1) % 3 = 0 then 2 * N + m
else if (j - 1) % 3 = 1 then 2 * (N + m)
else 2 * N + m + 1
end ColbrookAntunHansen
Read-back
What the Lean code literally says, in plain math · Aristotle (Harmonic) — same agent that drafted the statements (non-blind)
Non-blind read-back. This read-back was written by the same agent (Aristotle, by Harmonic) that drafted the Lean statements, at the proposal owner's explicit request. It is not independent, blind testimony: its author knew the source paper and the intended meaning while writing it. Reviewers should treat it as the drafter's own reading of the code, not as an independent audit, and should compare the Lean code against the source themselves.
This file defines a class of maps ("neural networks") and a width sequence.
Non-linear layers. For a width , a non-linear layer is one of:
- pointwise: a finite set and an arbitrary function ; it maps to the vector whose entry is if and otherwise;
- gated: a natural number with and an arbitrary ; it maps to the vector with entry equal to , entry equal to for , and entry equal to for .
No regularity (continuity, computability, measurability) is imposed on . Each layer has a key: for a pointwise layer, (pointwise, , the set viewed as a set of natural numbers, ); for a gated layer, (gated, , , ). Two layers count as "the same non-linear map" iff their keys are equal (so equal functions with different index sets or different count as different).
Network evaluation. Given a width sequence , a first matrix , matrices , skip matrices , bias vectors and non-linear layers of width (all for ), and an input , the hidden vectors are
The class. is a network with widths , affine layers and at most distinct non-linear maps iff: ; ; ; and there exist as above such that the set of keys has at most elements and, for every , (entries identified via ). Thus is the composition of affine maps (each also depending affinely on the original input ) with non-linear layers in between. Only matter. When the network is the single affine map .
Widths . For and : ; ; otherwise if , if , and if . So with the triple repeated times. (If the rule gives , . Entries beyond follow the periodic rule and are irrelevant.)