Speed

Soft Targets Weight Terms

Apply label smoothing to a three-class target vector and compute the weighted log-penalty contribution of every class.

The problem

A one-hot target assigns weight 1.0 to the ground-truth class and 0.0 to every alternative. That encourages a classifier to push its predicted probability on the true class as close to 1.0 as possible and its probabilities on all other classes toward 0.0, even when labels are noisy or classes overlap.

Soft targets replace the hard one-hot vector y with a distribution q that keeps small positive weights on every class. Under label smoothing with parameter ε\varepsilon (smooth) over K=3K = 3 classes,

qk=(1−ε)yk+εK,H(q,p)=−∑k=0K−1qklog⁡(pk)q_k = (1 - \varepsilon) y_k + \frac{\varepsilon}{K}, \qquad H(q, p) = -\sum_{k=0}^{K-1} q_k \log(p_k)

Because every entry qkq_k is strictly positive whenever smooth is above zero, none of the −log⁡(pk)-\log(p_k) terms vanish. The cross-entropy becomes a weighted average of all three class surprises.

Work through the numbers

Take the hard target y = [0.0, 1.0, 0.0] and model probabilities p = [0.20, 0.70, 0.10]. Their unweighted surprises −log⁡(pk)-\log(p_k) are −log⁡(0.20)=1.60944-\log(0.20) = 1.60944, −log⁡(0.70)=0.35667-\log(0.70) = 0.35667, and −log⁡(0.10)=2.30259-\log(0.10) = 2.30259.

At the default setting smooth equal to 0.15, each class receives 0.15/3=0.050.15 / 3 = 0.05 from the uniform component, while class 1 also keeps 0.85×1.0=0.850.85 \times 1.0 = 0.85. That gives q = [0.05, 0.90, 0.05], whose entries still sum to 1.00. Multiply each surprise by its weight qkq_k:

  • Class 0: 0.05×1.60944=0.080470.05 \times 1.60944 = 0.08047
  • Class 1: 0.90×0.35667=0.321010.90 \times 0.35667 = 0.32101
  • Class 2: 0.05×2.30259=0.115130.05 \times 2.30259 = 0.11513

Adding 0.08047, 0.32101, and 0.11513 yields a soft cross-entropy of 0.51661. Notice that class 2 (p2=0.10p_2 = 0.10) contributes 0.11513, which is larger than class 0's 0.08047 (p0=0.20p_0 = 0.20) even though both share the same target weight 0.05.

The live model

The blue bars on the left show the smoothed target weights q, and the red bars on the right show the weighted terms −qklog⁡(pk)-q_k \log(p_k). Dragging smooth redistributes target weight between class 1 and the two off-target classes.

At smooth equal to 0.0, q reverts to the hard one-hot vector [0.0, 1.0, 0.0] and the cross-entropy is 0.357 (0.35667). At the default smooth equal to 0.15, q is [0.05, 0.90, 0.05] and the loss rises to 0.517. At smooth equal to 0.30, q becomes [0.10, 0.80, 0.10] with terms [0.16094, 0.28534, 0.23026], raising the total to 0.677 (0.67654).

Three classes with smoothed target weights 0.05, 0.90, 0.05 and probabilities 0.20, 0.70, 0.10 give weighted terms 0.080, 0.321, 0.115 and sum 0.517.
let smooth = scene.parameter("smooth", 0.15, 0.0, 0.30, 0.15);
let y = input.select(0, 0);
let p = input.select(0, 1);
let q = y * (1.0 - smooth) + smooth / 3.0;
let terms = q * p.log().neg();
let ce = terms.sum().reshape(1);
let answer = torch.cat(torch.list().add(terms).add(ce), 0);
out.set("q", q).set("terms", terms).set("ce", ce);
{
  "q": [
    0.049999999999999996,
    0.9,
    0.049999999999999996
  ],
  "terms": [
    0.08047189562170501,
    0.3210074495448592,
    0.11512925464970226
  ],
  "ce": [
    0.5166085998162665
  ]
}

Your turn

At smooth = 0.15 the starter reports terms = [0.0, 0.35667, 0.0] and ce = 0.35667 because it still weights the surprises by the unsmoothed indicator vector; by hand the smoothed weights [0.05, 0.90, 0.05] give [0.08047, 0.32101, 0.11513] and a sum of 0.51661. Fix terms so every class surprise is weighted by the smoothed target distribution.

Three classes with smoothed target q; the starter multiplies p.log().neg() by hard y instead of q.
let smooth = scene.parameter("smooth", 0.15, 0.0, 0.30, 0.15);
let y = input.select(0, 0);
let p = input.select(0, 1);
let q = y * (1.0 - smooth) + smooth / 3.0;
let terms = y * p.log().neg();
let ce = terms.sum().reshape(1);
let answer = torch.cat(torch.list().add(terms).add(ce), 0);
out.set("q", q).set("terms", terms).set("ce", ce);
{
  "q": [
    0.049999999999999996,
    0.9,
    0.049999999999999996
  ],
  "terms": [
    0,
    0.35667494393873245,
    0
  ],
  "ce": [
    0.35667494393873245
  ]
}

Each class surprise in terms must be weighted by the smoothed target distribution rather than the hard one-hot vector. Check tests unseen probability vectors and smoothing levels.

With the smoothed target weights applied, the default run at smooth = 0.15 returns [0.08047190, 0.32100745, 0.11512925, 0.51660860]. The check reruns the smoothing on target [1.0, 0.0, 0.0] with p = [0.60, 0.25, 0.15] at smooth = 0.15 and on target [0.0, 0.0, 1.0] with p = [0.15, 0.35, 0.50] at smooth = 0.30.

Read the result carefully

Comparing smooth = 0.0 (0.35667) against smooth = 0.15 (0.51661) shows why soft targets change how wrong classes are treated. Under a one-hot target, shrinking p2p_2 toward zero has no effect on the loss as long as p1p_1 stays fixed. Under q = [0.05, 0.90, 0.05], pushing p2p_2 too close to zero inflates −0.05log⁡(p2)-0.05 \log(p_2) without bound.

Soft targets also arise in knowledge distillation, where a teacher network provides a full probability vector qq over classes. In both label smoothing and distillation, the cross-entropy formula −∑kqklog⁡(pk)-\sum_k q_k \log(p_k) is identical; only the source of the weight vector qq changes.

Recap

  • Soft targets replace a hard 0/1 label with a probability vector qq whose entries are positive and sum to 1.0.
  • Label smoothing forms qk=(1−ε)yk+ε/Kq_k = (1 - \varepsilon) y_k + \varepsilon / K, giving every non-target class weight ε/K\varepsilon / K.
  • Because qkq_k is positive for every class, −qklog⁡(pk)-q_k \log(p_k) penalizes driving any class probability pkp_k too close to zero.