TorchLean API

NN.MLTheory.Proofs.Approximation.Universal.UniversalApproximation

Universal approximation (1D, constructive) #

On a compact interval $I=[a,b]$, any Lipschitz function $f:\mathbb{R}\to\mathbb{R}$ can be uniformly approximated by a single-hidden-layer ReLU network (a 2-layer MLP).

This file formalizes the classic constructive proof strategy:

Main result #

References #

@[reducible, inline]

Shorthand for relu in this development, using TorchLean’s spec semantics.

Instances For

    If the knot $t$ is to the left of $x$, then $\operatorname{ReLU}(x-t)=x-t$.

    If $x$ is to the left of the knot $t$, then $\operatorname{ReLU}(x-t)=0$.

    Extract the unique entry of a length-1 tensor.

    Instances For
      noncomputable def NN.MLTheory.Proofs.UniversalApproximation.mlpEvalScalar (hidDim : ℕ) (l1 : Spec.LinearSpec ℝ 1 hidDim) (l2 : Spec.LinearSpec ℝ hidDim 1) (x : ℝ) :

      Evaluate a 2-layer ReLU MLP on a scalar input.

      Instances For

        TorchLean's MLP forward pass is exactly $\operatorname{linear}\circ\operatorname{ReLU}\circ\operatorname{linear}$.

        First real hinge layer: hidden unit $i$ computes $x-t_i$ before ReLU.

        Instances For

          Second real hinge layer: sum hidden activations with coefficients $c_i$ and bias $b$.

          Instances For
            noncomputable def NN.MLTheory.Proofs.UniversalApproximation.hingeFun (n : ℕ) (t c : Fin n → ℝ) (b x : ℝ) :

            Real hinge network $b+\sum_i c_i\operatorname{ReLU}(x-t_i)$.

            Instances For

              Matrix-vector multiply for a one-row matrix is the expected finite dot product.

              Matrix-vector multiply by the all-ones column extracts the scalar input into every hidden unit.

              The explicit two-layer network built from hingeLayer1 and hingeLayer2 computes hingeFun.

              This is the main semantic bridge from the approximation-theory hinge representation to TorchLean's spec-level MLP model.

              The uniform grid with left endpoint a and spacing δ.

              Instances For
                @[simp]

                The uniform grid starts at its left endpoint.

                Consecutive grid points are one mesh width apart.

                Nonnegative spacing gives an ordered grid.

                The slope between consecutive, arbitrary sample values.

                Instances For

                  Hinge coefficients are the initial slope followed by successive slope differences.

                  Instances For
                    theorem NN.MLTheory.Proofs.UniversalApproximation.Internal.UniformInterpolation.slope_mul_step {δ : ℝ} (hδ : δ ≠ 0) (y : ℕ → ℝ) (k : ℕ) :
                    slope δ y k * δ = y (k + 1) - y k

                    The slope times a nonzero mesh width is the corresponding sample increment.

                    Summing the slope differences recovers the slope of the last segment.

                    The width-N hinge interpolant of the samples y 0, …, y N.

                    Instances For
                      theorem NN.MLTheory.Proofs.UniversalApproximation.Internal.UniformInterpolation.affine_on_segment {N k : ℕ} {a δ x : ℝ} (y : ℕ → ℝ) (hδ : 0 ≤ δ) (hkN : k + 1 ≤ N) (hx0 : grid a δ k ≤ x) (hx1 : x ≤ grid a δ (k + 1)) :
                      interpolant N a δ y x = interpolant N a δ y (grid a δ k) + slope δ y k * (x - grid a δ k)

                      On each grid cell, the hinge sum is affine with the prescribed sample slope.

                      theorem NN.MLTheory.Proofs.UniversalApproximation.Internal.UniformInterpolation.interpolant_grid {N : ℕ} (a : ℝ) {δ : ℝ} (y : ℕ → ℝ) (hδ : 0 < δ) (k : ℕ) (hk : k ≤ N) :
                      interpolant N a δ y (grid a δ k) = y k

                      Positive spacing makes the hinge interpolant exact at every bounded grid point.

                      theorem NN.MLTheory.Proofs.UniversalApproximation.Internal.UniformInterpolation.hinge_fun_eq_interpolant (N : ℕ) (a δ : ℝ) (y : ℕ → ℝ) (x : ℝ) :
                      hingeFun N (fun (i : Fin N) => grid a δ ↑i) (fun (i : Fin N) => coefficient δ y ↑i) (y 0) x = interpolant N a δ y x

                      The finite-index hinge network and the range-sum interpolant have the same value.

                      theorem NN.MLTheory.Proofs.UniversalApproximation.relu_hinge_approximation_Icc_of_mesh {f : ℝ → ℝ} {a b L ε : ℝ} {N : ℕ} (h_ab : a < b) (hL : 0 < L) (h_lip : ∀ x ∈ Set.Icc a b, ∀ y ∈ Set.Icc a b, |f x - f y| ≤ L * |x - y|) (hNpos_nat : 0 < N) (hmesh : 2 * L * ((b - a) / ↑N) < ε) :
                      ∃ (t : Fin N → ℝ) (c : Fin N → ℝ), ∀ x ∈ Set.Icc a b, |f x - hingeFun N t c (f a) x| < ε

                      A uniform mesh with N cells gives a width-N hinge approximation whenever 2 * L * ((b - a) / N) is below the requested error. The same interpolant serves both the qualitative existence theorem and the theorem with an explicit width.

                      Explicit hidden width for the 1D Lipschitz ReLU approximation construction.

                      Instances For

                        The explicit ReLU approximation width is always positive.

                        The chosen width makes the mesh-size error term smaller than the target accuracy.

                        This is the arithmetic heart of the explicit-rate theorem: the ceiling construction ensures $N>2L(b-a)/\varepsilon$, hence $2L(b-a)/N<\varepsilon$.

                        theorem NN.MLTheory.Proofs.UniversalApproximation.relu_universal_approximation_Icc_hinge {f : ℝ → ℝ} {a b L : ℝ} (h_ab : a < b) (hL : 0 < L) (h_lip : ∀ x ∈ Set.Icc a b, ∀ y ∈ Set.Icc a b, |f x - f y| ≤ L * |x - y|) (ε : ℝ) :
                        ε > 0 → ∃ (hidDim : ℕ) (t : Fin hidDim → ℝ) (c : Fin hidDim → ℝ), ∀ x ∈ Set.Icc a b, |f x - hingeFun hidDim t c (f a) x| < ε

                        1D Universal Approximation (ReLU, one hidden layer).

                        This is the classic constructive proof: Lipschitz continuity on $[a,b]$, a uniform partition, and piecewise-linear interpolation, then represent the interpolant as a finite linear combination of hinges $\operatorname{ReLU}(x-t_i)$.

                        theorem NN.MLTheory.Proofs.UniversalApproximation.relu_universal_approximation_Icc {f : ℝ → ℝ} {a b L : ℝ} (h_ab : a < b) (hL : 0 < L) (h_lip : ∀ x ∈ Set.Icc a b, ∀ y ∈ Set.Icc a b, |f x - f y| ≤ L * |x - y|) (ε : ℝ) :
                        ε > 0 → ∃ (hidDim : ℕ) (l1 : Spec.LinearSpec ℝ 1 hidDim) (l2 : Spec.LinearSpec ℝ hidDim 1), ∀ x ∈ Set.Icc a b, |f x - mlpEvalScalar hidDim l1 l2 x| < ε

                        1D Universal Approximation (ReLU, one hidden layer), stated as an existence theorem for a 2-layer MLP.

                        This is a wrapper around relu_universal_approximation_Icc_hinge that instantiates the linear layers as the explicit hinge construction.