ML.Neural­Net

Roll-up for dense affine layers, activations carrying Lipschitz constants, and uniform-width feedforward network evaluation.

Layer 4 core · 0 supporting A dense (affine) layer x ↦ W x + b and an activation function carrying its Lipschitz constant. ★ DenseLayer★ Activation

Neural-network layers

A dense (affine) layer x ↦ W x + b and an activation function carrying its Lipschitz constant. These are the building blocks composed in NeuralNet/FeedForward.lean.

structure DenseLayer reviewed
Causalean.ML

A dense affine layer bundles a weight matrix and a bias vector determining the map xWx+bx \mapsto Wx + b from Fin m inputs to Fin n outputs.

Definition (Lean source)
m n :
The weight matrix.
W :
Matrix (Fin n) (Fin m) ℝ
The bias vector.
b :
Fin n → ℝ
Causalean.ML.DenseLayer · Causalean/ML/NeuralNet/Layer.lean:21
def eval reviewed
Causalean.ML.DenseLayer

For an input dimension, an output dimension, a dense affine layer, and an input vector, the layer evaluation is the output vector whose jjth coordinate is the weighted sum of the input coordinates plus the jjth bias.

Definition (Lean source)
m n :
L :
x :
Fin m → ℝ
eval L x :
Fin n → ℝ
fun j => (L.W *ᵥ x) j + L.b j
Causalean.ML.DenseLayer.eval · Causalean/ML/NeuralNet/Layer.lean:29 · uses DenseLayer
structure Activation reviewed
Causalean.ML

An activation function bundles a scalar map together with a Lipschitz constant and the certificate that the map is Lipschitz with that constant (e.g. ReLU, sigmoid, and tanh are all 1-Lipschitz).

Definition (Lean source)
The scalar activation.
act :
ℝ → ℝ
A Lipschitz constant for the activation.
lip :
Proof that `act` is `lip`-Lipschitz.
isLipschitz :
Causalean.ML.Activation · Causalean/ML/NeuralNet/Layer.lean:33
def applyVec reviewed
Causalean.ML.Activation

For a vector dimension, an activation-function bundle, and an input vector, the coordinatewise activation is the vector obtained by applying the bundle's scalar activation to each coordinate of the input.

Definition (Lean source)
n :
σ :
x :
Fin n → ℝ
applyVec σ x :
Fin n → ℝ
fun j => σ.act (x j)
Causalean.ML.Activation.applyVec · Causalean/ML/NeuralNet/Layer.lean:44 · uses Activation
Feed­Forward 3 core · 1 supporting A uniform-width feedforward network is a list of dense layers evaluated left-to-right with an activation after each affine map. ★ evalLayers_lipschitz

Feedforward networks (uniform width)

A uniform-width feedforward network is a list of dense layers evaluated left-to-right with an activation after each affine map. Structural facts: the evaluation respects layer concatenation (composition), and the network is Lipschitz with constant the product of the per-layer Lipschitz constants. Universal approximation and training dynamics are out of scope.

def layerMap reviewed
Causalean.ML

For a nonnegative network width, an activation function, a dense layer with that finite input and output width, and an input vector of that width, the one-layer network map applies the layer's affine transformation and then applies the activation function to each coordinate.

Definition (Lean source)
n :
σ :
L :
x :
Fin n → ℝ
layerMap σ L x :
Fin n → ℝ
σ.applyVec (L.eval x)
def evalLayers reviewed
Causalean.ML

For a nonnegative network width and an activation function, the evaluation of a uniform-width feedforward network maps a finite ordered list of equal-width dense layers and an input vector to its output vector. For an empty list, the output is the input itself; for a list whose first layer is followed by further layers, the output applies the first layer and then evaluates the remaining layers.

Definition (Lean source)
n :
σ :
evalLayers σ :
List (DenseLayer n n) → (Fin n → ℝ) → (Fin n → ℝ)
clause 1
| [], x => x
clause 2
| L :: Ls, x => evalLayers σ Ls (layerMap σ L x)
theorem evalLayers_lipschitz reviewed
Causalean.ML

Structure — Lipschitz. For a uniform-width feedforward network with activation σ and layer list Ls, if each layer's affine-then-activation map is Lipschitz with the constant assigned to it by k, then the whole network evaluation is Lipschitz with constant equal to the product of the per-layer constants.

Formal statement
n :
σ :
Ls :
k :
hk :
∀ L ∈ Ls, LipschitzWith (k L) (layerMap σ L)
LipschitzWith (Ls.map k).prod (evalLayers σ Ls)
Proof (Lean source)
theorem evalLayers_lipschitz {n : ℕ} (σ : Activation) (Ls : List (DenseLayer n n)) (k : DenseLayer n n → NNReal) (hk : ∀ L ∈ Ls, LipschitzWith (k L) (layerMap σ L)) : LipschitzWith (Ls.map k).prod (evalLayers σ Ls) := by induction Ls with | nil => -- `simp` no longer unfolds `id`, so state the identity bound in the -- lambda form the goal uses. have hid : LipschitzWith (1 : NNReal) (fun x : Fin n → ℝ => x) := LipschitzWith.id simpa [evalLayers] using hid | cons L Ls ih => have hL : LipschitzWith (k L) (layerMap σ L) := hk L (by simp) have hLs : ∀ L' ∈ Ls, LipschitzWith (k L') (layerMap σ L') := by intro L' hL' exact hk L' (by simp [hL']) simpa [evalLayers, map_cons, List.prod_cons, mul_comm, Function.comp_def] using (ih hLs).comp hL
1 supporting declaration (lemmas, instances)