Loss Functions › Label Smoothing

Michael BrenndoerferMay 2, 20253 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 64Loss Functions › Label Smoothing
Reading section 5 of 17 · Label Smoothing · 3 minSection 5 of 17 · 3 min

Book contents

Section 5 of 17 · 3 min read

Loss Functions › Label SmoothingOne section · Sections in this chapter5 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Standard cross-entropy trains the model to push probabilities toward 0 or 1 for the correct and incorrect classes. This works well, but it can cause overconfidence: the model may assign extremely high probability to one class even for ambiguous examples, becoming poorly calibrated.

Label smoothing addresses this by replacing the hard one-hot labels with soft labels that reserve a small probability mass ϵ\epsilon for all classes:

yksmooth={1−ϵ+ϵKif k=y∗ϵKif k≠y∗y_k^{\text{smooth}} = \begin{cases} 1 - \epsilon + \frac{\epsilon}{K} & \text{if } k = y^* \\ \frac{\epsilon}{K} & \text{if } k \neq y^* \end{cases}

where:

  • ϵ\epsilon: the smoothing parameter, typically 0.1
  • KK: the total number of classes
  • y∗y^*: the index of the true class
  • 1−ϵ+ϵK1 - \epsilon + \frac{\epsilon}{K}: the smoothed probability for the correct class (slightly less than 1)
  • ϵK\frac{\epsilon}{K}: the smoothed probability for each incorrect class (slightly above 0)

The cross-entropy loss with smoothed labels becomes:

LLS=−(1−ϵ)log⁡py∗−ϵK∑k=1Klog⁡pk\mathcal{L}_{\text{LS}} = -(1 - \epsilon) \log p_{y^*} - \frac{\epsilon}{K} \sum_{k=1}^{K} \log p_k

where:

  • py∗p_{y^*}: the predicted probability for the true class
  • ∑k=1Klog⁡pk\sum_{k=1}^{K} \log p_k: the sum of log probabilities across all classes

Label smoothing has two effects. It discourages the model from assigning zero probability to any class, which improves calibration. It also reduces the gap in logits between the correct class and incorrect classes, making the model's representations more spread out. The original Transformer paper used ϵ=0.1\epsilon = 0.1, and label smoothing has since become standard in many NLP models.

To see why label smoothing improves calibration, consider what happens during training without it. The optimal model under standard cross-entropy would assign probability 1 to the true class and probability 0 to all other classes, for every training example. This means the logits for the true class would need to go to infinity, which is impossible in finite training but the model tries to approach. This drives the logit gap (difference between the top logit and the second-highest) to be very large, leading to overconfident predictions. Label smoothing provides a finite target: the optimal model assigns 1−ϵ+ϵ/K1 - \epsilon + \epsilon/K to the true class, which requires a finite logit gap. Training naturally converges to this finite optimal, producing better-calibrated confidence scores.

Calibration

A model is well-calibrated if its predicted probabilities match the actual frequency of outcomes. A perfectly calibrated model that says "I'm 70% confident" should be correct 70% of the time. Overconfident models (assigning 99% probability to answers they get wrong) have poor calibration. Label smoothing tends to improve calibration by preventing extreme probability assignments.