Label Smoothing
Implement label smoothing, a regularization technique that prevents a model from becoming overconfident by softening the hard target labels. Instead of assigning probability 1 to the correct class and 0 to all others, label smoothing distributes a small amount of probability mass across all classes.
Formula:
y_smooth = y * (1 - epsilon) + epsilon / num_classes
Where:
y = original one-hot label vector
epsilon = smoothing factor (e.g., 0.1 or 0.3)
num_classes = total number of classes
Example:
Input: label = [0, 0, 1], factor = 0.3 (num_classes = 3)
Output: [0.1, 0.1, 0.8]
Explanation: The true class retains most of the probability: 1 * (1 - 0.3) + 0.3 / 3 = 0.7 + 0.1 = 0.8, while each non-true class receives: 0 * (1 - 0.3) + 0.3 / 3 = 0.1. This prevents the model from assigning full confidence to any single class, improving generalization and calibration.
Constraints:
Test Cases
[0,0,1][0.1,0.1,0.8][0,1,0][0.1,0.8,0.1]