A DAPTIVE I NPUT R EPRESENTATIONS FOR N EURAL L ANGUAGE M ODELING
A BSTRACT
We introduce adaptive input representations for neural language modeling which extend the adaptive softmax of Grave et al.
(2017) to input representations of variable capacity.
There are several choices on how to factorize the input and output layers, and whether to model words, characters or sub-word units.
We perform a systematic comparison of popular choices for a self-attentional architecture.
Our experiments show that models equipped with adaptive embeddings are more than twice as fast to train than the popular character input CNN while having a lower number of parameters.
On the WIKITEXT -103 benchmark we achieve 18.7 perplexity, an improvement of 10.5 perplexity compared to the previously best published result and on the BILLION WORD benchmark, we achieve 23.02 perplexity.
I NTRODUCTION
Language modeling is a basic task in natural language processing, with many applications such as speech recognition (Arisoy et al., 2012) and statistical machine translation (Schwenk et al., 2012;
Vaswani et al., 2013; Baltescu & Blunsom, 2015).
Recently, much progress has been made by neural methods (Bengio et al., 2003; Mikolov et al., 2010) based on LSTMs (J ´ ozefowicz et al., 2016), gated convolutional networks (Dauphin et al., 2017) and self-attentional networks (Al-Rfou et al., 2018).
There are different choices for the basic unit we wish to model, including full words (Bengio et al., 2003), characters for the input (Kim et al., 2016) or also the output (Merity et al., 2018) as well as sub-words
(Buckman & Neubig, 2018; Mielke & Eisner, 2018).
Word-based models are particularly challenging since computing probabilities for all 800K words of the BILLION WORD benchmark is still a substantial part of the overall computation (Chen et al., 2016).
A popular approach to lower the computational burden is to structure the output vocabulary so that not all probabilities need to be computed.
The hierarchical softmax does this by introducing latent variables or clusters to simplify normalization (Goodman, 2001; Morin & Bengio, 2005; Mikolov et al., 2011).
This has been further improved by the adaptive softmax which introduces a variable capacity scheme for output word embeddings, assigning more parameters to frequent words and fewer parameters to rare words (Grave et al., 2017).
In this paper, we introduce adaptive input embeddings which extend the adaptive softmax to input word representations.
This factorization assigns more capacity to frequent words and reduces the capacity for less frequent words with the beneﬁt of reducing overﬁtting to rare words.
For a competitive setup on the BILLION WORD benchmark, adaptive input embeddings reduce the number of parameters in the input and output layers by 23% while achieving higher accuracy over ﬁxed size embeddings.
When the adaptive input representations are tied with an adaptive softmax in the output, then the number of parameters is reduced by a total of 61%.
Our experiments compare models based on word inputs, character inputs, as well as sub-word units using a self-attention architecture (Vaswani et al., 2017).
We show that models with adaptive word representations can outperform very strong character-based models while training more than twice as fast.
We also substantially improve adaptive softmax by introducing additional dropout regularization in the tail projection.
On the WIKITEXT -103 benchmark we achieve a perplexity of 18.7, a
[Table]
[Figure]
reduction of 10.5 perplexity over the previously best published result.
On the larger BILLION WORD benchmark our best model with adaptive input embeddings achieves 23.02 perplexity, a reduction of nearly 5 perplexity over the next best previously published result.
R ELATED W ORK
Adaptive word representations are inspired by the adaptive softmax work Grave et al.
(2017) which ﬁrst described a GPU friendly way to construct a hierarchical softmax and showed that it performs very competitively compared to a full softmax, while offering signiﬁcantly faster speed and a lower memory footprint.
Merity et al.
(2018) use a modiﬁed version of adaptive softmax which does not reduce the dimensionality of less frequent words in order to be able to share output embeddings with the input.
This setup is akin to a hierarchical softmax with tied weights.
We show that variable-sized input embeddings can perform better than ﬁxed sized embeddings.
Furthermore, this also enables weight sharing with an adaptive softmax output layer.
Merity et al.
(2018) evaluates both character-based and word-based factorizations but does not directly compare them to each other.
We perform a direct comparison of word-based and characterbased input vocabularies and also compare to a sub-word factorization for both the input and output.
Recently, Al-Rfou et al.
(2018) demonstrated that self-attentional models can perform very well on language modeling tasks where the input and output is both characters.
We also consider word-based benchmarks.
A DAPTIVE I NPUT R EPRESENTATIONS
The adaptive softmax exploits the fact that the distribution of word types in natural language follows a Zipﬁan distribution in order to improve the computation of the output probabilities.
We apply the same intuition for input word embeddings with the motivation to reduce the number of parameters which frees up capacity for other parts of the model.
We deﬁne a number of clusters that partitions the frequency ordered vocabulary V = V 1 ∪
V 2 , . .
, V n − 1 ∪ V n such that V i ∩ V j = ∅ for ∀ i, j , and i (cid:54) =
j , where V 1 contains the most fre- quent words and
V n the least frequent words.
We will refer to V 1 as the head and to any subsequent clusters loosely as tail .
We reduce the capacity for each cluster by a factor of k .
That is, if words in V 1 have dimension d , then words in V n have dimension d k n − 1 .
We typically set k = 4 following
Grave et al.
Next, we add linear projections W 1 ∈ R d × d , . . .
W n ∈ R d/k n − 1 × d to map the embeddings of each cluster to dimension d so that the concatenated output of the adaptive input embedding layer can be easily used by the subsequent model (Figure 1).
We also project V 1 which already has dimension d .
When presented with a number of input words, the adaptive input embedding layer partitions the words into the various clusters, performs separate lookups in the embedding tables and then projects to dimension d , followed by concatenating the embeddings in the original order.
Weight sharing.
When the output layer is an adaptive softmax with the same partition of V , d , and k as the adaptive input layer, then we can tie the weights (Inan et al., 2016; Press & Wolf, 2017).
This further reduces the number of parameters and can simultaneously improve performance ( § 5).
We can share both the parameters for the actual words as well as the projections W 1 , . . .
, W n
Sharing the word embeddings is straightforward except for the head where the adaptive softmax has n − 1 additional embeddings for the remaining clusters which are not shared with the input.
We share all projections, except for the head projection which is not available in the adaptive softmax since the model output is directly multiplied with the output word embeddings for the head band.
Performance decreased when we added a head projection to the adaptive softmax in the output, regardless of when it was shared or not.
Sharing both the word embeddings as well as the projections performed very well on WIKITEXT -
but on BILLION WORD we only share the word embeddings as we found that this performed better on the validation set.
E XPERIMENTAL S ETUP 4. M ODEL
We follow most of the architectural choices described in Vaswani et al.
but use only a decoder network.
We add sinusoidal position embeddings to the input layer and stack N = 16 blocks for both BILLION WORD and WIKITEXT -103.
Each block contains two sub-blocks: the ﬁrst is a multihead self-attention module with H = 16 heads.
The second sub-block is a feed-forward module (FFN) of the form ReLU ( W 1 X + b 1 ) W 2 + b 2 where W 1 ∈ R e × e ff , W 1 ∈ R e ff × e and e = 1024 , e ff = 4096 unless otherwise stated.
Different to Vaswani et al.
(2017) we apply layer normalization before the self-attention and FFN blocks instead of after, as we ﬁnd it leads to more effective training.
Sub-blocks are surrounded by a residual connection (He et al., 2015).
We adapt the Transformer implementation available in the fairseq toolkit.
We use a dropout rate of 0.1 and attention dropout of 0.1 for BILLION WORD models, and increase regularization for WIKITEXT -103 by using dropout 0.3, and 0.1 ReLU dropout as well as attention dropout 0.1.
We use the same hyperparameters for all models trained on the same dataset in order to enable a like for like comparison.
When the dimensionality of the input or output layer differs from e , then we add a simple linear projection with no bias.
4. D ATASETS
We experiment on the BILLION WORD benchmark and WIKITEXT -103.
BILLION WORD contains 768M word tokens and has a vocabulary of about 800K word types, which corresponds to words with more than 3 occurrences in the training set (Chelba et al., 2013).
The training data of WIKITEXT -103 comprises about 100M tokens and a vocabulary of around 260K, corresponding to types with more than 3 occurrences in the training data (Merity et al., 2016).
The dataset is composed of shufﬂed Wikipedia articles where the context carries across sentences.
4. B ATCHING
For BILLION WORD we batch individual sentences since the corpus does not contain document structure.
For WIKITEXT -103 we partition the training data into blocks of 512 contiguous tokens ignoring document boundaries.
Evaluation is the same except that we require blocks to contain complete sentences totaling up to 512 tokens.
We limit the number of tokens per GPU to a maximum threshold B per GPU.
That is, we add examples of similar length until we reach this threshold.
When we train on multiple GPUs, each
GPU processes B tokens using the same model parameters.
This increases the effective batch size to the product of the number of GPUs and B .
For BILLION WORD models we use B = 2048 and typically train on 32 GPUs, giving an effective batch size of 65K tokens.
The smaller vocabulary of
WIKITEXT -103 enables increasing B to 4096 and we train on 8 GPUs.
We found that large batch training is beneﬁcial for this dataset
and we therefore accumulate gradient updates over two batches before committing a parameter update (Ott et al., 2018a).
This gives an effective batch size of 65K tokens for WIKITEXT -103.
4. I NPUT AND O UTPUT LAYER HYPERPARAMETERS
Embedding sizes.
For ﬁxed size word input layers and softmax output layers we generally use embeddings of size 512 for WIKITEXT -103.
When we use an adaptive softmax in the output and ﬁxed size word embeddings for the input, then we use dimension 256 for the input embeddings for
BILLION WORD and 64 for WIKITEXT -103.
We tuned this choice on the validation set (Appendix
A).
BPE inputs and outputs have embeddings of size 1024.
Character CNN.
We model character inputs by convolving the representations of all characters in a word following Kim et al.
(2015) which applies several ﬁlters, then max pooling, a number of highway layers and a projection.
Character embeddings have size 128 and we apply seven ﬁlters of size 1x128, 2x256, 3x384, 4x512, 5x512, 6x512, 7x512, where 3x128 indicates a ﬁlter processing three characters that outputs 128 features.
We use a single highway layer for WIKITEXT -103, and two for BILLION WORD .
We do not add start of word and end of word markers as they did not improve validation accuracy.
We train on the same pre-processed data as the other models, with unknown tokens in both the inputs and outputs.
Adaptive input representations and adaptive softmax.
We use an adaptive softmax output layer to train models with large word-based vocabularies.
For adaptive word inputs and adaptive softmax, we use embeddings of size d = 1024 for the head and reduce the size of subsequent clusters by a factor of k = 4 .
For WIKITEXT -103, we have three bands of size 20K (d=1024), 40K (d=256) and 200K (d=64).
For BILLION WORD the bands are 60K (d=1024), 100K (d=256), and 640K (d=64).
Sub-word models.
We learn a byte-pair encoding (BPE) of 32K codes on the training data of each benchmark (Sennrich et al., 2016).
After applying the code to the training data we obtain a vocabulary of 33,337 tokens for WIKITEXT -103 and 32,347 tokens for BILLION WORD .
BPE input/output embeddings have size 1024.
The ﬁnal evaluation is in terms word-level perplexity to be comparable to other models.
The probability of a word is the product of the sub-word units.
4. O PTIMIZATION
Different to Vaswani et al.
we use Nesterov’s accelerated gradient method (Sutskever et al., 2013) with a momentum of 0 .
and we renormalize gradients if their norm exceeds 0 .
1 (Pascanu et al., 2013).
The learning rate is linearly warmed up from 10 − 7 to 1 for 16K steps and then annealed using a cosine learning rate schedule with C cycles (Loshchilov & Hutter, 2016).
Each cycle runs for twice the number of updates than the previous cycle and we lower the maximum and minimum learning rates by a rate M compared to the previous cycle.
The initial minimum learning rate is 10 − 5 and the maximum is 1 .
BILLION WORD models train for a total of 975K updates over C = 3 cycles, the ﬁrst cycle takes 137K steps, and we set M = 0 .
The WIKITEXT -103 models train for 286K steps over C =cycles, the ﬁrst cycle takes 18K setps and we set M = 0 .
We run experiments on DGXmachines with 8 NVIDIA V100 GPUs and machines are interconnected by Inﬁniband.
We also use the NCCL2 library and the torch.distributed package for inter-GPU communication.
We train models with 16-bit ﬂoating point precision, following Ott et al.
(2018b).
[Table]
[Table]
E XPERIMENTS AND R ESULTS 5. M AIN RESULTS
For the main results on BILLION WORD
[Table]
we doubled the batch size by training on 64 GPUs instead of 32 GPUs.
We also consider two larger setups, one where we added four more blocks ( N = 20 ) and increased the FFN dimension to e ff = 6144 (large)
[Table]
and another where we add another four blocks ( N = 24 ) with e ff = 8192 and e = 1536 (very large).
All other settings follow § 4.4 and all models were trained for the same number of steps.
Table 1 compares our models to previous work on BILLION WORD .
The adaptive input model outperforms the best previously reported result at an order of magnitude fewer parameters.
Our large model performs nearly as well as an ensemble of over ten models and achieves a new state of the art of 24.14 perplexity.
Our very large model performs as well as an ensemble of over ten models and achieves 23.02 perplexity.
The Char-CNN model performs 0.6 PPL worse than the standard adaptive input model even though it trained for over 40% longer.
Table 2 shows our result on WIKITEXT -103 where adaptive inputs achieve 18.7 perplexity. For this result only, we partition the training data into blocks of 3072 contiguous tokens instead oftokens as for other experiments. During evaluation we require blocks to contain complete sentences totaling up to 3072 tokens of which the ﬁrst 2560 tokens serve as context to score the last 512 tokens; we take care to score all tokens in the test and validation sets. We motivate this choice in § 5.3.
5. C OMPARISON OF INPUT AND OUTPUT LAYER FACTORIZATIONS
Next, we perform a systematic comparison of different input and output layer factorizations.
We consider a word-based setup with ﬁxed size word input embeddings and a standard word softmax (SM) where embeddings have either dimension 512 ( WIKITEXT -103) or 64 ( BILLION WORD ).
We consider tying the input and output embeddings (SM-T).
Instead of words, we try less sparse sub- [Table] [Table] word units, both in the input and output, with embeddings of size 1024 (BPE) and shared weights (BPE-T).
Next, we consider replacing the ﬁxed size output representations by an adaptive softmax (ASM) and characters as input (CNN).
Finally, we use both adaptive input word representations as well as an adaptive softmax (ADP) and a tied version (ADP-T).
All models use the same selfattention architecture described in § 4.1.
Table 3 shows results when training all conﬁgurations for the same number of updates.
Adaptive input representations with tied input and output layers (ADP-T) achieve the highest accuracy at the same speed as the BPE models which have a very small vocabulary (33K versus 260K).
CNN is 1 perplexity worse than ADP-T and requires well over twice the training time.
It is the slowest approach, even though it has a fast adaptive softmax in the output.
Fixed word embeddings perform least well (SM).
Sub-word units are fast to train and perform better than word models with ﬁxed sized embeddings.
ASM improves over SM and greatly speeds up training.
For ASM, we found that reducing the dimension of the input word embeddings to 64 on WIKITEXT -103 results in better accuracy (Appendix A).
Table 4 shows that adaptive input representations perform equally well on BILLION WORD compared to other factorizations.
ADP-T is 34% faster than ADP because there are fewer parameters to update.
Similar to before, ADP-T trains more than twice as fast as CNN at higher accuracy, however, the accuracy gap is narrower than for WIKITEXT -103.
Regularization is more important on WIKITEXT -103 while models for BILLION WORD beneﬁt from additional capacity.
Because of this we used input word embeddings of size 256 for ASM.
We also trained CNN without replacing input words outside the vocabulary by an unknown symbol, however, this only improved validation perplexity by 0.16.
[Figure]
SM
SM-T
BPE
BPE-T
ASM
CNN
ADP
ADP-T
[Figure]
[Figure]
[Figure]
L o ss on t h e n e x t w o r d
[Figure]
5. A NALYSIS
Next, we turn to the question of how well models perform on rare words compared to frequent words.
We compute the average loss for each word in the test set and group words by frequency.
Figure 2 shows results on WIKITEXT -103.
Tying weights helps all models on rare words, likely because of regularization effects.
Fixed size word embeddings with a word softmax (SM and SMT) do not perform well on rare words.
This is likely due to underﬁtting on common words and we use the largest possible embedding size we could ﬁt on 16GB GPU cards given our batch size (more experimentation in Appendix A).
BPE and BPE-T perform poorly on rare words because probabilities are a product of several sub-word units.
ADP-T performs best across all frequency ranges.
Figure 3 bins the loss by the frequency of the previous word and shows that CNN does well when it has rare words in the context, however, ADP-T does best across all bins.
Figure 4 shows an equivalent analysis for BILLION WORD .
The largest differences between models is on rare words.
CNN performs best on very rare words but is outperformed by ADP in all other settings.
Similar to WIKITEXT -103, BPE and BPE-T perform poorly on rare words.
Further 1K 10K 100K 1M 1M
[Table]
Word frequency bin
[Figure]
BPE
BPE-T
ASM
CNN
ADP
ADP-T
[Table]
Training block size number of consecutive tokens considered during analysis (Appendix 5.3) binning the loss by the frequency of the previous word shows that weight sharing also helps for BILLION WORD and that CNN does very well on rare words for BILLION
WORD compared to other models.
Table 5 shows the importance of context size for WIKITEXT -103.
Training block size is the number of consecutive tokens that are considered at once during training.
Inference context is the number of tokens that are provided at evaluation before any tokens are scored.
Simply increasing the training block size from 512 to 3072 results in an improvement of nearly 1.2 perplexity with no inference context window.
Increasing the context size at inference time results in an improvement of 0.
perplexity for the largest training block size.
5. A DAPTIVE S OFTMAX VS . FULL S OFTMAX
We also found that adaptive softmax can beneﬁt from additional regularization of rare words.
Adaptive softmax ﬁrst projects the model output to the dimension of a particular cluster and then computes a dot product with the respective word embeddings.
We add dropout to the output of the ﬁrst projection for all clusters, except for the head.
This change enables the adaptive softmax to outperform a standard softmax over ﬁxed size output word embeddings on WIKITEXT -
(Table 6).
However, we found that adding dropout in this way is not helpful for larger datasets such as BILLION
WORD .
Unfortunately, a standard softmax over 800K words is not tractable and we were unable to make a comparison.
It may be possible to achieve better results by tuning dropout for each band of the tail and we leave this for future work. [Table]
C ONCLUSION
Adaptive input embeddings vary the size of input word embeddings which can improve accuracy while drastically reducing the number of model parameters.
When sharing parameters with an adaptive softmax, the number of parameters can be further reduced which improves training speed.
We presented a comparison between different input and output layer factorizations including word inputs, character inputs and sub-word units in both the input and output.
Our experiments show that models with adaptive input embeddings train faster compared to character input CNNs while achieving higher accuracy.
We achieve new state of the art results on
WIKITEXT -103 and BILLION WORD .
In future work, we will apply variable sized input embeddings to other tasks.
A CKNOWLEDGMENTS
We thank Tom Bosc for fruitful comments and suggestions.
S UPPLEMENTARY M ATERIAL A A DDITIONAL EXPERIMENTS ON WIKITEXT -
This appendix shows various ablation.
Table 7 shows that reducing the capacity of ﬁxed size word input embddings is beneﬁcial on WIKITEXT -103.
The next set of results in Table 7 shows results for various settings of the SM and SM-T models.
We also experimented with sharing the head projection but found this to perform less well than not sharing it.
Finally, Table 8 shows various band sizes for adaptive input word embbedings.
We also show the performance of Merity et al.
(2018) who use an adaptive softmax with equally sized word representations and share the input and output embeddings (no dim reduction, tied). Input Output
Dropout
Valid PPL
Parameters 256d Embedding
Adaptive 0.
314.7M 128d Embedding
Adaptive 0.
280.3M 64d Embedding
Adaptive 0.
263.1M 32d Embedding
Adaptive 0.
254.5M 512d Embedding 512d Softmax (tied) 0.
339.7M 512d Embedding 512d Softmax (tied) 0.
339.7M 512d Embedding 512d Softmax 0.
512d Embedding 512d Softmax 0.
256d Embedding 256d Softmax (tied) 0.
270.6M 256d Embedding 256d Softmax 0.
339.1M 64d Embedding 512d Softmax 0.
356.3M
Adaptive
Adaptive (tied emb, not proj) 0.
247.3M
Adaptive Adaptive (tied emb/proj not head) 0.
246.9M
Adaptive
Adaptive (
[Table] [Table]
B A NALYSIS
This appendix extends the analysis in § 5.3 by showing a breakdown of the test loss when binning by the frequency of the previous word.
[Figure]
Word frequency bin
L o ss on t h e n e x t w o r d
BPE
BPE-T
ASM
CNN
ADP ADP-T