TorchLean API

NN.Proofs.Analysis.Softmax

Softmax analysis properties #

This module proves theorem-level facts about TorchLean's spec-level softmax operators. The definitions themselves live in NN.Spec.Layers.Activation; this file belongs under NN.Proofs.Analysis because it imports real-analysis and finite-sum proof machinery to establish properties of those definitions.

Current theorem surface:

We intentionally state these over : positivity of exp and division by a positive denominator are the mathematical facts that make the probabilistic interpretation precise.

Scalar helpers #

softmaxVecSpec is written over tensors, so even one coordinate has type Tensor ℝ .scalar. Local helper definitions expose scalar coordinates to the proof without adding public API.

Stable max shift #

Every coordinate is bounded above by the exact maximum used by softmax and log-softmax.

The maximum used by stable softmax is attained by an input coordinate.

Every logit shifted by the implementation's maximum is nonpositive.

At least one max-shifted logit is exactly zero.

Exponentiating a max-shifted real logit produces a value at most one. This is the central overflow-prevention fact behind stable softmax.

Max-shifted real exponentials remain strictly positive.

One max-shifted exponential is exactly one, because the maximum is attained.

The stable softmax denominator lies in [1,n] for a nonempty vector of length n.

The lower bound rules out division by zero. The upper bound follows because every shifted exponential is at most one. Together with softmax_shift_exp_le_one, this makes overflow prevention an explicit theorem of the max-shifted implementation rather than an empirical claim.

Normalized coordinates #

theorem Proofs.softmax_vec_spec_normalized {n : } (t : Spec.Tensor (Spec.Shape.dim n.succ Spec.Shape.scalar)) :
∃ (weights : Fin n.succ), (∀ (i : Fin n.succ), 0 < weights i) ∀ (i : Fin n.succ), TensorAlgebra.toVec (Activation.softmaxVecSpec t) i = weights i / j : Fin n.succ, weights j

The stable vector softmax has positive weights normalized by their sum.

This lemma exposes exactly one reusable algebraic description of the implementation. The weights are the max-shifted exponentials computed by softmaxVecSpec; subsequent proofs of positivity, range, and normalization do not unfold the implementation again.

Coordinate equation for the concrete stable vector softmax.

This is the small unfolding lemma that downstream algebraic proofs should use. It exposes the max-shifted numerator and its tensor sum while hiding the implementation chosen for tensor reduction.

Probability-simplex properties #

Every coordinate of a nonempty real softmax vector is strictly positive.

softmaxVecSpec produces a vector whose entries sum to 1 over .

Every coordinate of a nonempty real softmax vector lies in the closed unit interval.

Backward conservation #

The concrete stable softmax backward is tangent to the probability simplex.

Activation.softmaxBackwardSpec is the VJP used by the spec and tape layers. Its coordinate sum is zero because the stable forward weights sum to one. This statement is about the actual tensor definition, not the separate analytic EuclideanSpace presentation of the same derivative.

Coordinatewise bound for the concrete stable softmax VJP.

If every upstream coordinate has magnitude at most G, each input-gradient coordinate has magnitude at most 2G. The estimate does not grow with the axis length because the softmax output is a nonnegative vector of total mass one.

softmaxSpec on matrices is rowwise, so each row sums to 1.

This is the attention-shaped theorem: for score matrices, the key axis is the last/vector axis, and softmax is applied independently to every query row.

Convenience row-sum theorem when the key dimension is written as an arbitrary nK plus a proof nK ≠ 0.

Many model statements quantify over a natural key length nK; this wrapper converts that style into the Nat.succ _ shape required by sum_spec_softmax_spec_row.