ai.si
Field notes

From AI to SI

What Can a Smaller Model Learn From a Larger Teacher?

Bottom line

Two handwritten nines can have the same top label while one resembles an eight and the other a four. A teacher’s probability distribution exposes that distinction to the student. Held-out labels tell you whether learning it helped.

Examples feed a model, its outputs pass through a check, and selected material loops back to the examples.

In a learning loop, follow the information that changes the next attempt and identify what checks it.

A smaller model can learn the relative probabilities its teacher assigns to several plausible classes. How much more will depend on the transfer data, what the teacher reveals, and how much the student is capable of learning. All of these are illustrated in Hinton et al.'s paper on knowledge distillation, applied to a number of simple classification problems, one of which is recognition of handwritten digits. The result depends on what the student actually saw during transfer. Distilling the Knowledge in a Neural Network

If the teacher were asked to identify the digit shown in 2 badly reproduced images of handwriting, both of which in fact show a nine, it might attach rather different probabilities to other digits. In one case the bottom may be almost closed, tending towards 8; in the other there may be a long descending diagonal to the left, tending towards 4. So labelling each with "9" conveys the same information to the student in both cases, but the probabilities attached to other digits would convey additional and different information. These probabilities are the soft targets.

What the second-most-likely class tells the student

Take 3 digits as all the classes, and suppose that, in the order 9, 8, 4, the teacher returns 0.80, 0.18, 0.02 for one of the above examples, and 0.80, 0.02, 0.18 for the other. So most likely class and associated probability are unchanged. If only the top label and its probability are retained, the two examples have identical targets. With the complete set of probabilities, however, there is an obvious difference in 2nd most likely. If the student returns 0.80, 0.10, 0.10 in the same order of classes for both, an error function based only on the probability of the true class rewards increasing that probability but gives the same signal on both examples about which runner-up should gain. Minimizing the difference from the teacher's answers, on the other hand, would encourage increased probability for 8 rather than 4 in the first case, and vice versa. So there is an incentive to alter answers in appropriate directions, despite already giving most probability to the true class.

These figures are contrived to make the point, and illustrate the mechanism. Nor is it important that the probabilities returned by the teacher should correspond exactly to proportions of other digits to which a person would be tempted if asked to identify each. To the extent that the probabilities reflect salient characteristics of the image, it seems likely that learning to reproduce them would be beneficial. If the teacher makes a systematic mistake, copying it can improve agreement while reducing accuracy.

Role of "temperature"

A classifier can compute scores, called logits, for its classes and apply a softmax function to obtain probabilities. For distillation each logit is divided by the same positive temperature before softmax. This changes the probabilities while preserving the ordering. Raising the temperature thus "softens" the probabilities returned by the teacher, as desired for the above discussion. So classes with only a small margin from each other, and small probability compared with that of the top class, can be more clearly distinguished. Hinton et al., section 2

In the paper, the student applies the same temperature to its outputs to minimize the difference. In addition, where the true class is known, a term may be added to require the student to return that class (at "normal" temperature). The relative weightings of the 2 terms will be significant. To some extent the true class term will ensure that probability remains high for the correct answer, even if other aspects of the distribution cannot be closely matched. At test time the student is expected to use temperature 1. Hinton et al., objective function for distillation

This is quite different from raising the temperature at which samples may be taken from the final model. If 1 result only is required and taken at a raised temperature, that still only provides 1 class. Providing all the probabilities tells the student about all other classes at once. Of course taking many samples would also give some indication of the relative probabilities, but the number required could be very large if some are unlikely. And of course 1 sample tells nothing about the others.

What happens when threes are missing from the transfer set?

One experiment removed every image of a three from the student’s transfer set. Presumably the teacher would return some non-zero probability for 3 for some other digits. So the student would be exposed to 3 fairly frequently, although never with a hard target. This is significant when considering the result. The transfer set contained no images of threes, while soft targets for other digits could still supply information about the three class.

Of 206 total errors returned at test time, 133 were for 3, of 1,010 total. So 877 3's were correctly identified, 86.8%. By changing a constant bias applied to all outputs for 3, the number of errors on 3 was reduced dramatically to 14. Since this constant was chosen to maximize overall accuracy on the test data, this should be viewed simply as showing the possible benefit of such a change, and requires an independent test for confirmation. Hinton et al., omitted-three experiment

Intuitively one would expect the student to under-estimate 3 generally, since there are no examples to which it must return 3. But it may well have learned various features such that most actual 3's are much closer to 3 than to most other digits. It would simply require more other evidence to tip it over to 3. Ideally one would determine an appropriate constant on further validation data, and then test on new data.

What will the student be able to learn?

Clearly it can only learn to reproduce aspects of the teacher's behaviour which arise in the examples given for transfer. The teacher may be able to return a sensible answer to images in a style with which it is not familiar, but unless there are similar images in the transfer data the student may receive little or no signal to learn to do so. On the other hand a good selection of (unlabeled) images may cause the teacher to reveal a great deal of useful information. The paper considers unlabeled transfer examples and reuse of the original training set. Hinton et al., section 2

As an example, assume one wished to read both type and a variety of "messy" hand-written figures. If most of the transfer images were well printed figures, one might expect very close agreement between student and teacher on many images. But this would be poor guidance as to performance on very dirty images. It is clearly desirable to test over the full range expected. So compare teacher, student trained to hard targets only, and student trained with soft targets, applying identical processing and decision rule. If the teacher does badly on some images, it may be pointless trying to reproduce its behaviour.

Alongside comparisons of accuracy it would also be useful to compare the size of the models, speed of response required on the relevant machine, and overall cost to provide the service. One motivation for distillation was to avoid running an entire ensemble to obtain an answer. So size of model will be important, but speed and cost on realistic distributions of inputs will also be relevant. Depending on the application, a small number of particular types of error may be very costly (for example requiring human intervention) and so be more significant than a general small increase in speed.

So it would be useful to train a number of models of comparable size to various targets: true class; most likely class according to teacher; complete set of probabilities returned by teacher. All would be exposed to the same set of images. Superiority of the last would then not be attributable simply to a wider variety of training images. A separate comparison could add unlabeled cases with the teacher’s predictions and measure the value of that additional material. It would also be relevant to note whether outputs from the teacher had to be specifically obtained for the purpose, or could be taken from some existing store.

Agreement with teacher

It seems useful to tabulate, for new images, the answer returned by teacher, that returned by student, and (where available) the true class. Then one can identify cases where student and teacher agree but are both wrong, and so the student has been very successful at distillation, although the result may be useless. One can also see where student differs from teacher and is correct. So a simple measure of agreement would be misleading. Or where both return the correct answer but very different probabilities, so accuracy of top class would fail to measure the desired property. So various measures may be relevant, depending on the property of interest.

Where there is interest in a small number of classes it would be useful to know how many instances occur in the transfer data, how many in the test data, and how many in any data used to optimize subsequent processing. So there are various ways in which the student may be exposed to 3 in the above example. As noted above, a fair number of other digits may cause a substantial probability to be attached to 3, so it may learn quite a lot about 3 from other images, which would not be apparent if all targets were "hard".

Correspondingly, if "distilling" from a LM, there is a large difference between exposing the student only to examples of completed answers produced by the teacher, and providing the full range of probabilities it attaches to each possible next token. The former may teach a great deal about "style" and approaches to various questions. So one needs to be clear what information was provided by the "teacher" (hard labels, range of probabilities, 1 or more sampled results, comments on answers produced by the student, ...) before transferring conclusions from 1 situation to another.

So given a claim that a student has benefited significantly from a much larger "teacher", it would be good to know what the 2 nines above might have looked like. What features did the teacher distinguish between them? Would the student have been able to detect the difference from its targets? And what properties, other than returning the most likely answer of the teacher, would be measured and found desirable in testing? Ideally one would know what data was available to the student to learn from, how it was subsequently tested, and the reduction in resources required now that the teacher is no longer needed.

References

All field notes