In machine learning, knowledge distillation or model distillation is the process of transferring knowledge from a large model to a smaller one. While large models (such as very deep neural networks or ensembles of many models) have more knowledge capacity than small models, this capacity might not be fully utilized. It can be just as computationally expensive to evaluate a model even if it utilizes little of its knowledge capacity. Knowledge distillation transfers knowledge from a large model to a smaller one with the aim of retaining predictive performance. As smaller models are less expensive to evaluate, they can be deployed on less powerful hardware (such as a mobile device). There is also a less common technique called Reverse Knowledge Distillation, where knowledge is transferred from a smaller model to a larger one. Model distillation is not to be confused with model compression, which describes methods to decrease the size of a large model itself, without training a new model. Model compression generally preserves the architecture and the nominal parameter count of the model, while decreasing the bits-per-parameter. Knowledge distillation has been successfully used in several applications of machine learning such as object detection, acoustic models, medical imaging and natural language processing. Recently, it has also been introduced to graph neural networks applicable to non-grid data.
Methods Knowledge transfer from a large model to a small one somehow needs to teach the latter while preserving as much predictive performance as possible. If both models are trained on the same data, the smaller model may have insufficient capacity to learn a concise knowledge representation compared to the large model. However, some information about a concise knowledge representation is encoded in the pseudolikelihoods assigned to its output: when a model correctly predicts a class, it assigns a large value to the output variable corresponding to such class, and smaller values to the other output variables. The distribution of values among the outputs for a record provides information on how the large model represents knowledge. Therefore, the goal of economical deployment of a valid model can be achieved by training only the large model on the data, exploiting its better ability to learn concise knowledge representations, and then distilling such knowledge into the smaller model, by training it to learn the soft output of the large model.
Mathematical formulation Given a large model as a function of the vector variable 𝐱, trained for a specific classification task, typically the final layer of classification networks is a softmax in the form
yi(𝐱|t)=ezi(𝐱)t∑jezj(𝐱)t where t is the temperature, a parameter which is set to 1 for a standard softmax. The softmax operator converts the logit values zi(𝐱) to pseudo-probabilities: higher temperature values generate softer distributions of pseudo-probabilities among the output classes. Knowledge distillation consists of training a smaller network, called the distilled model, on a data set called the transfer set which could correspond to the original training set or consist of new, possibly unlabeled data. A cross-entropy loss function is typically used, computed between the output of the distilled model 𝐲(𝐱|t) and the output of the large model 𝐲̂(𝐱|t) on the same record (or the average of the individual outputs, if the large model is an ensemble), using a high value of softmax temperature t for both models:
E(𝐱|t)=−∑iŷi(𝐱|t)logyi(𝐱|t). In this context, a high temperature increases the entropy of the output, therefore providing more information to learn for the distilled model compared to hard targets, and at the same time reducing the variance of the gradient between different records, thus allowing a higher learning rate. If ground truth is available for the transfer set, the process can be strengthened by adding to the loss the cross-entropy between the output yi(𝐱|1) of the distilled model computed with t=1, and the known label y¯i
E(𝐱|t)=−t2∑iŷi(𝐱|t)logyi(𝐱|t)−∑iy¯ilogyi(𝐱|1) where the component of the loss with respect to the large model is weighted by a factor of t2 since, as the temperature increases, the gradient of the loss with respect to the model weights scales by a factor of 1t2.
Relationship with model compression Under the assumption that the logits have zero mean, it is possible to show that model compression is a special case of knowledge distillation. The gradient of the knowledge distillation loss E with respect to the logit of the distilled model zi is given by
∂∂ziE=−∂∂zi∑jŷjlogyj=−∂∂ziŷilogyi+(−∂∂zi∑k≠iŷklogyk)=−ŷi1yi∂∂ziyi+∑k≠i(−ŷk⋅1yk⋅ezkt⋅(−1(∑jezjt)2)⋅ezit⋅1t)=−ŷi1yi∂∂ziezit∑jezjt+∑k≠i(ŷk⋅1yk⋅yk⋅yi⋅1t)=−ŷi1yi(1tezit∑jezjt−1t(ezit)2(∑jezjt)2)+yi∑k≠iŷkt=−ŷi1yi(yit−yi2t)+yi(1−ŷi)t=1t(yi−ŷi)=1t(ezit∑jezjt−eẑit∑jeẑjt) where ẑi are the logits of the large model. For large values of t this can be approximated as
1t(1+zitN+∑jzjt−1+ẑitN+∑jẑjt) and under the zero-mean hypothesis ∑jzj=∑jẑj=0 it becomes zi−ẑiNT2, which is the derivative of 12(zi−ẑi)2, i.e. the loss is equivalent to matching the logits of the two models, as done in model compression.
"Optimal Brain Damage" algorithm The Optimal Brain Damage (OBD) algorithm is as follows:
Do until a desired level of sparsity or performance is reached: Train the network (by methods such as backpropagation) until a reasonable solution is obtained Compute the saliencies for each parameter Delete some lowest-saliency parameters Deleting a parameter means fixing the parameter to zero. The "saliency" of a parameter θ is defined as 12(∂θ2L)θ2, where L is the loss function. The second-derivative ∂θ2L can be computed by second-order backpropagation. The idea for optimal brain damage is to approximate the loss function in a neighborhood of optimal parameter θ∗ by Taylor expansion:L(θ)≈L(θ∗)+12∑i(∂θi2L(θ∗))(θi−θi∗)2where ∇L(θ∗)≈0, since θ∗ is optimal, and the cross-derivatives ∂θi∂θjL are neglected to save compute. Thus, the saliency of a parameter approximates the increase in loss if that parameter is deleted.
… excerpt ends here. Continue reading the full article.