MediumLoss Functions

Label Smoothing Loss

Loss Functions

Medium

Problem

Label smoothing is a regularization technique that prevents a model from becoming overconfident. Instead of using hard one-hot targets (1 for the correct class, 0 for all others), it softens the target distribution by redistributing a small fraction of probability mass to the incorrect classes.

Given a predicted probability distribution, a target class index, and a smoothing parameter epsilon, compute the cross-entropy loss with smoothed labels.

Algorithm

  1. Build the smoothed target distribution. For K classes and smoothing factor epsilon:

q_i = (1 - \epsilon) + \frac{\epsilon}{K} \quad \text{if } i = \text{target}

q_i = \frac{\epsilon}{K} \quad \text{if } i \ne \text{target}

  1. Compute the cross-entropy loss between the smoothed targets q and the predictions p:

L = -\sum_{i=0}^{K-1} q_i \cdot \ln(p_i)

Theory

Standard classification uses one-hot labels:

Example for 5-class classification with true class 2:

Cross-entropy with hard labels pushes the model to:

This causes problems:

Overconfidence: The model becomes extremely confident, outputting predictions like [0.001, 0.001, 0.997, 0.001, 0.000]. This overconfidence is rarely warranted.

Poor calibration: The model's confidence does not match its accuracy. A 95% confident prediction might only be correct 80% of the time.

Sensitivity to noise: If labels have errors, the model tries hard to fit the wrong labels.


What Label Smoothing Does

Label smoothing replaces hard labels with soft labels:

y_{\text{smooth}} = (1 - \epsilon) \cdot y_{\text{hard}} + \frac{\epsilon}{K}

Where:

For true class c:

For other classes j \neq c:


Numerical Example

5-class classification, true class = 2, smoothing = 0.1:

Hard label: [0, 0, 1, 0, 0]

Smoothed label:

The model is now encouraged to:


The Loss Function

Label smoothing modifies cross-entropy by using soft labels:

L = -\sum_{k=1}^{K} y_k^{\text{smooth}} \log(\hat{y}_k)

Expanding:

L = (1 - \epsilon) \cdot [-\log(\hat{y}_c)] + \epsilon \cdot \left[-\frac{1}{K}\sum_{k=1}^{K} \log(\hat{y}_k)\right]

This is:

The second term pushes predictions toward uniform, counteracting overconfidence.


Why It Works

Regularization effect:

Better calibration:

Robustness to label noise:

Improved generalization:


Choosing the Smoothing Parameter

The smoothing parameter \epsilon controls how soft the labels are:

epsilon = 0: No smoothing, standard hard labels epsilon = 0.1: Typical value, mild smoothing epsilon = 0.2: Stronger smoothing, more regularization epsilon = 1/K: Uniform labels, the model learns nothing about class identity

Common practice:


Effect on Predictions

Without label smoothing:

With label smoothing:

Example predictions for a sample:

Without smoothing: [0.001, 0.002, 0.995, 0.001, 0.001] With smoothing: [0.03, 0.04, 0.88, 0.03, 0.02]

Both predict class 2, but the smoothed version is less extreme.


The Gradient

The gradient with respect to logits changes slightly:

Without smoothing (for true class):

\frac{\partial L}{\partial z_c} = \hat{y}_c - 1

With smoothing (for true class):

\frac{\partial L}{\partial z_c} = \hat{y}_c - (1 - \epsilon + \epsilon/K)

For other classes:

\frac{\partial L}{\partial z_j} = \hat{y}_j - \epsilon/K

The key difference: with smoothing, the gradient for non-true classes is nonzero. The model receives a small signal pushing predictions for other classes away from zero.


Label Smoothing vs. Temperature Scaling

Both affect prediction confidence, but differently:

Label smoothing:

Temperature scaling:

They can be combined: train with label smoothing, then fine-tune calibration with temperature scaling.


Where Label Smoothing Is Used

Not recommended when:

Examples

Example 1

Input
predictions = [0.9, 0.05, 0.05], target = 0, epsilon = 0.1
Output
0.29805196618423724
Explanation
The correct class receives 0.933333 target mass and each other class receives 0.033333.

Example 2

Input
predictions = [0.7, 0.3], target = 0, epsilon = 0.2
Output
0.44140472997745284

Hints

  1. Give every class epsilon divided by the class count.
  2. Add one minus epsilon to the target class before accumulating cross-entropy.

Requirements

Constraints

Starter Code

import math

def label_smoothing_loss(predictions: list, target: int, epsilon: float) -> float:
    """
    Returns cross-entropy loss for the smoothed target distribution.
    """
    # Write code here
    pass

Test Cases

CaseMatches
3 classpublic
Binarypublic