TorchLean API

NN.Proofs.Autograd.FDeriv.SoftmaxSpec

Softmax and log-softmax: spec definitions versus analytic presentation #

NN.Proofs.Autograd.FDeriv.Softmax and NN.Proofs.Autograd.FDeriv.LogSoftmax prove Fréchet-derivative facts about softmaxVec and logSoftmaxVec, which are stated directly on Euclidean vectors. The specification layer instead defines Activation.softmaxVecSpec and Activation.logSoftmaxVecSpec on tensors, using the numerically stable max-shifted form.

This file closes that gap over ℝ:

Softmax #

The spec softmax kernel is the analytic softmaxVec after vectorization.

softmaxVecSpec on a vector built from Euclidean coordinates.

The vectorized forward map of Spec.softmaxOp 0 on vectors is softmaxVec.

Axis-0 spec softmax on vectors is Fréchet-differentiable with derivative softmaxDerivCLM.

The spec softmax VJP is the analytic softmaxJvp (the softmax Jacobian is symmetric).

Spec.softmaxOp 0 on vectors with its analytic JVP and the VJP/JVP adjointness law.

Instances For

    Spec.softmaxOp 0 on vectors is analytically correct: its JVP is the Fréchet derivative.

    Instances For

      The spec softmax backward is the vector-Jacobian product of the spec softmax forward.

      Log-softmax #

      The spec log-softmax kernel is the analytic logSoftmaxVec after vectorization.

      The vectorized forward map of Spec.logSoftmaxOp 0 on vectors is logSoftmaxVec.

      Axis-0 spec log-softmax on vectors is Fréchet-differentiable with derivative logSoftmaxDerivCLM.

      Exponentiating the spec log-softmax output recovers the analytic softmax coordinates.

      The typed graph's compensated log-softmax JVP agrees with the analytic derivative.

      The spec log-softmax VJP, evaluated on the recomputed forward output, is the analytic logSoftmaxVjp.

      Spec.logSoftmaxOp 0 on vectors with its analytic JVP and the VJP/JVP adjointness law.

      Instances For

        Spec.logSoftmaxOp 0 on vectors is analytically correct: its JVP is the Fréchet derivative.

        Instances For

          The spec log-softmax backward is the vector-Jacobian product of the spec log-softmax forward.

          Weighted logits cross-entropy #

          The overflow-aware weighted loss preserves the real cross-entropy formula. This is a real identity, not bitwise equivalence between the two floating-point evaluation schedules.

          The shared logits pullback differentiates the actual weighted loss, including unnormalized and zero target weights. No probability-normalization hypothesis is needed.

          theorem Proofs.Autograd.hasFDerivAt_crossEntropyTargets_vec {n : ℕ} (logits : TorchLean.Tensor ℝ [n]) (target : Vec n) :
          HasFDerivAt (fun (weights : Vec n) => Spec.crossEntropyLogitsSpec 0 logits (ofFnE weights)) (dotCLM (getScalarE (Spec.CrossEntropy.targetGradient 0 logits))) target

          Target weights remain differentiable, including at zero. The numerical implementation must not turn a zero-weight forward shortcut into a zero target derivative.

          Distillation #

          The logit-difference gradient is the usual student-minus-teacher probability difference over the reals. Both numerical branches and the residual correction preserve this identity.

          theorem Proofs.Autograd.hasFDerivAt_distillSpec_vec {n : ℕ} (student : Vec (n + 1)) (teacher : TorchLean.Tensor ℝ [n + 1]) :
          HasFDerivAt (fun (x : Vec (n + 1)) => Spec.distillSpec 0 (ofFnE x) teacher) (dotCLM (getScalarE (Spec.distillDerivSpec 0 (ofFnE student) teacher))) student

          With teacher logits fixed, distillDerivSpec is the Fréchet derivative of the actual distillation loss on nonempty class vectors. This connects the compensated pullback to the loss, rather than just identifying it with another gradient formula.