Sequence-Level Knowledge Distillation

Yoon Kim, Alexander M. Rush

Introduction

Neural machine translation (NMT) [Kalchbrenner and Blunsom, 2013, Cho et al., 2014, Sutskever et al., 2014, Bahdanau et al., 2015] is a deep learning-based method for translation that has recently shown promising results as an alternative to statistical approaches. NMT systems directly model the probability of the next word in the target sentence simply by conditioning a recurrent neural network on the source sentence and previously generated target words.

While both simple and surprisingly accurate, NMT systems typically need to have very high capacity in order to perform well: ?) used a 44-layer LSTM with 10001000 hidden units per layer (herein 4×10004\times 1000) and ?) obtained state-of-the-art results on English →\rightarrow French with a 1616-layer LSTM with 512512 units per layer. The sheer size of the models requires cutting-edge hardware for training and makes using the models on standard setups very challenging.

This issue of excessively large networks has been observed in several other domains, with much focus on fully-connected and convolutional networks for multi-class classification. Researchers have particularly noted that large networks seem to be necessary for training, but learn redundant representations in the process [Denil et al., 2013]. Therefore compressing deep models into smaller networks has been an active area of research. As deep learning systems obtain better results on NLP tasks, compression also becomes an important practical issue with applications such as running deep learning models for speech and translation locally on cell phones.

Existing compression methods generally fall into two categories: (1) pruning and (2) knowledge distillation. Pruning methods [LeCun et al., 1990, He et al., 2014, Han et al., 2016], zero-out weights or entire neurons based on an importance criterion: ?) use (a diagonal approximation to) the Hessian to identify weights whose removal minimally impacts the objective function, while ?) remove weights based on thresholding their absolute values. Knowledge distillation approaches [Bucila et al., 2006, Ba and Caruana, 2014, Hinton et al., 2015] learn a smaller student network to mimic the original teacher network by minimizing the loss (typically L2L_{2} or cross-entropy) between the student and teacher output.

In this work, we investigate knowledge distillation in the context of neural machine translation. We note that NMT differs from previous work which has mainly explored non-recurrent models in the multi-class prediction setting. For NMT, while the model is trained on multi-class prediction at the word-level, it is tasked with predicting complete sequence outputs conditioned on previous decisions. With this difference in mind, we experiment with standard knowledge distillation for NMT and also propose two new versions of the approach that attempt to approximately match the sequence-level (as opposed to word-level) distribution of the teacher network. This sequence-level approximation leads to a simple training procedure wherein the student network is trained on a newly generated dataset that is the result of running beam search with the teacher network.

We run experiments to compress a large state-of-the-art 4×10004\times 1000 LSTM model, and find that with sequence-level knowledge distillation we are able to learn a 2×5002\times 500 LSTM that roughly matches the performance of the full system. We see similar results compressing a 2×5002\times 500 model down to 2×1002\times 100 on a smaller data set. Furthermore, we observe that our proposed approach has other benefits, such as not requiring any beam search at test-time. As a result we are able to perform greedy decoding on the 2×5002\times 500 model 1010 times faster than beam search on the 4×10004\times 1000 model with comparable performance. Our student models can even be run efficiently on a standard smartphone.https://github.com/harvardnlp/nmt-android Finally, we apply weight pruning on top of the student network to obtain a model that has 13×13\times fewer parameters than the original teacher model. We have released all the code for the models described in this paper.https://github.com/harvardnlp/seq2seq-attn

Background

Let s=[s1,…,sI]\mathbf{s}=[s_{1},\dots,s_{I}] and t=[t1,…,tJ]\mathbf{t}=[t_{1},\dots,t_{J}] be (random variable sequences representing) the source/target sentence, with II and JJ respectively being the source/target lengths. Machine translation involves finding the most probable target sentence given the source:

where T\mathcal{T} is the set of all possible sequences. NMT models parameterize p(t ∣ s)p(\mathbf{t}\,|\,\mathbf{s}) with an encoder neural network which reads the source sentence and a decoder neural network which produces a distribution over the target sentence (one word at a time) given the source. We employ the attentional architecture from ?), which achieved state-of-the-art results on English →\rightarrow German translation.Specifically, we use the global-general attention model with the input-feeding approach. We refer the reader to the original paper for further details.

2 Knowledge Distillation

Knowledge distillation describes a class of methods for training a smaller student network to perform better by learning from a larger teacher network (in addition to learning from the training data set). We generally assume that the teacher has previously been trained, and that we are estimating parameters for the student. Knowledge distillation suggests training by matching the student’s predictions to the teacher’s predictions. For classification this usually means matching the probabilities either via L2L_{2} on the log⁡\log scale [Ba and Caruana, 2014] or by cross-entropy [Li et al., 2014, Hinton et al., 2015].

Concretely, assume we are learning a multi-class classifier over a data set of examples of the form (x,y)(x,y) with possible classes V\mathcal{V}. The usual training criteria is to minimize NLL for each example from the training data,

In knowledge distillation, we assume access to a learned teacher distribution q(y ∣ x;θT)q(y\,|\,x;\theta_{T}), possibly trained over the same data set. Instead of minimizing cross-entropy with the observed data, we instead minimize the cross-entropy with the teacher’s probability distribution,

Since this new objective has no direct term for the training data, it is common practice to interpolate between the two losses,

where α\alpha is mixture parameter combining the one-hot distribution and the teacher distribution.

Knowledge Distillation for NMT

The large sizes of neural machine translation systems make them an ideal candidate for knowledge distillation approaches. In this section we explore three different ways this technique can be applied to NMT.

NMT systems are trained directly to minimize word NLL, LWORD-NLL\mathcal{L}_{\text{WORD-NLL}}, at each position. Therefore if we have a teacher model, standard knowledge distillation for multi-class cross-entropy can be applied. We define this distillation for a sentence as,

where V\mathcal{V} is the target vocabulary set. The student can further be trained to optimize the mixture of LWORD-KD\mathcal{L}_{\text{WORD-KD}} and LWORD-NLL\mathcal{L}_{\text{WORD-NLL}}. In the context of NMT, we refer to this approach as word-level knowledge distillation and illustrate this in Figure 1 (left).

2 Sequence-Level Knowledge Distillation

Word-level knowledge distillation allows transfer of these local word distributions. Ideally however, we would like the student model to mimic the teacher’s actions at the sequence-level. The sequence distribution is particularly important for NMT, because wrong predictions can propagate forward at test-time.

First, consider the sequence-level distribution specified by the model over all possible sequences t∈T\mathbf{t}\in\mathcal{T},

for any length JJ. The sequence-level negative log-likelihood for NMT then involves matching the one-hot distribution over all complete sequences,

where y=[y1,…,yJ]\mathbf{y}=[y_{1},\dots,y_{J}] is the observed sequence. Of course, this just shows that from a negative log likelihood perspective, minimizing word-level NLL and sequence-level NLL are equivalent in this model.

But now consider the case of sequence-level knowledge distillation. As before, we can simply replace the distribution from the data with a probability distribution derived from our teacher model. However, instead of using a single word prediction, we use q(t ∣ s)q(\mathbf{t}\,|\,\mathbf{s}) to represent the teacher’s sequence distribution over the sample space of all possible sequences,

Note that LSEQ-KD\mathcal{L}_{\text{SEQ-KD}} is inherently different from LWORD-KD\mathcal{L}_{\text{WORD-KD}}, as the sum is over an exponential number of terms. Despite its intractability, we posit that this sequence-level objective is worthwhile. It gives the teacher the chance to assign probabilities to complete sequences and therefore transfer a broader range of knowledge. We thus consider an approximation of this objective.

Our simplest approximation is to replace the teacher distribution qq with its mode,

Observing that finding the mode is itself intractable, we use beam search to find an approximation. The loss is then

where y^\hat{\mathbf{y}} is now the output from running beam search with the teacher model.

To summarize, sequence-level knowledge distillation suggests to: (1) train a teacher model, (2) run beam search over the training set with this model, (3) train the student network with cross-entropy on this new dataset. Step (3) is identical to the word-level NLL process except now on the newly-generated data set. This is shown in Figure 1 (center).

3 Sequence-Level Interpolation

Next we consider integrating the training data back into the process, such that we train the student model as a mixture of our sequence-level teacher-generated data (LSEQ-KD\mathcal{L}_{\text{SEQ-KD}}) with the original training data (LSEQ-NLL\mathcal{L}_{\text{SEQ-NLL}}),

where y\mathbf{y} is the gold target sequence.

Since the second term is intractable, we could again apply the mode approximation from the previous section,

and train on both observed (y\mathbf{y}) and teacher-generated (y^\hat{\mathbf{y}}) data. However, this process is non-ideal for two reasons: (1) unlike for standard knowledge distribution, it doubles the size of the training data, and (2) it requires training on both the teacher-generated sequence and the true sequence, conditioned on the same source input. The latter concern is particularly problematic since we observe that y\mathbf{y} and y^\hat{\mathbf{y}} are often quite different.

As an alternative, we propose a single-sequence approximation that is more attractive in this setting. This approach is inspired by local updating [Liang et al., 2006], a method for discriminative training in statistical machine translation (although to our knowledge not for knowledge distillation). Local updating suggests selecting a training sequence which is close to y\mathbf{y} and has high probability under the teacher model,

where simsim is a function measuring closeness (e.g. Jaccard similarity or BLEU [Papineni et al., 2002]). Following local updating, we can approximate this sequence by running beam search and choosing

where TK\mathcal{T}_{K} is the KK-best list from beam search. We take simsim to be smoothed sentence-level BLEU [Chen and Cherry, 2014].

Experimental Setup

To test out these approaches, we conduct two sets of NMT experiments: high resource (English →\rightarrow German) and low resource (Thai →\rightarrow English).

The English-German data comes from WMT 2014.http://statmt.org/wmt14 The training set has 44m sentences and we take newstest2012/newstest2013 as the dev set and newstest2014 as the test set. We keep the top 5050k most frequent words, and replace the rest with UNK. The teacher model is a 4×10004\times 1000 LSTM (as in ?)) and we train two student models: 2×3002\times 300 and 2×5002\times 500. The Thai-English data comes from IWSLT 2015.https://sites.google.com/site/iwsltevaluation2015/mt-track There are 9090k sentences in the training set and we take 2010/2011/2012 data as the dev set and 2012/2013 as the test set, with a vocabulary size is 2525k. Size of the teacher model is 2×5002\times 500 (which performed better than 4×10004\times 1000, 2×7502\times 750 models), and the student model is 2×1002\times 100. Other training details mirror ?).

We evaluate on tokenized BLEU with multi-bleu.perl, and experiment with the following variations:

Student is trained on the original data and additionally trained to minimize the cross-entropy of the teacher distribution at the word-level. We tested α∈{0.5,0.9}\alpha\in\{0.5,0.9\} and found α=0.5\alpha=0.5 to work better.

Sequence-Level Knowledge Distillation (Seq-KD)

Student is trained on the teacher-generated data, which is the result of running beam search and taking the highest-scoring sequence with the teacher model. We use beam size K=5K=5 (we did not see improvements with a larger beam).

Sequence-Level Interpolation (Seq-Inter)

Student is trained on the sequence on the teacher’s beam that had the highest BLEU (beam size K=35K=35). We adopt a fine-tuning approach where we begin training from a pretrained model (either on original data or Seq-KD data) and train with a smaller learning rate (0.10.1). For English-German we generate Seq-Inter data on a smaller portion of the training set (∼50%\sim 50\%) for efficiency.

The above methods are complementary and can be combined with each other. For example, we can train on teacher-generated data but still include a word-level cross-entropy term between the teacher/student (Seq-KD ++ Word-KD in Table 1), or fine-tune towards Seq-Inter data starting from the baseline model trained on original data (Baseline ++ Seq-Inter in Table 1).For instance, ‘Seq-KD ++ Seq-Inter ++ Word-KD’ in Table 1 means that the model was trained on Seq-KD data and fine-tuned towards Seq-Inter data with the mixture cross-entropy loss at the word-level.

Results and Discussion

Results of our experiments are shown in Table 1. We find that while word-level knowledge distillation (Word-KD) does improve upon the baseline, sequence-level knowledge distillation (Seq-KD) does better on English →\rightarrow German and performs similarly on Thai →\rightarrow English. Combining them (Seq-KD ++ Word-KD) results in further gains for the 2×3002\times 300 and 2×1002\times 100 models (although not for the 2×5002\times 500 model), indicating that these methods provide orthogonal means of transferring knowledge from the teacher to the student: Word-KD is transferring knowledge at the the local (i.e. word) level while Seq-KD is transferring knowledge at the global (i.e. sequence) level.

Sequence-level interpolation (Seq-Inter), in addition to improving models trained via Word-KD and Seq-KD, also improves upon the original teacher model that was trained on the actual data but fine-tuned towards Seq-Inter data (Baseline ++ Seq-Inter). In fact, greedy decoding with this fine-tuned model has similar performance (19.619.6) as beam search with the original model (19.519.5), allowing for faster decoding even with an identically-sized model.

We hypothesize that sequence-level knowledge distillation is effective because it allows the student network to only model relevant parts of the teacher distribution (i.e. around the teacher’s mode) instead of ‘wasting’ parameters on trying to model the entire space of translations. Our results suggest that this is indeed the case: the probability mass that Seq-KD models assign to the approximate mode is much higher than is the case for baseline models trained on original data (Table 1: p(t=y^)p(\mathbf{t}=\hat{\mathbf{y}})). For example, on English →\rightarrow German the (approximate) argmax⁡\operatorname*{argmax} for the 2×5002\times 500 Seq-KD model (on average) accounts for 16.9%16.9\% of the total probability mass, while the corresponding number is 0.9%0.9\% for the baseline. This also explains the success of greedy decoding for Seq-KD models—since we are only modeling around the teacher’s mode, the student’s distribution is more peaked and therefore the argmax⁡\operatorname*{argmax} is much easier to find. Seq-Inter offers a compromise between the two, with the greedily-decoded sequence accounting for 7.6%7.6\% of the distribution.

Finally, although past work has shown that models with lower perplexity generally tend to have higher BLEU, our results indicate that this is not necessarily the case. The perplexity of the baseline 2×5002\times 500 English →\rightarrow German model is 8.28.2 while the perplexity of the corresponding Seq-KD model is 22.722.7, despite the fact that Seq-KD model does significantly better for both greedy (+4.2+4.2 BLEU) and beam search (+1.4+1.4 BLEU) decoding.

Run-time complexity for beam search grows linearly with beam size. Therefore, the fact that sequence-level knowledge distillation allows for greedy decoding is significant, with practical implications for running NMT systems across various devices. To test the speed gains, we run the teacher/student models on GPU, CPU, and smartphone, and check the average number of source words translated per second (Table 2). We use a GeForce GTX Titan X for GPU and a Samsung Galaxy 6 smartphone. We find that we can run the student model 1010 times faster with greedy decoding than the teacher model with beam search on GPU (1051.31051.3 vs 101.9101.9 words/sec), with similar performance.

2 Weight Pruning

Although knowledge distillation enables training faster models, the number of parameters for the student models is still somewhat large (Table 1: Params), due to the word embeddings which dominate most of the parameters.Word embeddings scale linearly while RNN parameters scale quadratically with the dimension size. For example, on the 2×5002\times 500 English →\rightarrow German model the word embeddings account for approximately 63%63\% (5050m out of 8484m) of the parameters. The size of word embeddings have little impact on run-time as the word embedding layer is a simple lookup table that only affects the first layer of the model.

We therefore focus next on reducing the memory footprint of the student models further through weight pruning. Weight pruning for NMT was recently investigated by ?), who found that up to 80−90%80-90\% of the parameters in a large NMT model can be pruned with little loss in performance. We take our best English →\rightarrow German student model (2×5002\times 500 Seq-KD ++ Seq-Inter) and prune x%x\% of the parameters by removing the weights with the lowest absolute values. We then retrain the pruned model on Seq-KD data with a learning rate of 0.20.2 and fine-tune towards Seq-Inter data with a learning rate of 0.10.1. As observed by ?), retraining proved to be crucial. The results are shown in Table 3.

Our findings suggest that compression benefits achieved through weight pruning and knowledge distillation are orthogonal.To our knowledge combining pruning and knowledge distillation has not been investigated before. Pruning 80%80\% of the weight in the 2×5002\times 500 student model results in a model with 13×13\times fewer parameters than the original teacher model with only a decrease of 0.40.4 BLEU. While pruning 90%90\% of the weights results in a more appreciable decrease of 1.01.0 BLEU, the model is drastically smaller with 88m parameters, which is 26×26\times fewer than the original teacher model.

3 Further Observations

For models trained with word-level knowledge distillation, we also tried regressing the student network’s top-most hidden layer at each time step to the teacher network’s top-most hidden layer as a pretraining step, noting that ?) obtained improvements with a similar technique on feed-forward models. We found this to give comparable results to standard knowledge distillation and hence did not pursue this further.

There have been promising recent results on eliminating word embeddings completely and obtaining word representations directly from characters with character composition models, which have many fewer parameters than word embedding lookup tables [Ling et al., 2015a, Kim et al., 2016, Ling et al., 2015b, Jozefowicz et al., 2016, Costa-Jussa and Fonollosa, 2016]. Combining such methods with knowledge distillation/pruning to further reduce the memory footprint of NMT systems remains an avenue for future work.

Related Work

Compressing deep learning models is an active area of current research. Pruning methods involve pruning weights or entire neurons/nodes based on some criterion. ?) prune weights based on an approximation of the Hessian, while ?) show that a simple magnitude-based pruning works well. Prior work on removing neurons/nodes include ?) and ?). ?) were the first to apply pruning to Neural Machine Translation, observing that that different parts of the architecture (input word embeddings, LSTM matrices, etc.) admit different levels of pruning. Knowledge distillation approaches train a smaller student model to mimic a larger teacher model, by minimizing the loss between the teacher/student predictions [Bucila et al., 2006, Ba and Caruana, 2014, Li et al., 2014, Hinton et al., 2015]. ?) additionally regress on the intermediate hidden layers of the student/teacher network as a pretraining step, while ?) obtain smaller word embeddings from a teacher model via regression. There has also been work on transferring knowledge across different network architectures: ?) show that a deep non-recurrent neural network can learn from an RNN; ?) train a CNN to mimic an LSTM for speech recognition. ?) recently investigated knowledge distillation for structured prediction by having a single parser learn from an ensemble of parsers.

Other approaches for compression involve low rank factorizations of weight matrices [Denton et al., 2014, Jaderberg et al., 2014, Lu et al., 2016, Prabhavalkar et al., 2016], sparsity-inducing regularizers [Murray and Chiang, 2015], binarization of weights [Courbariaux et al., 2016, Lin et al., 2016], and weight sharing [Chen et al., 2015, Han et al., 2016]. Finally, although we have motivated sequence-level knowledge distillation in the context of training a smaller model, there are other techniques that train on a mixture of the model’s predictions and the data, such as local updating [Liang et al., 2006], hope/fear training [Chiang, 2012], SEARN [Daumé III et al., 2009], DAgger [Ross et al., 2011], and minimum risk training [Och, 2003, Shen et al., 2016].

Conclusion

In this work we have investigated existing knowledge distillation methods for NMT (which work at the word-level) and introduced two sequence-level variants of knowledge distillation, which provide improvements over standard word-level knowledge distillation.

We have chosen to focus on translation as this domain has generally required the largest capacity deep learning models, but the sequence-to-sequence framework has been successfully applied to a wide range of tasks including parsing [Vinyals et al., 2015a], summarization [Rush et al., 2015], dialogue [Vinyals and Le, 2015, Serban et al., 2016, Li et al., 2016], NER/POS-tagging [Gillick et al., 2016], image captioning [Vinyals et al., 2015b, Xu et al., 2015], video generation [Srivastava et al., 2015], and speech recognition [Chan et al., 2015a]. We anticipate that methods described in this paper can be used to similarly train smaller models in other domains.

References