Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-166

Label-Smoothed Cross-Entropy

Problem

Compute label-smoothed cross-entropy: the true class gets probability mass 1 - epsilon + epsilon/K and every other class gets epsilon/K, and the loss is -sum(mass * log(p)).

Probabilities are clipped to at least 1e-15. Return the mean over samples; empty input returns 0.0.

Hint

Smoothing spreads a little target mass onto every class, penalizing overconfident predictions.

Test cases

case 1
in: [[0,1],[[0.8,0.2],[0.3,0.7]],0.1]
expected: 0.34574905316414845
case 2
in: [[0],[[0.9,0.05,0.05]],0.3]
expected: 0.6834348672370592
case 3
in: [[1],[[1,0]],0]
expected: 34.538776394910684
case 4
in: [[],[],0.1]
expected: 0
Starter codePython
import math
def label_smoothed_cross_entropy(y_true, probs, epsilon):
    # Your code here
    pass

4 test cases · run your code against them in the editor