N-gram Language Modeling using Recurrent Neural Network Estimation

03/31/2017 ∙ by Ciprian Chelba, et al. ∙ 0

We investigate the effective memory depth of RNN models by using them for n-gram language model (LM) smoothing. Experiments on a small corpus (UPenn Treebank, one million words of training data and 10k vocabulary) have found the LSTM cell with dropout to be the best model for encoding the n-gram state when compared with feed-forward and vanilla RNN models. When preserving the sentence independence assumption the LSTM n-gram matches the LSTM LM performance for n=9 and slightly outperforms it for n=13. When allowing dependencies across sentence boundaries, the LSTM 13-gram almost matches the perplexity of the unlimited history LSTM LM. LSTM n-gram smoothing also has the desirable property of improving with increasing n-gram order, unlike the Katz or Kneser-Ney back-off estimators. Using multinomial distributions as targets in training instead of the usual one-hot target is only slightly beneficial for low n-gram orders. Experiments on the One Billion Words benchmark show that the results hold at larger scale: while LSTM smoothing for short n-gram contexts does not provide significant advantages over classic N-gram models, it becomes effective with long contexts (n > 5); depending on the task and amount of data it can match fully recurrent LSTM models at about n=13. This may have implications when modeling short-format text, e.g. voice search/query LMs. Building LSTM n-gram LMs may be appealing for some practical situations: the state in a n-gram LM can be succinctly represented with (n-1)*4 bytes storing the identity of the words in the context and batches of n-gram contexts can be processed in parallel. On the downside, the n-gram context encoding computed by the LSTM is discarded, making the model more expensive than a regular recurrent LSTM LM.

READ FULL TEXT VIEW PDF

Authors

page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

A statistical language model (LM) estimates the prior probability values

for strings of words in a vocabulary whose size is usually in the tens or hundreds of thousands. Typically the string is broken into sentences, or other segments such as utterances in automatic speech recognition which are assumed to be conditionally independent; the independence assumption has certain advantages in practice but is not strictly necessary.

Applying the chain rule to a sentence

we get:

(1)

Since the parameter space of is too large, the language model is forced to put the context into an equivalence class determined by a function . As a result:

(2)

Research in language modeling consists of finding appropriate equivalence classifiers

and methods to estimate .

1.1 Perplexity as a Measure of Language Model Quality

A commonly used quality measure for a given model is related to the entropy of the underlying source and was introduced under the name of perplexity (PPL) Jelinek (1997):

(3)

To give intuitive meaning to perplexity, it represents the average number of guesses the model needs to make in order to ascertain the identity of the next word, when running over the test word string

from left to right. It can be easily shown that the perplexity of a language model that uses the uniform probability distribution over words in the vocabulary

equals the size of the vocabulary; a good language model should of course have lower perplexity, and thus the vocabulary size is an upper bound on the perplexity of a given language model.

Very likely, not all words in the test data are part of the language model vocabulary. It is common practice to map all words that are out-of-vocabulary to a distinguished unknown word symbol, and report the out-of-vocabulary (OOV) rate on test data—the rate at which one encounters OOV words in the test sequence —as yet another language model performance metric besides perplexity. Usually the unknown word is assumed to be part of the language model vocabulary—open vocabulary language models—and its occurrences are counted in the language model perplexity calculation in Eq. (3). A situation less common in practice is that of closed vocabulary language models where all words in the test data will always be part of the vocabulary .

1.2 Smoothing

Since the language model is meant to assign non-zero probability to unseen strings of words (or equivalently, ensure that the cross-entropy of the model over an arbitrary test string is not infinite), a desirable property is that:

(4)

also known as the smoothing requirement.

There are currently two dominant approaches for building LMs:

1.2.1 -gram Language Models

The most widespread paradigm in language modeling makes a Markov assumption and uses the -gram equivalence classification, that is, defines

(5)

A large body of work has accumulated over the years on various smoothing methods for -gram LMs. The two most popular smoothing techniques are probably Kneser & Ney (1995) and Katz (1987), both making use of back-off to balance the specificity of long contexts with the reliability of estimates in shorter -gram contexts. Goodman (2001) provides an excellent overview that is highly recommended to any practitioner of language modeling.

Approaches that depart from the nested features used in back-off -gram LMs have shown excellent results at the cost of increasing the number of features and parameters stored by the model, e.g. Pelemans et al. (2016).

1.2.2 Neural Language Models

Neural networks (NNLM) have emerged in recent years as an alternative to estimating and storing

-gram LMs. Words (or some other modeling unit) are represented using an embedding vector

. A simple NNLM architecture makes the Markov assumption and feeds the concatenated embedding vectors for the words in the -gram context to one or more layers each consisting of an affine transform followed by a non-linearity (typically ); the output of the last such layer is then fed to the output layer consisting again of an affine transform but this time followed by an exponential non-linearity that is normalized to guarantee a proper probability over the vocabulary. This is commonly named a feed-forward architecture for an -gram LM (FF-NNLM), first introduced by Bengio et al. (2001).

An alternative is the recurrent NNLM architecture that feeds the embedding of each word one at a time, advancing the state of a recurrent cell and producing a new output :

(6)

This provides a representation for the context that can be directly plugged into Eq. 2:

(7)

Similar to the FF-NNLM architecture, the output of the recurrent cell is then fed to a soft-max layer consisting of an affine transform followed by an exponential non-linearity properly normalized over the vocabulary.

The recurrent cell can consist of one or more simple affine/non-linearity layers, often called a vanilla RNN architecture, see Mikolov et al. (2010). The LSTM cell due to Hochreiter & Schmidhuber (1997) has proven very effective at modeling long range dependencies and has become the state-of-the-art architecture for language modeling using RNNs, see Józefowicz et al. (2016).

In this work we approximate unlimited history (R)NN models with -gram models in an attempt to identify the order at which they become equivalent from a perplexity point of view. This is a promising direction in a few ways:

  • the training data can be reduced to -gram sufficient statistics, and the target distribution presented to the NN

    -gram LM in a given context can be a multinomial pmf instead of the one-hot encoding used in on-line training for (R)NN LMs;

  • unlike many LSTM LM implementations, back-propagation through time for the LSTM -gram need not be truncated at the begining of segments used to batch the training data;

  • the state in a -gram LM can be succinctly represented with bytes storing the identity of the words in the context; this is in stark contrast with the state for an RNN LM, where or higher, making the -gram LM much easier to use in decoders such as for ASR/SMT;

  • similar to Brants et al. (2007), batches of -gram contexts can be processed in parallel to estimate a sharded (R)NN -gram model; this is particularly attractive because it allows scaling both the amount of training data and the NNLM size significantly (100X).

2 Method

As mentioned in the previous section, the Markov assumption made by -gram models allows us to present to the NN multinomial training targets specifying the full distribution in a given -gram context instead of the usual one-hot target specifying the predicted word occurring in a given context instance. In addition, when using multinomial targets we can either weight each training sample by the context count or simply present each context token encountered in the training data along with the conditional multinomial pmf computed from the entire training set.

We thus have three main training regimes:

  • context-weighted multinomial targets

  • multinomial targets (context count )

  • one-hot targets (context count , word count )

The loss function optimized in training is the cross-entropy between the model pmf

in some -gram context and the relative frequency in the training data (or development , or test data ) is computed as:

(8)

where is the length of the training data and is the -gram model being evaluated/trained as parameterized by .

The baseline back-off

-gram models (Katz, interpolated Kneser-Ney) are trained by making a sentence independence assumption. As a result

-gram contexts at the beginning of the sentence are padded to the left to reach the full context length. The same

-gram counting strategy is used when preparing the data for the various NN -gram LMs that we experimented with. Since RNN LMs are normally trained and evaluated without making this independence assumption by passing the LM state across sentence boundaries, we also evaluated the impact of resetting the RNN LM state at sentence beginning.

We next detail the various flavors or NN LM implementations we experimented with.

For all NN LMs we represent context words using an embedding vector . Unless otherwise stated, all models are trained to minimize the cross-entropy on training data in Eq. (8), using Adagrad (Duchi et al. (2011)) and gradient norm clipping (Pascanu et al. (2012)

); the model parameters are initialized by sampling from a truncated normal distribution of zero mean and a given standard deviation.

Training proceeds for a fixed number of epochs for every given point on the grid of hyper-parameters explored for a given model type; the best performing model (parameter values) on development data

is retained as the final one to be evaluated on test data in order to report the model perplexity.

All models were implemented using TensorFlow, see 

Abadi & et al. (2015b).

2.1 Feed Forward -gram LM

Each word in the -gram context is embedded using the mapping ; the resulting vectors are concatenated to form a dimensional vector that is first fed into a dropout layer Srivastava et al. (2014)

and then into an affine layer followed by a

non-linearity. The output of this so-called “hidden” layer is again fed into a dropout layer and then followed by an affine layer whose output is of the same dimensionality as the vocabulary. An exponential “soft-max” layer converts the activations produced by the last affine layer into probabilities over the vocabulary.

To summarize:

(9)

The parameters of the model are the embedding matrix , the keep probability for dropout layers , the affine input layer parameterized by , and the output one parameterized by , .

The hyper-parameters controlling the training are: number of training epochs, -gram order, dimensionality of the model parameters , keep probability value, gradient norm clipping value, standard deviation for the initializer and the Adagrad learning rate and initial accumulator value.

2.2 “Vanilla” Recurrent -gram LM

Each word in the -gram context is embedded using the mapping followed by dropout and then fed in left-to-right order into the RNN cell in Eq. (6). The final output of the RNN cell is then fed first into a dropout layer and then into an affine layer followed by exponential “soft-max”.

Assuming that we encode the context with an RNN cell defined as follows (using the running index to traverse the context and a dropout layer on the embedding ):

(10)

we pick the last output and feed it into a dropout layer followed by an affine layer and soft-max output:

(11)

The parameters of the model are the embedding matrix , the keep probability for dropout layers , the RNN affine layer parameterized by and and the output one parameterized by and . Note that we choose to use the same dimensionality for both .

The hyper-parameters controlling the training are the same as in the previous section.

2.3 LSTM Recurrent -gram LM

Finally, we replace the “vanilla” RNN cell defined above with a multilayer LSTM cell with dropout. Since this was the most effective model, we experimented with a few options:

  • forward context encoding: context words are fed in left-to-right order in the LSTM cell; the LSTM cell output after the last context word is then fed into the output layer;

  • reverse context encoding: context words are fed in left-to-right order in the LSTM cell; the LSTM cell output after the first context word is then fed into the output layer;

  • stacked output for either of the above: we concatenate the output vectors along the way and feed that into the output layer;

  • bidirectional context encoding: we encode the context twice, forward and reverse order respectively, using two separate LSTM cells; the two outputs are then concatenated and fed to the output layer;

  • forward context encoding with incremental loss with/out exponential decay as a function of the context length

The last item above deserves a more detailed explanation. It is possible that the LSTM encoder would benefit from incremental error back-propagation along the -gram context instead of just one back-propagation step at the end of the context. As such, we modify the loss function to be the cumulative cross-entropy between the relative frequency and the model output distribution at each step in the for loop feeding the -gram context into the LSTM cell instead of just the last one. Thus amounts to targetting a mix of -gram target distributions; to have better control over the contribution of different -gram orders to the loss function, we weigh each loss function by an exponential term . The value controls how fast the contribution to the loss function from lower -gram orders decays; note that the highest order has weight so a very large value restores the regular training loss function. For this training regime we only implemented one-hot targets: the amount of data that needs to be fed to the TensorFlow graph would increase significantly for incremental multinomial targets.

The hyper-parameters controlling the training are: number of training epochs, -gram order, embedding dimensionality , LSTM cell output dimensionality and number of layers, keep probability value, gradient norm clipping value, standard deviation for the initializer. To match the fully recurrent LSTM LM implemented by the UPenn Treebank TensorFlow tutorial, we estimated all of our LSTM -gram models using gradient descent with variable learning rate: initially the learning rate is constant for a few iterations after which it follows a linear decay schedule. The hyper-parameters controlling this schedule were not optimized but rather we used the same values as in the RNN LM tutorial provided with Abadi & et al. (2015a) or the implementation in Józefowicz (2016), respectively.

Perhaps a bit of a technicality but it is worth pointing out a major difference between error back-propagation through time (BPTT) as implemented in either of the above and the error back-propagation in the LSTM/RNN -gram LM: Abadi & et al. (2015a) and Józefowicz (2016) implement BPTT by segmenting the training data into non-overlapping segments (of length 35 or 20, respectively)111We have evaluated the impact of reducing the segment length dramatically, e.g. 4 instead of 35. Much to our surprise, the LSTM PPL increased modestly, from 84 to 88, see the before last row in Table 1; for the One Billion Words experiments using a segment of length 5 did not change PPL at all.. The error BPTT does not cross the left boundary of such segments, whereas the LSTM state is of course copied forward. As a result, the first word in a segment is not really contributing to training, and the immediately following ones have a limited effect. This is in contrast to error back-propagation for the LSTM/RNN -gram LM: the -gram window slides over the training/test data, and error back-propagation covers the entire -gram context; the LSTM cell state and output computed for a given -gram context are discarded once the output distribution is computed.

3 Experiments

3.1 UPenn Treebank Corpus

For our initial set of experiments we used the same data set as in Abadi & et al. (2015a), with exactly the same training/validation/test set partition and vocabulary. The training data consists of about one million words, and the vocabulary contains ten thousand words; the validation/test data contains 73760/82430 words, respectively (including the end-of-sentence token). The out-of-vocabulary rate on validation/test data is 5.0/5.8%, respectively.

As an initial batch of experiments we trained and evaluated back-off -gram models using Katz and interpolated Kneser-Ney smoothing. We also used the medium setting in Abadi & et al. (2015a) as an LSTM/RNN LM baseline; since the baseline -gram models are trained under a sentence independence assumption, we also ran the LSTM/RNN LM baseline by resetting the LSTM state at each sentence beginning. The results are presented in Table 1.

Model Order Test PPL
-gram, baseline
Katz, back-off 5 167
Katz, back-off 9 182
Interpolated Kneser-Ney, back-off 5 143
Interpolated Kneser-Ney, back-off 9 143
LSTM RNN, baseline
LSTM (medium setting) reset state at <S> 95
LSTM (medium setting) 84
Table 1: UPenn Treebank: baseline back-off -gram and LSTM perplexity values.

As expected Kneser-Ney (KN) is better than Katz, and it does not improve with the -gram order past a certain value, in this case . This behavior is due to the fact that the -gram hit ratio on test data (number of test -grams that were observed in training) decreases dramatically with the -gram order: the percentage of covered -grams222For the hit ratio calculation the -grams are not padded to the left of sentence beginning; if we are to count hit ratios using padded -grams, the values are: 100, 81, 44.7, 24.0, 16.5, 13.7, 12.5, 11.8, 11.5, respectively. for is 100, 81, 42, 18, 8.6, 5.0, 3.3, 2.5, 2.0, respectively.

The medium setting for the LSTM LM in Abadi & et al. (2015a) performs significantly better than the KN baseline. Resetting the state at sentence beginning degrades PPL significantly by 13% relative.

Model Order Test PPL
Training Target multinomial one-hot
-gram, baseline
Interpolated Kneser-Ney, back-off 5 143
Feed-fwd -gram
Feed-fwd -gram 5 127 128
Feed-fwd -gram 9 125 126
Feed-fwd -gram 13 125 127
“Vanilla” RNN -gram
RNN -gram 9 127 131
LSTM RNN -gram
LSTM -gram, forward context encoding 5 103 106
LSTM -gram, forward context encoding 9 94 93
LSTM -gram, forward context encoding 13 91 90
LSTM -gram, reversed context encoding 9 102 107
LSTM -gram, bidirectional context encoding 9 100 102
incremental LSTM -gram with decay, 13 91
LSTM RNN, baseline
LSTM (medium setting) reset at <S> 95
Table 2: UPenn Treebank: perplexity values for neural network smoothed -gram LM.

We then trained and evaluated various NN-smoothed -gram LMs, as described in Section 2. The results are presented in Table 2. The best model among the ones considered is by far the LSTM -gram. The most significant experimental result is that the LSTM -gram can match and even outperform the fully recurrent LSTM LM as we increase the order : matches the LSTM LM performance, decreasing the LM perplexity by 34% relative over the Kneser-Ney baseline. LSTM -gram smoothing also has the desirable property of improving with the

-gram order, unlike the Katz or Kneser-Ney back-off estimators, which can be credited to better feature extraction from the

-gram context.

Multinomial targets can slightly outperform the one-hot ones although the difference is shrinking as we increase the -gram order. Weighting the contribution of each context to the loss function by its count did not work; we suspect this is because on-line training does not work well with the Zipf distribution on context counts.

Among the various flavors of LSTM models we experimented with, the forward context encoding performs best. The incremental LSTM -gram with a fairly large decay () is slightly better but we do not consider the difference to be statistically significant (it also entails significantly more computation, we need to perform back-propagation steps for each input -gram).

To compare with the LSTM RNN LM that does not reset state at sentence beginning we also trained LSTM -gram models (forward context encoding only) that straddle the sentence beginning. The results are presented in Table 3. Again, we notice that for a large enough order the LSTM -gram LM comes very close to matching the fully recurrent LSTM baseline.

Model Order Test PPL
Training Target multinomial one-hot
LSTM RNN -gram
LSTM -gram, forward context encoding, straddling <S> 5 102 104
LSTM -gram, forward context encoding, straddling <S> 9 91 95
LSTM -gram, forward context encoding, straddling <S> 13 87 91
LSTM RNN, baseline
LSTM (medium setting) 84
Table 3: UPenn Treebank: perplexity values for neural network smoothed -gram LM when straddling the sentence beginning boundary.

3.2 One Billion Words Benchmark

In a second set of experiments we used the corpus in Chelba et al. (2013), the same as in Józefowicz et al. (2016). For the baseline LSTM model we used the single machine implementation provided by Józefowicz (2016); the LSTM -gram variant was implemented as a minor tweak on this codebase and is thus different from the one used in the UPenn Treebank experiments in Section 3.1.

We experimented with the LSTM configuration in Table 3 of Józefowicz et al. (2016) for both baseline LSTM and -gram variant, which are also the default settings in Józefowicz (2016)

: embedding and projection layer dimensionality was 128, one layer with state dimensionality of 2048. Training used Adagrad with gradient clipping by global norm (10.0) and droput (probability 0.1); back-propagation at the output soft-max layer is done using importance sampling as described in 

Józefowicz et al. (2016) with a set 8192 “negative” samples. An additional set of experiments investigated the benefits of adding one more layer to both baseline and -gram LSTM.

The results are presented in Tables 4-5; unlike the UPenn Treebank experiments, we did not tune the hyper-parameters for the -gram LSTM and instead just used the same ones as for the LSTM baseline; as a result the perplexity values for the -gram LSTM may be slightly suboptimal.

Similar to the UPenn Treebank experiments, we examined the effect of resetting state at sentence boundaries. As expected PPL dit not change significantly because the sentences in the training and test data were randomized, see Chelba et al. (2013); in fact modeling the sentence independence explicitly is slightly beneficial.

We observe that on large amounts of data LSTM smoothing for short -gram contexts does not provide significant advantages over classic back-off -gram models. This may have implications for short-format text, e.g. voice search/query LMs. On the other hand, LSTM smoothing becomes very effective with long contexts () approaching the fully recurrent LSTM model perplexity at about .

Training times are significantly different between the LSTM baseline and the -gram variant, with the latter being about an order of magnitude slower due to the fact that the LSTM state is recomputed and discarded for every new training sample.

Model Order Test PPL
Training Target one-hot
-gram, baseline
Interpolated Kneser-Ney, back-off 5 68
LSTM RNN -gram
LSTM -gram, forward context encoding 5 70
LSTM -gram, forward context encoding 9 54
LSTM -gram, forward context encoding 13 49
LSTM RNN, baseline
LSTM reset at <S> 48
2-layer LSTM RNN -gram
LSTM -gram, forward context encoding 5 68
LSTM -gram, forward context encoding 9 51
LSTM -gram, forward context encoding 13 46
2-layer LSTM RNN, baseline
LSTM reset at <S> 43
Table 4: One Billion Words Benchmark: perplexity values for neural network smoothed -gram LM when enforcing the sentence independence.
Model Order Test PPL
Training Target one-hot
LSTM RNN -gram
LSTM -gram, forward context encoding, straddling <S> 5 70
LSTM -gram, forward context encoding, straddling <S> 9 54
LSTM -gram, forward context encoding, straddling <S> 13 49
LSTM RNN, baseline
LSTM 49
2-layer LSTM RNN -gram
LSTM -gram, forward context encoding, straddling <S> 5 68
LSTM -gram, forward context encoding, straddling <S> 9 51
LSTM -gram, forward context encoding, straddling <S> 13 46
2-layer LSTM RNN, baseline
LSTM 43
Table 5: One Billion Words Benchmark: perplexity values for neural network smoothed -gram LM when straddling the sentence beginning boundary.

4 Conclusions and Future Work

We investigated the effective memory depth of (R)NN models by using them for word-level -gram LM smoothing. The LSTM cell with dropout was by far the best (R)NN model for encoding the -gram state.

When preserving the sentence independence assumption the LSTM -gram matches the LSTM LM performance for and slightly outperforms it for . When allowing dependencies across sentence boundaries, the LSTM -gram almost matches the perplexity of the unlimited history LSTM LM.

We can thus conclude that the memory of LSTM LMs seems to be about 9-13 previous words which is not a trivial depth but not that large either.

Compared to standard -gram smoothing methods LSTMs have excellent statistical properties: they improve with the -gram order well beyond the point where Katz or Kneser-Ney back-off smoothing methods saturate, proving that they are able to extract richer features from the same context. Using multinomial targets in training is only slightly beneficial in this setting, although the advantage over one-hot diminishes with increasing -gram order.

Experiments on the One Billion Words benchmark confirm that -gram LSTMs can match the performance of fully recurrent LSTMs at larger amounts of data.

Building LSTM -gram LMs is attractive due to the fact that the state in a -gram LM can be succinctly represented on bytes storing the identity of the context words. This is in stark contrast with the state for an RNN LM, where or higher, making the -gram LM easier to use in decoders such as for ASR/SMT. The LM requests in the decoder can be batched, making the RNN LM operation more efficient on GPUs.

On the downside, the LSTM encoding for the -gram context is discarded and cannot be re-used; caching it for frequent LM states is possible.

Acknowledgments

We would like to thank Oriol Vinyals and Rafał Józefowicz for support with the baseline implementation of LSTM LMs for UPenn Treebank in Abadi & et al. (2015a) and One Billion Words Benchmark in Józefowicz (2016), respectively. We would also like to thank Maxim Krikun for thorough code reviews and useful discussions.

References