Before you start
Write cross entropy with label smoothing and padding, the loss used to train most classifiers and language models.
Hard labels say the true class has probability 1. Label smoothing moves a
fraction smoothing of that mass evenly over all C classes:
q = (1 - smoothing) * one_hot(target) + smoothing / C
The loss is the cross entropy between q and the model's distribution
p = softmax(logits). Because q is a mixture, the loss splits into two
parts:
loss = (1 - smoothing) * (-log p[target]) + smoothing * mean over classes of (-log p)
Compute log p with torch.log_softmax, so that large logits do not overflow.
Positions where the target equals ignore_index are padding. Leave them out,
and average over the rest.
def smoothed_cross_entropy(logits, targets, smoothing, ignore_index=-100):
Input
logits = torch.tensor([[2.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
targets = torch.tensor([0, -100])
smoothed_cross_entropy(logits, targets, smoothing=0.3)
Output
tensor(0.6395)
Reasoning
Only the first row counts. log p = [-0.2395, -2.2395, -2.2395]. The hard part
is 0.2395, and the mean of -log p is 1.5728. So the loss is
0.7 * 0.2395 + 0.3 * 1.5728 = 0.6395.
logits has shape (..., C) and targets has shape (...), int64. A
language model gives (batch, time, C).ignore_index. 0 <= smoothing < 1.label_smoothing argument of the built in loss.Run it in Colab
Google Colab has PyTorch and a free GPU, and needs nothing installed. Take a code, open the notebook, and paste the code into its first cell. Grading happens there and the result comes back here.
In Colab, the grading cell is your Submit. It runs the hidden cases there, and the result shows here on its own.
⇧↵run cell