Search

Search papers and lessons

Hint-Based Training

Level 4 · 13 min read

Hint-based training teaches a deep, thin student to copy a middle layer of a trained teacher, not only its final answers, so the student gets a clear target early in training.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Probability
  • Linear algebra
  • Losses & regularization
  • ReadReady to readNot yet

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.

  1. The teacher produces the hint. Show the input to the teacher. Its middle layer outputs two numbers, (2,0)(2, 0). 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.

  2. The student produces its guided output. Show the same input to the student. Its guided layer is narrower and outputs one number, v=1v = 1. Check: one number, not a pair.

  3. The regressor widens the guided output. The regressor has two weights, 11 and 33, one for each position in the hint. It multiplies the guided output by each weight and gets (1,3)(1, 3). Check: the regressor output now has the same length as the hint.

  4. Measure the gap. Subtract the regressor output from the hint: (2,0)−(1,3)=(1,−3)(2, 0) - (1, 3) = (1, -3). Square each gap, add the squares, and halve the sum: 12(12+32)=5\frac12 (1^2 + 3^2) = 5. This number is the hint loss. Check: it is zero only when the regressor output equals the hint exactly.

  5. Move the student to close the gap. Work out how the loss changes when vv changes. The answer is 88, a positive number, so making vv smaller lowers the loss. With a learning rate of 0.050.05, the update gives v=1−0.05⋅8=0.6v = 1 - 0.05 \cdot 8 = 0.6. Now the gap is (2,0)−(0.6,1.8)=(1.4,−1.8)(2, 0) - (0.6, 1.8) = (1.4, -1.8) and the loss is 2.62.6. Check: the loss fell from 5 to 2.6. For this step the regressor weights are held fixed so the arithmetic stays small.

  6. 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 vv in step 4 and step 5, with the regressor weights fixed. The lowest point is at v=0.2v = 0.2. The two dots mark the start, v=1v = 1 with loss 5, and the point after one update, v=0.6v = 0.6 with loss 2.6.

−0.500.511.50123456guided output vhint loss
  • hint loss L(v)
The hint loss against the one guided output. One update moves the student from v = 1 down to v = 0.6 and lowers the loss from 5 to 2.6.

The math

Definition: Hint loss

Let u\mathbf{u} be the teacher's hint for an input, a vector of hh numbers, and let v\mathbf{v} be the student's guided output, a vector of gg numbers. Let rr be the regressor. The hint loss is

LHT=12∥u−r(v)∥2,\mathcal{L}_{\text{HT}} = \frac{1}{2} \lVert \mathbf{u} - r(\mathbf{v}) \rVert^2,

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 u=(2,0)\mathbf{u} = (2, 0), so h=2h = 2. 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 v=(v1,…,vg)\mathbf{v} = (v_1, \dots, v_g). In the example g=1g = 1 and v=1\mathbf{v} = 1. The regressor is a linear map with a weight matrix Wr\mathbf{W}_r of size h×gh \times g, so r(v)=Wrvr(\mathbf{v}) = \mathbf{W}_r \mathbf{v}. In the example Wr\mathbf{W}_r is the column (1,3)(1, 3) and r(1)=(1,3)r(1) = (1, 3).

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 Nh,1×Nh,2×Oh×Ng,1×Ng,2×OgN_{h,1} \times N_{h,2} \times O_h \times N_{g,1} \times N_{g,2} \times O_g weights, where Nh,1×Nh,2N_{h,1} \times N_{h,2} and OhO_h are the spatial size and channel count of the hint, and similarly for the guided layer. A convolutional regressor with a kernel of size k1×k2k_1 \times k_2 needs only k1×k2×Oh×Ogk_1 \times k_2 \times O_h \times O_g 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 12∥u−r(v)∥2=12(12+(−3)2)=12⋅10=5\frac12 \lVert \mathbf{u} - r(\mathbf{v}) \rVert^2 = \frac12 (1^2 + (-3)^2) = \frac12 \cdot 10 = 5.

Step 4 in symbols: the gradient and the update

For a single guided value, the chain rule (see Chain Rule) gives

∂LHT∂v=−∑j(uj−rj) wj,\frac{\partial \mathcal{L}_{\text{HT}}}{\partial v} = -\sum_{j} (u_j - r_j)\, w_j,

where wjw_j is the regressor weight for hint position jj. In the example this is −(1⋅1+(−3)⋅3)=8-(1 \cdot 1 + (-3) \cdot 3) = 8. Gradient descent (see Gradient Descent) then updates the guided output's weights in the direction that lowers the loss:

v←v−η∂LHT∂v,v \leftarrow v - \eta \frac{\partial \mathcal{L}_{\text{HT}}}{\partial v},

with learning rate η\eta. With η=0.05\eta = 0.05 this gives v=1−0.05⋅8=0.6v = 1 - 0.05 \cdot 8 = 0.6. 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

WGuided∗,Wr∗=arg⁡min⁡WGuided,WrLHT,\mathbf{W}_{\text{Guided}}^{*}, \mathbf{W}_r^{*} = \arg\min_{\mathbf{W}_{\text{Guided}}, \mathbf{W}_r} \mathcal{L}_{\text{HT}},

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

Example
1. One hint value, by hand
  1. Setup: hint u=(2,0)\mathbf{u} = (2, 0), guided output v=1v = 1, regressor weights (1,3)(1, 3).
  2. Regressor output: r=(1⋅1,3⋅1)=(1,3)r = (1 \cdot 1, 3 \cdot 1) = (1, 3).
  3. Gap: (2−1,  0−3)=(1,−3)(2 - 1, \; 0 - 3) = (1, -3).
  4. Loss: 12(12+(−3)2)=12⋅10=5\frac12 (1^2 + (-3)^2) = \frac12 \cdot 10 = 5.
  5. Gradient with respect to vv: −(1⋅1+(−3)⋅3)=8-(1 \cdot 1 + (-3) \cdot 3) = 8.
  6. One update with learning rate 0.050.05: v=1−0.05⋅8=0.6v = 1 - 0.05 \cdot 8 = 0.6.
  7. Loss at the new value: r=(0.6,1.8)r = (0.6, 1.8), gap (1.4,−1.8)(1.4, -1.8), loss 12(1.96+3.24)=2.6\frac12 (1.96 + 3.24) = 2.6.

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.

Example
2. Several guided units
  1. Setup: guided output v=(1,2)\mathbf{v} = (1, 2) with two units. Hint u=(1,0,2)\mathbf{u} = (1, 0, 2) with three units. The regressor matrix has rows (1,0)(1, 0), (0,1)(0, 1) and (1,1)(1, 1), so it is 3×23 \times 2.
  2. Regressor output, row by row: 1⋅1+0⋅2=11 \cdot 1 + 0 \cdot 2 = 1, then 0⋅1+1⋅2=20 \cdot 1 + 1 \cdot 2 = 2, then 1⋅1+1⋅2=31 \cdot 1 + 1 \cdot 2 = 3. So r=(1,2,3)\mathbf{r} = (1, 2, 3).
  3. Gap: u−r=(0,−2,−1)\mathbf{u} - \mathbf{r} = (0, -2, -1).
  4. Loss: 12(02+22+12)=12⋅5=2.5\frac12 (0^2 + 2^2 + 1^2) = \frac12 \cdot 5 = 2.5.
  5. Gradient with respect to v\mathbf{v} is −WrT(u−r)-\mathbf{W}_r^{\mathsf{T}} (\mathbf{u} - \mathbf{r}). The transpose has rows (1,0,1)(1, 0, 1) and (0,1,1)(0, 1, 1). Applied to the gap, the first entry is 1⋅0+0⋅(−2)+1⋅(−1)=−11 \cdot 0 + 0 \cdot (-2) + 1 \cdot (-1) = -1 and the second is 0⋅0+1⋅(−2)+1⋅(−1)=−30 \cdot 0 + 1 \cdot (-2) + 1 \cdot (-1) = -3. Negating gives (1,3)(1, 3).
  6. One update with learning rate 0.10.1: v=(1−0.1,  2−0.3)=(0.9,1.7)\mathbf{v} = (1 - 0.1, \; 2 - 0.3) = (0.9, 1.7).
  7. Check the new loss: r=(0.9,1.7,2.6)\mathbf{r} = (0.9, 1.7, 2.6), gap (0.1,−1.7,−0.6)(0.1, -1.7, -0.6), loss 12(0.01+2.89+0.36)=1.63\frac12 (0.01 + 2.89 + 0.36) = 1.63.

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.

Example
3. A realistic CIFAR-10 comparison
  1. 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.
  2. Size ratio: 2.5/9≈0.282.5 / 9 \approx 0.28. The student has under a third of the teacher's parameters, which matches the paper's phrase "roughly 1/31/3".
  3. Accuracy gain over the teacher: 91.61−90.18=1.4391.61 - 90.18 = 1.43 percentage points.
  4. 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 91.61−88.2=3.4191.61 - 88.2 = 3.41 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: 91.78−91.61=0.1791.78 - 91.61 = 0.17 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
The hint is the output of a middle (hidden) layer of the trained teacher network, for the same input the student sees. It is the target that the student's guided layer learns to predict, after the regressor maps the guided output to the hint's size.
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
The regressor output is (3, 9). The gap is (2 - 3, 0 - 9) = (-1, -9). The squares are 1 and 81, which sum to 82. Half of that is 41. Answer: 41.
QuestionWhy does the method need a regressor at all?Show answer
The guided layer of a thin student is usually narrower than the teacher's hint layer, so the two outputs have different sizes and cannot be subtracted. The regressor maps the guided output to the hint's size. The paper uses a convolutional regressor so this mapping does not need a huge number of weights.
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
A teacher's hidden layer gives a smoother target than a label that is either right or wrong. The paper suggests that very discriminative targets at a middle layer are too aggressive, and a teacher's representation generalises better. A 19-layer network trained with labelled hints at every layer reached 88.2% on CIFAR-10, against 91.61% for the teacher-hinted FitNet.
QuestionYou have a student with exactly the same architecture as its teacher. Should you use hint-based training?Show answer
Usually no. The method is meant for a student that is deeper and thinner than its teacher, where the hint helps with training. For a student of the same shape, ordinary knowledge distillation on the outputs is simpler and is usually enough.

Sources