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 (smooth) over classes,
Because every entry is strictly positive whenever smooth is above zero, none of the 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 are , , and .
At the default setting smooth equal to 0.15, each class receives from the uniform component, while class 1 also keeps . That gives q = [0.05, 0.90, 0.05], whose entries still sum to 1.00. Multiply each surprise by its weight :
- Class 0:
- Class 1:
- Class 2:
Adding 0.08047, 0.32101, and 0.11513 yields a soft cross-entropy of 0.51661. Notice that class 2 () contributes 0.11513, which is larger than class 0's 0.08047 () 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 . 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).

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.

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 toward zero has no effect on the loss as long as stays fixed. Under q = [0.05, 0.90, 0.05], pushing too close to zero inflates without bound.
Soft targets also arise in knowledge distillation, where a teacher network provides a full probability vector over classes. In both label smoothing and distillation, the cross-entropy formula is identical; only the source of the weight vector changes.
Recap
- Soft targets replace a hard
0/1label with a probability vector whose entries are positive and sum to1.0. - Label smoothing forms , giving every non-target class weight .
- Because is positive for every class, penalizes driving any class probability too close to zero.