TL;DR
Hint-based training teaches a small student network by asking one of its middle layers to predict the output of a middle layer of a trained teacher, in addition to matching the teacher's final answers. A learned adapter lets the student's narrower layer be compared with the teacher's wider one. This gives a student that is deeper and thinner than its teacher a concrete target early in training, where the final answers alone would not train it well.
Builds on: Knowledge Distillation, Backpropagation, Mean Squared Error · Leads to: FitNets: Hints for Thin Deep Nets
Where it's used
Shrinking an image classifier. In the FitNets paper (Romero and colleagues, 2014), the teacher on CIFAR-10 was a network with about 9 million parameters and 90.18% test accuracy. A deeper, thinner student with about 2.5 million parameters reached 91.61%. The student was smaller than its teacher and more accurate, which is the result that made the method worth knowing.
Training a very deep network. Deep networks are hard to train because the learning signal has to pass backwards through many non-linear layers before it reaches the early ones. Hints give the early and middle layers a target of their own, so the signal does not have to travel all the way from the final answer. Teams that want a deep model but cannot afford its size at run time use this to train the deep version with help from a wider teacher.
Choosing a student shape for speed. A deep but narrow student can run faster or use less memory than a wide teacher. The paper frames the choice as a trade-off: the student's capacity decides whether it generalises better or runs faster. Hints make the deep, narrow shape trainable in the first place.
How it works
Start with one teacher, which is already trained, and one student, which starts from random weights. Pick one middle layer of each. The teacher's middle layer output for an input is the hint. The student's chosen middle layer is the guided layer. The student's guided layer and the teacher's hint usually have different sizes, so a regressor, a small extra layer on top of the guided layer, maps the guided output to the same size as the hint. Then the two can be subtracted. This walkthrough uses one input and tiny numbers.
-
The teacher produces the hint. Show the input to the teacher. Its middle layer outputs two numbers, . This is the target the student's middle layer should learn to predict. Check: the hint has two numbers, and the second one is zero.
-
The student produces its guided output. Show the same input to the student. Its guided layer is narrower and outputs one number, . Check: one number, not a pair.
-
The regressor widens the guided output. The regressor has two weights, and , one for each position in the hint. It multiplies the guided output by each weight and gets . Check: the regressor output now has the same length as the hint.
-
Measure the gap. Subtract the regressor output from the hint: . Square each gap, add the squares, and halve the sum: . This number is the hint loss. Check: it is zero only when the regressor output equals the hint exactly.
-
Move the student to close the gap. Work out how the loss changes when changes. The answer is , a positive number, so making smaller lowers the loss. With a learning rate of , the update gives . Now the gap is and the loss is . Check: the loss fell from 5 to 2.6. For this step the regressor weights are held fixed so the arithmetic stays small.
-
Second stage: match the teacher's outputs. Once the guided layer has been trained this way, the whole student is trained with ordinary knowledge distillation, starting from those weights. The regressor is used only in the first stage. The finished model is the student's own layers. Check: nothing from the regressor is in the final model.
flowchart LR T["Trained teacher"] --> H["Hint: middle layer output"] S["Student: deeper and thinner"] --> G["Guided: middle layer output"] G --> R["Regressor: maps to hint size"] R --> C["Compare: half the squared gap"] H --> C C --> U["Update student layers up to the guided layer"] U --> K["Stage two: train whole student with distillation"]
The curve below shows the hint loss for the single guided output in step 4 and step 5, with the regressor weights fixed. The lowest point is at . The two dots mark the start, with loss 5, and the point after one update, with loss 2.6.
- hint loss L(v)
The math
Let be the teacher's hint for an input, a vector of numbers, and let be the student's guided output, a vector of numbers. Let be the regressor. The hint loss is
the half squared distance between the hint and the regressed guided output. Training makes this loss small by changing the student's weights up to the guided layer and the regressor's weights.
Step 1 in symbols: the hint
The hint is the teacher's hidden layer output. In the example it is , so . The teacher's own weights are frozen; they are only read.
Step 2 in symbols: the guided output and the regressor
The guided output is . In the example and . The regressor is a linear map with a weight matrix of size , so . In the example is the column and .
The paper uses a convolutional regressor instead of a full matrix, because a full matrix becomes very large when both layers are convolutional. A full matrix would need weights, where and are the spatial size and channel count of the hint, and similarly for the guided layer. A convolutional regressor with a kernel of size needs only weights, and its kernel is chosen so its output has the same spatial size as the hint.
Step 3 in symbols: the loss value
Subtract and square. The loss is .
Step 4 in symbols: the gradient and the update
For a single guided value, the chain rule (see Chain Rule) gives
where is the regressor weight for hint position . In the example this is . Gradient descent (see Gradient Descent) then updates the guided output's weights in the direction that lowers the loss:
with learning rate . With this gives . In a real network the same rule is applied to every weight in the student up to the guided layer, and Backpropagation computes all those gradients at once.
Step 5 in symbols: the two stages
Stage one solves
which trains the student up to its guided layer and the regressor. Stage two starts from those student weights and minimises the Knowledge Distillation loss over all of the student's weights, so the last layers are trained on the teacher's softened outputs and the true labels.
Worked examples
- Setup: hint , guided output , regressor weights .
- Regressor output: .
- Gap: .
- Loss: .
- Gradient with respect to : .
- One update with learning rate : .
- Loss at the new value: , gap , loss .
The loss fell from 5 to 2.6 in one small step. This is what stage one does, repeated over many inputs and many steps.
- Setup: guided output with two units. Hint with three units. The regressor matrix has rows , and , so it is .
- Regressor output, row by row: , then , then . So .
- Gap: .
- Loss: .
- Gradient with respect to is . The transpose has rows and . Applied to the gap, the first entry is and the second is . Negating gives .
- One update with learning rate : .
- Check the new loss: , gap , loss .
The loss fell from 2.5 to 1.63. The same rule works with any number of units: the regressor carries the gradient from hint space back to the guided layer.
- Setup from the FitNets paper: the teacher has about 9 million parameters and reaches 90.18% test accuracy. The FitNet has about 2.5 million parameters and reaches 91.61%. Its 11th layer was trained to mimic the teacher's 2nd layer.
- Size ratio: . The student has under a third of the teacher's parameters, which matches the paper's phrase "roughly ".
- Accuracy gain over the teacher: percentage points.
- The paper also reports a 19-layer network trained with labelled hints at every layer, using deep supervision, which reached 88.2%. Compared with that, the gap is points.
Try it: Table 1 of the paper lists the Deeply-Supervised Networks result as 91.78%. How many percentage points is this above the FitNet's 91.61%? Answer: points, so the FitNet is close to the best result in that table.
Common mistakes and tradeoffs
It is tempting to think a hint is just the teacher's final output under another name. In fact a hint is the output of an intermediate layer, because that layer carries features the output layer later combines. Knowledge distillation matches the final probabilities; hint-based training matches the features that produce them.
It is tempting to think that any intermediate layer works, and that a deeper one gives more guidance. In fact the choice of layer pair is a design decision. The paper's CIFAR-10 FitNet hints its 11th layer with the teacher's 2nd layer, a choice made for that architecture, not a general rule.
It is tempting to think the regressor is part of the final model. In fact it is used only in stage one, and the output of the training procedure is the student's own weights. The regressor exists to make the two layers the same size and can be thrown away.
It is tempting to think that hinting with the true labels does the same job. In fact, in the paper's experiments, supervised hints at middle layers, combined with classification targets, never got the thin, deep student to learn under any setting they tried. The authors suggest that very discriminative hints at intermediate layers are too aggressive, while a teacher's smoother hint works. A 19-layer network trained with labelled hints at every layer reached 88.2%, against 91.61% for the FitNet.
Hint-based training is worth its cost when the student is deeper and thinner than the teacher and you want its internal features to follow the teacher's. It costs a trained teacher, two training stages and a regressor. It is not needed when the student has the same shape as the teacher, because ordinary Knowledge Distillation is enough. Alternatives include Deep Supervision, which adds labelled losses at middle layers, and Curriculum Learning, which the paper argues this method resembles, because the teacher's confidence tells the student which examples are easy.
History
Training very deep networks was hard before 2014. In 2013, Gulcehre and Bengio showed that guidance hints on an intermediate layer could ease training in a curriculum learning setting, but their hints needed prior knowledge of the task. The FitNets paper's authors pointed out that a teacher network could supply such hints without that knowledge.
Knowledge distillation, training a small model to imitate a large one's outputs, had been studied before 2014. Hinton, Vinyals and Dean's 2015 paper made the idea widely known. The FitNets paper extended it from outputs to intermediate features, and was posted as arXiv preprint 1412.6550 in 2014.
The method caught on because the recipe is simple to state and the result was striking: a deeper student with fewer parameters beat its own teacher on CIFAR-10. Many later distillation methods match intermediate features in similar ways.
Recap
- A hint is the output of a middle layer of a trained teacher.
- The student's guided layer and a small regressor are trained so the regressor's output matches the hint. The loss is half the squared gap.
- The gradient of that loss tells the update which way to move the guided layer.
- Stage two then trains the whole student with knowledge distillation.
- The result is a deep, thin student that can match or beat a wider teacher with fewer parameters.
Read next: FitNets: Hints for Thin Deep Nets for the full method and experiments, then Deep Supervision for the labelled alternative.
Check yourself
QuestionIn hint-based training, what is the hint, and where does it come from?Show answer
QuestionThe teacher's hint is (2, 0). The student's guided output is v = 3, and the regressor weights are (1, 3). What is the hint loss? Give the number.Show answer
QuestionWhy does the method need a regressor at all?Show answer
QuestionThe paper reports that hinting a deep student with the true labels did not train it well. Why does a teacher's hidden layer work better as a hint?Show answer
QuestionYou have a student with exactly the same architecture as its teacher. Should you use hint-based training?Show answer
Sources
- Romero et al., "FitNets: Hints for Thin Deep Nets" (2014), arXiv: https://arxiv.org/abs/1412.6550
- Hinton, Vinyals and Dean, "Distilling the Knowledge in a Neural Network" (2015), arXiv: https://arxiv.org/abs/1503.02531
- Knowledge distillation, Wikipedia: https://en.wikipedia.org/wiki/Knowledge_distillation