Label formats: class index, one-hot, label smoothing
There are several ways to tell a classifier "this photo is a cat": a single number, a bar with a 1 in one slot, a bar with a little taken off and shared out, or the probabilities a bigger model gave. Change the answer bars and see how the loss of the same prediction, and the place the model is pulled toward, both change.
Short answer. For classification you usually give the answer as one class number (an index). In the loss this means the same as a one-hot bar with a 1 in that slot. You can also give the answer as a probability bar: label smoothing takes ε off the right class and shares it over every class, y′ = (1 − ε)·y + ε ÷ K, and knowledge distillation uses a big model's probabilities as the answer. With one-hot, the model keeps pushing the right score up forever. With a probability bar, it stops once it matches that bar. If one example can have several right answers (multi-label), you use a sigmoid per class and binary cross-entropy instead of softmax.
Use the buttons on top to pick the answer bars (green). ε only moves for label smoothing. Changing the cat score z₁ moves the prediction (blue) and the black dot on the curve below. The other three scores stay at 1, 0, −1. This is a small toy with four scores, not a real model. In real training every score moves for every example, and the teacher's probabilities (0.7, 0.2, 0.07, 0.03) are made up.
Definitions
The text below explains with pictures and experiments. The textbook definitions are as follows. K is the number of classes, k is a class number (1 to K), and log is the natural log.
- logit, softmax
- The logit zk is the score a classification model gives for each class. Softmax takes the K logits and gives the predicted probabilities pk below. Every pk is greater than 0 and they add up to 1.
- class index, one-hot
- A class index is the single number c of the correct class. One-hot writes the same answer as a bar y of length K, where only the answer slot yc is 1 and every other slot is 0.
- cross-entropy loss
- The loss below, which measures how far the predicted probabilities p are from an answer bar y that adds up to 1. If y is the one-hot of answer c, only −log pc is left, the same as the loss from the class index.
- label smoothing
- Using as the answer the bar y′ made by taking a share ε (0 < ε < 1) away from the one-hot answer y and giving ε ÷ K to each of the K slots. It is the same as mixing the one-hot with the uniform distribution (every slot 1 ÷ K) in the ratio 1 − ε to ε. The answer slot becomes 1 − ε + ε ÷ K and every other slot ε ÷ K.
- knowledge distillation
- A method that uses the predicted probability bar of a model trained earlier (the teacher) as the answer bar for another model (the student), and trains the student with cross-entropy.
- multi-label classification, binary cross-entropy
- Classification where one item can have several correct classes. The answer is a bar y with 0 or 1 in each slot, and it does not add up to 1. Each slot gets its own sigmoid, pk = σ(zk), and the binary cross-entropy of each slot is averaged (some implementations add them up instead).
1. Ways to give the answer
A classifier gives each class a score z, and softmax turns the scores into predicted probabilities p that add up to 1. How far the prediction p is from the answer y is measured with cross-entropy: loss = −Σ y_k·log p_k (k is the class number, K is the number of classes). In this formula the answer y is a bar with a number in every slot. How you fill that bar is what this page is about. For a cat photo with four classes (cat, dog, fox, rabbit, so K = 4):
| Format | Answer (cat, dog, fox, rabbit) | Sum of slots | Loss it goes with |
|---|---|---|---|
| Class index | 0 (the cat's number. Code counts from 0, while z₁ and p₁ in the formulas below count from 1) | (one number) | −log p_right |
| One-hot | (1, 0, 0, 0) | 1 | softmax + cross-entropy. Same value as the index |
| Label smoothing (ε = 0.1) | (0.925, 0.025, 0.025, 0.025) | 1 | softmax + cross-entropy |
| Teacher's probabilities (distillation) | (0.7, 0.2, 0.07, 0.03) | 1 | softmax + cross-entropy |
| Multi-label (a cat and a dog together) | (1, 1, 0, 0) | not 1 | a sigmoid per class + binary cross-entropy |
One-hot is 0 everywhere except the right class, so only that one slot is left in the loss sum. That is why an index and a one-hot bar give the same loss: the index is just a short way to write the one-hot bar. One-hot, smoothing and the teacher's probabilities are all "probability bars that add up to 1", so they go into the same loss. Only multi-label asks a different question. It is not "which one of the four" but "is each one there or not", so there is no reason for the sum to be 1 (section 5).
2. How the formula is built: label smoothing
We start from the one-hot answer and add one piece at a time. ε (epsilon) is the amount we take off, and K is the number of classes. The lab uses ε = 0.1 and K = 4.
- Start:
y. One-hot. The right class is 1, the rest are 0. - Take some off:
(1 − ε)·y. The right class becomes 0.9. Why: to stop the model from trusting the answer 100%, the right class has to get less than 1. As it stands the sum is 0.9, so this is not a probability. - Share it out:
(1 − ε)·y + ε ÷ K. Split the 0.1 evenly, 0.025 to each of the four classes. The right class gets a share too. Why: the sum has to be 1 again, and we do not know which wrong class is more likely, so every class gets the same. This is the same as mixing in ε of a flat bar where every class is 1 ÷ K. - Done:
y′ = (1 − ε)·y + ε ÷ K. y′ = (0.925, 0.025, 0.025, 0.025). The right class is1 − ε + ε ÷ K. Now the prediction with the lowest loss is p = y′, and there the right score beats the other scores byln(0.925 ÷ 0.025) = ln 37 ≈ 3.61. Unlike one-hot, there is an end.
Tools can differ a little. PyTorch's label_smoothing shares over all K classes, the right one included, like this formula. Some code shares only over the other K − 1 classes, so check the docs of the tool you use.
Try this. Press "Next" three times and watch the sum at the top. It goes 1 → 0.9 → 1, and the right class goes 1 → 0.9 → 0.925. It ends at 0.925, not 0.9, because the right class also gets its 0.025 share.
3. Work it out by hand
Take the lab's starting scores z = (2, 1, 0, −1) and work out the loss for the three answers. The log is the natural log (ln).
- e^z and their sum. e² ≈ 7.389, e¹ ≈ 2.718, e⁰ = 1, e⁻¹ ≈ 0.368. Sum ≈ 11.475,
ln 11.475 ≈ 2.44. - Prediction. Divide each one by the sum: p ≈ (0.644, 0.237, 0.087, 0.032).
- −log p for each class.
−log p_k = 2.44 − z_k, so 0.44, 1.44, 2.44, 3.44. - Weight by the answer bar and add. One-hot:
1 × 0.44 = 0.44. Smoothing:0.925 × 0.44 + 0.025 × (1.44 + 2.44 + 3.44) ≈ 0.407 + 0.183 = 0.59. Teacher:0.7 × 0.44 + 0.2 × 1.44 + 0.07 × 2.44 + 0.03 × 3.44 ≈ 0.308 + 0.288 + 0.171 + 0.103 = 0.87.
| Answer bars | Loss | Floor (entropy of the answer) | Gap left |
|---|---|---|---|
| One-hot | 0.44 | 0 | 0.44 |
| Label smoothing (ε = 0.1) | 0.59 | 0.349 | 0.241 |
| Teacher's probabilities | 0.87 | 0.863 | 0.007 |
The floor is the loss you get when the prediction matches the answer bars exactly. It is the entropy of the answer, −Σ y·log y. Unless the answer is one-hot, the floor is above 0. So the teacher's loss of 0.87 looks the biggest, but its gap to the floor is the smallest, 0.007. This prediction is almost the same as the teacher's. When the answer bars differ, do not compare raw loss numbers. Compare the gap left (the KL divergence).
4. The answer bars decide where the model goes
With softmax and cross-entropy together, the slope of the loss as you change the score of class k is simply p_k − y_k (when y adds up to 1). Training moves each score against this slope, so a class whose p is below y gets its score raised, and one above y gets it lowered. It stops when p = y.
- One-hot: the right class has y = 1, but softmax never lets p reach 1. So the right score is always told to go up, and its gap to the other classes keeps growing. This is one reason models become too sure of themselves.
- Label smoothing: it stops once p₁ reaches 0.925. The score gap stops growing at about 3.61. The lowest point on the lab's curve is at 3.92 because the other three scores, 1, 0, −1, are not equal.
- Teacher's probabilities: the student follows the teacher not just on the right class but on the size of every wrong class too. Numbers like dog 0.2 and rabbit 0.03 tell it "what this looks like".
Try this. ① With one-hot, raise the cat score 2 → 6 → 10. The loss keeps falling, 0.44 → 0.01 → 0 (rounded to three places). The curve has no lowest point, and the "Go to the lowest loss" button is off. ② Pick label smoothing (ε = 0.1) and press the button. z₁ goes to 3.92, the loss is 0.372 and p₁ is 0.925. Raise it to 6 and the loss goes back up to 0.46, and to 0.75 at 10. ③ Raise ε to 0.2 and 0.3 and the lowest point moves down to 3.142 and 2.644. The bigger ε, the less sure the model is where it stops. ④ Pick the teacher's probabilities. At the starting score the loss is 0.87, the floor 0.863, the gap 0.007. Press the button and z₁ goes to 2.255, where the prediction (0.7, 0.2, 0.073, 0.027) is almost the teacher's bar.
5. When there can be several answers: multi-label
If one photo can show a cat and a dog together, the question is no longer "which one of the four" but four questions: "Is there a cat? Is there a dog? …". This is multi-label classification. The answer is a bar with 0 or 1 in each slot, (1, 1, 0, 0), and it does not add up to 1. So you do not use softmax, which makes the classes share one total. Instead you put a sigmoid on each class on its own to get "the chance it is there", and add up or average the binary cross-entropy −[y·log p + (1 − y)·log(1 − p)] of each class.
The scores come straight from the first lab. Move the cat score z₁ up there.
Try this. ① At the starting scores (2, 1, 0, −1), softmax adds up to 1, while sigmoid gives 0.881, 0.731, 0.5, 0.269, which add up to 2.381. The classes above 0.5 are cat and dog, and the binary cross-entropy for the answer (1, 1, 0, 0) is 0.362. The fox has a score of 0, so its sigmoid is exactly 0.5. ② Lower the cat score to −1. In softmax the dog rises 0.237 → 0.61, but in sigmoid the dog stays at 0.731. In softmax, when one class drops another takes its share. In sigmoid, the classes do not affect each other. The binary cross-entropy grows to 0.658. ③ Raise it to 5. The softmax dog shrinks to 0.018, but the sigmoid dog is still 0.731, and the loss is 0.332.
6. Things to know
| Property | In plain words | In the lab |
|---|---|---|
| Index = one-hot | With one-hot only the right slot stays in the loss. Passing the number gives the same value | 0.44 in section 3 |
| The slope is p − y | With softmax + cross-entropy, scores move until p reaches y | p₁ = y₁ at the lowest point of the curve |
| One-hot has no end | p never reaches 1, so the right score wants to grow forever. Smoothing stops at a score gap of ln(1 + K·(1 − ε) ÷ ε) | Section 4, try ① and ② |
| The loss floor is the answer's entropy | With a probability bar as the answer the loss never reaches 0. Across different answers, compare the gap left, not the loss | "floor" and "gap left" in the readout |
| Multi-label treats each class on its own | Sigmoids do not affect each other and do not add up to 1. Two right answers, or none, are fine | Section 5, sigmoid sum 2.381 |
7. Which one to use when
| Option | Good | Bad | Use it when |
|---|---|---|---|
| Class index (one-hot) | The simplest. You store the answer as one number. Loss functions take it as is | It pushes the right score up forever, so the model easily gets too sure. A wrong label is trusted as fully as a right one | Plain "pick one" classification. Start here |
| Label smoothing | One setting turns it on. The score gap stops, so the model is less sure. No teacher model needed | You have to pick ε. It treats all wrong classes the same, so it cannot say what looks like what. Predicted probabilities stop near 1 − ε + ε ÷ K | Training a big model with many classes, or when it looks too sure of itself |
| Teacher's probabilities (knowledge distillation) | It learns the size of the wrong classes too, so what looks like what passes to the student | You need a well-trained teacher model and the cost of running it. If the teacher is wrong, the student follows. You have to pick a temperature T | Shrinking a big model into a small, fast one |
| Multi-label (sigmoid + binary cross-entropy) | Several answers, or none, are fine. Each class is judged on its own | Classes do not compete, so it does not fit "pick exactly one" problems. You have to set a cutoff (say 0.5) per class | Several things that can be true at once, like objects in a photo or topic tags on a text |
In distillation, the teacher's and the student's scores are both divided by the same temperature T before softmax. The bigger T is, the flatter the bar, so the differences between wrong classes show up more. This loss is usually mixed with the loss on the original one-hot answer (the distillation paper in the sources).
Where it is used
- Image classification. The answer is stored as a class number, and big models sometimes turn on label smoothing (the Inception paper in the sources).
- A language model picking the next word. The next word's number is the answer index, and K is the size of the vocabulary (Transformer, sampling).
- Making small models. A small model is taught with a big model's probabilities as the answer (knowledge distillation).
- Tagging. Putting every tag that fits on a photo or a text is multi-label.
# PyTorch: pass the raw scores (logits). softmax / sigmoid happen inside the loss
import torch.nn as nn
loss = nn.CrossEntropyLoss()
loss(logits, labels) # answer: class numbers (index)
nn.CrossEntropyLoss(label_smoothing=0.1)(logits, labels) # label smoothing
loss(logits / T, (teacher_logits / T).softmax(dim=1)) # distillation: divide student and teacher scores by the same T
nn.BCEWithLogitsLoss()(logits, multi_hot.float()) # multi-label: 0 or 1 per class
Common questions
Do I have to turn my labels into one-hot vectors?
No. PyTorch's CrossEntropyLoss takes the class numbers as they are, and a one-hot version gives the same loss. The numbers use less memory. You only pass bars when the answer has to be a probability bar (when you build smoothing yourself, or for distillation).
When do I use softmax and when sigmoid?
Use softmax when the model picks one class out of many. Use sigmoid when it answers yes or no for each class on its own. With softmax, when one class goes up the others go down. With sigmoid, the classes do not affect each other. With only two classes, softmax is the same as a sigmoid on the difference of the two scores.
How is label smoothing different from knowledge distillation?
Both give the answer as a probability bar. Label smoothing splits the amount it takes off evenly over every class, so all the wrong classes look the same. In distillation the teacher model gives each class a different amount, so the bar carries facts like "this cat photo looks more like a dog than a rabbit". The cost is that you need a teacher model and have to run it.
Words
| Plain words used here | What books call it |
|---|---|
| the answer's number | class index, hard label |
| a bar with a 1 in one slot | one-hot vector |
| an answer given as a probability bar | soft label, soft target |
| taking some off the answer and sharing it over every class | label smoothing |
| teaching a small model with a big model's probabilities | knowledge distillation. The big model is the teacher, the small one the student |
| the number T the scores are divided by | temperature |
| picking one of the four | multi-class classification |
| is each one there or not | multi-label classification. The answer bar is multi-hot |
| score | logit |
| the floor of the loss | entropy |
| the gap left | KL divergence (Kullback-Leibler divergence) |
| the loss for 0 or 1 per class | binary cross-entropy (BCE) |
| being too sure | overconfidence |
Sources
- Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens, Zbigniew Wojna, Rethinking the Inception Architecture for Computer Vision, arXiv:1512.00567. The label smoothing section: mixing the answer with a flat distribution, and why with one-hot the largest score wants to grow forever.
- Geoffrey Hinton, Oriol Vinyals, Jeff Dean, Distilling the Knowledge in a Neural Network, arXiv:1503.02531. The teacher's probabilities (soft targets), the temperature T, mixing with the loss on the original answer.
- PyTorch docs: CrossEntropyLoss (class numbers or probability bars as the answer,
label_smoothing). - PyTorch docs: BCEWithLogitsLoss (a sigmoid and binary cross-entropy per class, multi-label).