The label-smoothed loss cotangent, at a GENERAL target #
The ties of the per-example SGD-inline renders pin their top-of-chain cotangent to
softmax(logits) − oneHot label — the gradient of plain cross-entropy at a hard label. The
batched ImageNet renders emit a different, label-smoothed chain (below), and their step ties —
ResNet-34, ResNet-50, MobileNetV2, MobileNetV4, EfficientNet-B0, ConvNeXt-T and ViT, plus the
sync-BN twins of the first five — state it through smoothedLossCotGraph.
ResNet34RenderB.lean composes the head cotangent from six kit ops:
%sm = softmaxRow(logits) %d0 = %sm − %onehot %lsa = α · %onehot
%d1 = %d0 + %lsa %d2 = %d1 − α/K %dy = %d2 / B
so dy = (softmax(z) − t + α·t − α/K) / B, with α = 0.1 baked (the ls0 variants set it to 0)
and t arriving as the graph INPUT %onehot — which under mixup or cutmix is a soft vector drawn
on the host, not a one-hot. ConvNeXtRenderB and ViTRenderB compose the same six ops. This file
is the cotangent lemma those ties need: at a general target and at the smoothed form.
What is proved #
softCE K t z = Σ_k t_k · crossEntropy K z k— cross-entropy against a target distribution, not a label.softCE_gradis its gradient,(Σ_k t_k)·softmax(z)_j − t_j, with no hypothesis ontat all: it issoftmaxCE_gradunderpdiv_finset_sum, and the familiarsoftmax − tis theΣ t = 1case.smoothTarget K α t = (1−α)·t + α/Kis label smoothing as a map on targets, and it preservesΣ = 1(smoothTarget_sum, the one place0 < Kis needed).smoothedCE_grad: the gradient ofsoftCEat the SMOOTHED target is exactly the expression the render emits —softmax(z)_j − t_j + α·t_j − α/K. So the six-op chain is not an approximation of the smoothed loss's gradient; it is that gradient, rearranged so that the smoothing is two extra elementwise ops on the target rather than a change to the target itself.smoothedLossCotGraph/smoothedLossCotGraph_den/smoothedLossCotGraph_row: the emitted graph, its denotation, and the per-example row of that denotation as the smoothed gradient divided by the batch.
What is NOT claimed #
Note: The / B is the batch mean, and it is a convention, not a theorem here. smoothedLossCotGraph_row
states the row IS (1/B)·∂softCE/∂z at that example's logits; that the sum of B such rows is the
gradient of the mean loss is the linearity step, and a tie against a *dp* artifact needs the
replica mean on top of it (DataParallel.Node).
Note: α is a free real. The committed renders bake 0.1, and the ls0 twins bake 0; both are
instances. Note: Nothing here says t is a probability vector — only Σ t = 1 is ever used, which is
what mixup's convex combination of two one-hots satisfies.
References #
- Szegedy et al. 2016, Rethinking the Inception Architecture for Computer Vision (§7 label smoothing). https://arxiv.org/abs/1512.00567
Soft-target cross-entropy −Σ_k t_k log softmax(z)_k, written as the t-weighted sum of
the per-class crossEntropy. At t = oneHot label it IS crossEntropy K z label
(softCE_oneHot).
Equations
- Proofs.softCE K t z = ∑ k : Fin K, t k * Proofs.crossEntropy K z k
Instances For
At a one-hot target, soft-target CE is the ordinary cross-entropy.
The soft-target CE gradient, with NO hypothesis on t:
∂/∂z_j (−Σ_k t_k log p_k) = (Σ_k t_k)·p_j − t_j. Each summand is softmaxCE_grad; the sum
comes out by pdiv_finset_sum, and each t_k factor by pdiv_mul against a constant.
The emitted expression IS the smoothed loss's gradient. ∂/∂z_j of soft-target CE at
the SMOOTHED target (1−α)t + α/K equals softmax(z)_j − t_j + α·t_j − α/K, which is exactly
what the render's softmaxRow → subB → scaleB → addVB → shiftB chain computes, before the
batch divide.
The five ops after the softmax: (sm − oh + α·oh + s) / B. Both smoothed graphs below
are this applied to their softmax, and it is computable (the shift s is an argument, where
the graphs pass -(α/K)), so a render prints its cotangent as pretty of this node over its
softmax's name (RenderKit.smoothedCotB).
Equations
- Proofs.smoothedCotTail α s B aStr negAK bStr sm oh = Proofs.StableHLO.SHlo.divConstB bStr B (Proofs.StableHLO.SHlo.shiftB negAK s ((sm.subB oh).addVB (Proofs.StableHLO.SHlo.scaleB aStr α oh)))
Instances For
The six-op label-smoothed cotangent chain the batched renders emit, at one row per example
(m = 1, n = K) and batch N. logits is the head's output and t the graph input
%onehot; α is the smoothing and B the batch divisor (the render bakes B = N).
Equations
- One or more equations did not get rendered due to their size.
Instances For
What the chain denotes, coordinatewise: (rowSoftmax(logits) − t + α·t − α/K) / B.
Each row of the emitted cotangent is the smoothed loss's gradient at that example's
logits, divided by the batch. Mat.unflatten splits the flat N·(1·K) activation into its
N per-example rows; smoothedCE_grad supplies the gradient. The hypothesis is only that the
example's target sums to 1 — a one-hot, a mixup convex combination, or any distribution.
The six-op chain as ConvNeXtRenderB and ViTRenderB emit it. Those two renders spell the
row softmax as batchOp expe then batchOp softmaxDiv — the per-example
softmaxDiv (expe ·) pair the SGD-inline files tie (cnxLossCot_den), lifted — and run the
whole chain at the plain width N·K rather than N·(1·K), so there is no row index and no
rowB/unrowB cast at the head. Same six functions, different AST;
smoothedLossCotGraphDiv_den says the denotation is smoothedLossCotGraph's, and
smoothedLossCotGraphDiv_row is the per-example statement at batchSlice.
Equations
- One or more equations did not get rendered due to their size.
Instances For
What the chain denotes, coordinatewise: (softmax(logits_n) − t + α·t − α/K) / B, the
per-example softmax lifted by batchMap — the same function smoothedLossCotGraph_den reads
off the softmaxRow spelling.
Each example's row of the emitted cotangent is the smoothed loss's gradient at that
example's logits, divided by the batch — smoothedLossCotGraph_row at the softmaxDiv
spelling and the plain N·K index, where the example is batchSlice N K · n with no
Mat.unflatten. The hypothesis is only that the example's target sums to 1.
The hard-label cotangent and the output-layer chain rule #
The per-example ties' top of chain: any emitted softmax − onehot cotangent denotes the plain
cross-entropy gradient at a hard label (softmaxCELossCot_den), and the whole-loss gradient
wrt a top dense layer's weight is the certified ∂logits/∂W contracted with that cotangent
(lossWeightGrad_eq_sum, by pdiv_comp).
Any emitted softmax-CE loss cotangent denotes softmax(logits) − onehot, generic in the
class count and the logits. Every net's *LossCot_den is this at its forward's logits.
The dense layer is differentiable in its (flattened) weights. The map
v ↦ dense (unflatten v) b x is affine — a finite sum of coordinate
evaluations scaled by x, plus the constant bias.
The total-loss gradient wrt a weight entry equals the certified
(∂logits/∂W) · (softmax − onehot) contraction (the chain rule, pdiv_comp).