Forward pass, Lagrange function, and the sigmoid network
DefinitionLeCun1988_DefsOver real normed spaces (states) and (weights):
forward F x0 W: , .lagrangian C F N X W B(eq. 1), with continuous linear maps .
On (Fin n → ℝ):
act σ y;dact σ' yis the diagonal map .cov bis the covector ;sqerr D x.netF σ k x Vfor a weight map .bvec σ' D X W N k: and, for , , whereyvec(eqs. 5, 7, 8).
import Mathlib
namespace LeCun1988
variable {E Θ : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]
[NormedAddCommGroup Θ] [NormedSpace ℝ Θ]
/-- Forward pass: `X(0) = x₀`, `X(k+1) = F_k(X(k), W(k))`. -/
def forward (F : ℕ → E → Θ → E) (x0 : E) (W : ℕ → Θ) : ℕ → E
| 0 => x0
| k + 1 => F k (forward F x0 W k) (W k)
/-- LeCun's Lagrange function (eq. (1)), for one pattern:
`L(X, W, B) = C(X(N)) + Σ_{k<N} B(k+1) (X(k+1) - F_k(X(k), W(k)))`. -/
noncomputable def lagrangian (C : E → ℝ) (F : ℕ → E → Θ → E) (N : ℕ) (X : ℕ → E) (W : ℕ → Θ)
(B : ℕ → E →L[ℝ] ℝ) : ℝ :=
C (X N) + ∑ k ∈ Finset.range N, B (k + 1) (X (k + 1) - F k (X k) (W k))
end LeCun1988
namespace LeCun1988
open Finset
section Net
variable {n : ℕ}
/-- Componentwise activation `σ` applied to a pre-activation vector. -/
def act (σ : ℝ → ℝ) (y : Fin n → ℝ) : Fin n → ℝ := fun i => σ (y i)
/-- Its derivative: the diagonal map `v ↦ (σ'(y i) * v i)_i`. -/
noncomputable def dact (σ' : ℝ → ℝ) (y : Fin n → ℝ) : (Fin n → ℝ) →L[ℝ] (Fin n → ℝ) :=
ContinuousLinearMap.pi fun i => σ' (y i) • ContinuousLinearMap.proj i
/-- Covector `v ↦ ∑ i, b i * v i` of a vector `b`. -/
noncomputable def cov (b : Fin n → ℝ) : (Fin n → ℝ) →L[ℝ] ℝ :=
∑ i, b i • ContinuousLinearMap.proj i
/-- Squared error `∑ i, (D i - x i)^2`. -/
def sqerr (D x : Fin n → ℝ) : ℝ := ∑ i, (D i - x i) ^ 2
/-- Layer map `x ↦ σ(V x)`, the same for every layer index. -/
def netF (σ : ℝ → ℝ) : ℕ → (Fin n → ℝ) → ((Fin n → ℝ) →L[ℝ] (Fin n → ℝ)) → (Fin n → ℝ) :=
fun _ x V => act σ (V x)
/-- Backward vectors (LeCun eqs. 5, 7, 8): `b N = 2 (D - X N)`;
for `k < N`, `b k = W kᵀ y k` with `y k i = σ'((W k (X k)) i) * b (k+1) i`. -/
noncomputable def bvec (σ' : ℝ → ℝ) (D : Fin n → ℝ) (X : ℕ → Fin n → ℝ)
(W : ℕ → (Fin n → ℝ) →L[ℝ] (Fin n → ℝ)) (N : ℕ) (k : ℕ) : Fin n → ℝ :=
if h : N ≤ k then fun i => 2 * (D i - X N i)
else fun j => ∑ i, (σ' ((W k (X k)) i) * bvec σ' D X W N (k + 1) i) *
(W k (fun m => if j = m then 1 else 0)) i
termination_by N - k
/-- The error signal `y k` of LeCun's eq. (7). -/
noncomputable def yvec (σ' : ℝ → ℝ) (D : Fin n → ℝ) (X : ℕ → Fin n → ℝ)
(W : ℕ → (Fin n → ℝ) →L[ℝ] (Fin n → ℝ)) (N k : ℕ) : Fin n → ℝ :=
fun i => σ' ((W k (X k)) i) * bvec σ' D X W N (k + 1) i
end Net
end LeCun1988
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
General part (E, Θ real normed spaces). forward F x0 W : ℕ → E is the forward pass: forward 0 = x0, forward (k+1) = F k (forward k) (W k); so W k is the weight of the map from layer k to layer k+1, and F may depend on the layer index. lagrangian C F N X W B = C(X N) + Σ_{k ∈ range N} B(k+1)(X(k+1) - F k (X k)(W k)), where X : ℕ → E is an arbitrary state sequence (not necessarily the forward pass) and each B j : E →L[ℝ] ℝ is a continuous linear functional, so B(k+1)(·) plays the role of B(k+1)ᵀ(·). Only X 0..X N, W 0..W(N-1) and B 1..B N appear; B 0 and all indices above N are ignored. Network part (state space Fin n → ℝ, the same width n for every layer). act σ y is σ applied componentwise to y. dact σ' y is the continuous linear diagonal map v ↦ (σ'(y i) * v i)_i (the Jacobian of act σ at y when σ' is σ's derivative). cov b is the covector v ↦ Σ_i b i * v i (dot product with b). sqerr D x = Σ_i (D i - x i)^2, with no factor 1/2. netF σ _ x V = act σ (V x): each layer is a square linear map V : (Fin n → ℝ) →L (Fin n → ℝ) followed by componentwise σ, with no bias, and the layer index is ignored. bvec σ' D X W N k (defined by well-founded recursion on N - k): if N ≤ k it is 2(D - X N); if k < N, its j-th entry is Σ_i (σ'((W k (X k)) i) * bvec(k+1) i) * (W k e_j) i, where e_j = (m ↦ if j = m then 1 else 0) is the j-th standard basis vector, so bvec k = W kᵀ y_k (W k applied to e_j gives column j of the matrix). yvec σ' D X W N k has entry i equal to σ'((W k (X k)) i) * bvec(k+1) i, i.e. y_k = σ'(pre-activation of layer k+1) ⊙ b(k+1), LeCun's eq. (7). Like the Lagrangian, bvec and yvec take an arbitrary X; the theorems plug in X = forward (netF σ) x0 W. I checked by compilation that bvec N = 2(D - X N) and that bvec (N+1) N matches W Nᵀ yvec.
Auditor concerns: (a) Junk value: bvec returns 2(D - X N) for every k ≥ N, not just k = N, and yvec at k ≥ N uses that junk value. This is harmless because the statements only use yvec at k < N, which needs bvec only at indices up to N. (b) dact is defined but is not used in any of the 9 statements, so it is dead code here; it is correct as a diagonal Jacobian. (c) Network restrictions: equal width n for every layer, no biases, the same σ in every layer, and netF ignores the layer index. That is a special case of LeCun's network. (d) Sign and scale: the factor 2 in b N = 2(D - X N) is consistent with sqerr having no 1/2, and with B N = -∇C. (e) The pre-activation in yvec is W k (X k), which is correct for X(k+1) = σ(W k X k), but only if X is the forward pass; for an arbitrary X it is merely a formula. (f) The termination measure N - k is sound, and the definitions compile. Otherwise none.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.