Low-Rank RNN Adaptation for Context-Aware Language Modeling

A context-aware language model uses location, user and/or domain metadata (context) to adapt its predictions. In neural language models, context information is typically represented as an embedding and it is given to the RNN as an additional input, which has been shown to be useful in many applications. We introduce a more powerful mechanism for using context to adapt an RNN by letting the context vector control a low-rank transformation of the recurrent layer weight matrix. Experiments show that allowing a greater fraction of the model parameters to be adjusted has benefits in terms of perplexity and classification for several different types of context.


Introduction
In many language modeling applications, the speech or text is associated with some metadata or contextual information. For example, in speech recognition, if a user is speaking to a personal assistant then the system might know the time of day or the identity of the task that the user is trying to accomplish. If the user takes a picture of a sign to translate it with their smart phone, the system would have contextual information related to the geographic location and the user's preferred language. The context-aware language model targets these types of applications with a model that can adapt its predictions based on the provided contextual information.
There has been much work on using context information to adapt language models. Here, we are interested in contexts described by metadata (vs. word history or related documents) and in neural network approaches due to their flexibility for representing diverse types of contexts. Specifically, we focus on recurrent neural networks (RNNs) due to their widespread use.
The standard approach to adapt an RNN language model is to concatenate the context representation with the word embedding at the input to the RNN (Mikolov and Zweig, 2012). Optionally, the context embedding is also concatenated with the output from the recurrent layer to adapt the softmax layer. This basic strategy has been adopted for various types of adaptation such as for LM personalization (Wen et al., 2013;Li et al., 2016), adapting to television show genres (Chen et al., 2015), and adapting to long range dependencies in a document , etc.
We propose a more powerful mechanism for using a context vector, which we call the FactorCell. Rather than simply using context as an additional input, it is used to control a factored (low-rank) transformation of the recurrent layer weight matrix. The motivation is that allowing a greater fraction of the model parameters to be adjusted in response to the input context will produce a model that is more adaptable and responsive to that context.
We evaluate the resulting models in terms of context-dependent perplexity and context classification accuracy on six tasks reflecting different types of context variables, comparing to baselines that represent the most popular methods for using context in neural models. We choose tasks where context is specified by metadata, rather than text samples as used in many prior studies. The combination of experiments on a variety of data sources provides strong evidence for the utility of the Factor-Cell model, but the results show that it can be useful to consider more than just perplexity in training a language model. The remainder proceeds as follows. In Section 2, we introduce the FactorCell model and show how it differs mathematically from alternative approaches. Next, Section 3 describes the six datasets used to probe the performance of different models. Experiments and analyses contrasting perplexity and classification results for a variety of context variables are provided in Section 4, demonstrating consistent improvements in both criteria for the Fac-torCell model but also confirming that perplexity is not correlated with classification performance for all models. Analyses explore the effectiveness of the model for characterizing high-dimensional context spaces. The model is compared to related work in Section 5. Finally, Section 6 summarizes contributions and open questions.
2 Context-Aware RNN Our model uses adaptation in both the recurrent layer and in the bias vector of the output layer. In this section we describe how we represent context as an embedding and methods for adapting the recurrent layer and the softmax layer, showing that our proposed model is a generalization of prior methods. The novelty of our model is that instead of using context as an additional input to the model, it uses the context information to transform the weights of the recurrent layer. This is accomplished using a low-rank decomposition in order to control the extent of parameter sharing between contexts, which is important for handling high-dimensional, sparse contexts.

Context representation
We assume the availability of contextual information (metadata or other side information) that is represented as a set of context variables f 1:n = f 1 , f 2 , . . . f n , from which we produce a kdimensional representation in the form of an embedding, c ∈ R k . Each of the context variables, f i , represents some type of information or metadata about the sequence and can be either categorical or numer-ical. The embeddings can either be learned off-line using a topic model (Mikolov and Zweig, 2012) or end-to-end as part of the adapted LM (Tang et al., 2016). Here, we use end-to-end learning, where the context embedding is the output of a feed-forward network with a ReLU activation function. The resulting embedding, c, is used for adapting both the recurrent layer and the output layer of the RNN.

Adapting the recurrent layer
The basic operation of the recurrent layer is to use a matrix W to transform the concatenation of a word embedding, w t ∈ R e , with the hidden state from the previous time step, h t−1 ∈ R d , and produce a new hidden state, h t , as given by Equation 1: (1) The size of W is d × (e + d). For simplicity, our equations assume a simple RNN. Appendix A shows how the equations can be adjusted to work with an LSTM.
The standard approach to recurrent layer adaptation is to include (via concatenation) the context embedding as an additional input to the recurrent layer (Mikolov and Zweig, 2012). When the context embedding is constant across the whole sequence, it is easy to show that this concatenation is equivalent to using a context-dependent bias at the recurrent layer: whereŴ = [W V] and b = Vc + b is the contextdependent bias, formed by adding a linear projection of the context embedding. We refer to this adaptation approach as the ConcatCell model.
Our proposed model extends the ConcatCell by using a context-dependent weight matrix W = W + A, in place of the generic weight matrix W. (We refer to W as generic because it is shared across all context settings.) The adaptation matrix, A, is generated by taking the product of the context embedding vector against a set of left and right basis tensors to produce a rank r matrix. The left and right adaptation basis tensors are given as Z L ∈ R k×(e+d)×r and Z R ∈ R r×d×k . The two bases tensors together can be thought of as holding k different rank r matrices, A j = Z L,j Z R,j , each the size of W. By taking the product between c and the corresponding tensor modes of Z L and Z R (using × i to denote the mode-i tensor product, i.e., the product with the i-th dimension of the tensor), the context determines the weighted combination of the k matrices: The number of degrees of freedom of A is controlled by the dimension k of the context vector and the rank r of the k weight matrices. The rank is treated as a hyperparameter and controls the extent to which the model relies on the generic weight matrix W versus behaves in a more context-specific manner. We call this model the FactorCell because the weight matrix has been adapted by adding a factored component. The ConcatCell model is a special case of the FactorCell where Z L and Z R are set to zero. In summary, the proposed model is given by: If the context is known in advance, W can be precomputed, in which case applying the RNN at test time requires no more computation than using an unadapted RNN of the same size. This means that for a fixed sized recurrent layer, the FactorCell model can have many more parameters than the Concat-Cell model but hardly any increase in computational cost.

Adapting the Softmax Bias
The last layer of the model predicts the probability of the next symbol in the sequence using the output from the recurrent layer using the softmax function to create a normalized probability distribution. The output probabilities are given by where E ∈ R |V |×e is the matrix of word embeddings, L ∈ R e×d is a linear projection to match the dimension of the recurrent layer (when e = d), and b out ∈ R |V | is the softmax bias vector. We tie the word embeddings in the input layer with the ones in the output layer (Press and Wolf, 2017;Inan et al., 2017). If s j is the indicator row vector for the jth word in the vocabulary then p(w t |w 1:t−1 ) = s t y t and log p(w 1:T ) = t log s wt y t .
Adapting the softmax bias alters the unigram distribution. There are two ways to accomplish this. When the values that the context can take are categorical with low cardinality then context-dependent softmax bias vectors can be learned directly. This is equivalent to replacing c with a one-hot vector. Otherwise, a projection of the context embedding, Qc where Q ∈ R |V |×k , can be used to adapt the bias vector as in The projection can be thought of as a low-rank approximation to using the one-hot context vector. Both strategies are explored, depending on the nature of the original context space. As noted in Section 5, adaptation of the softmax bias has been used in other studies. As we show in the experimental work, it is useful for representing phenomena where unigram statistics are important.

Data
Our experiments make use of six datasets: four targeting word-level sequences, and two targeting character sequences. The character studies are motivated by the growing interest in character-level models in both speech recognition and machine translation (Hannun et al., 2014;Chung et al., 2016). By using multiple datasets with different types of context, we hope to learn more about what makes a dataset amenable to adaptation. The datasets range in size from over 100 million words of training data to 5 million characters of training data for the smallest one. When using a word-based vocabulary, we preprocess the data by lowercasing, tokenizing and removing most punctuation. We also truncate sentences to be shorter than a maximum length of 60 words for AGNews and DBPedia and 150 to 200 tokens for the remaining datasets. Summary information is provided in Table 1 of tokens, vocabulary size, number of training documents (i.e. context samples), and the context variables (f 1:n ). The largest dataset, TripAdvisor, has over 800 thousand hotel review documents, which adds up to over 125 million words of training data. The first three datasets (AGNews, DBPedia, and Yelp) have previously been used for text classification (Zhang et al., 2015). These consist of newspaper headlines, encyclopedia entries, and restaurant and business reviews, respectively. The context variables associated with these correspond to the newspaper section (world, sports, business, sci & tech) for each headline, the page category on DBPedia (out of 14 options such as actor, athlete, building, etc.), and the star rating on Yelp (from one to five). For AgNews, DBPedia, and Yelp we use the same test data as in previous work. Our fourth dataset, from TripAdvisor, was previously used for language modeling and consists of two relevant context variables: an identifier for the hotel and a sentiment score from one to five stars (Tang et al., 2016). Some of the reviews are written in French or German but most are in English. There are 4,333 different hotels but we group all the ones that do not occur at least 50 times in the training data into a single entity, leaving us with around 3,500. These four datasets use wordbased vocabularies.
We also experiment on two Twitter datasets: Eu-roTwitter and GeoTwitter. EuroTwitter consists of 80 thousand tweets labeled with one of nine languages: (English, Spanish, Galician, Catalan, Basque, Portuguese, French, German, and Italian). The corpus was created by combining portions of multiple published datasets for language identification including Twitter70 (Jaech et al., 2016), Tweet-LID (Zubiaga et al., 2014), and the monolingual portion of tweets from a code-switching detection workshop (Molina et al., 2016). The GeoTwitter data contains tweets with latitude and longitude information from England, Spain, and the United States. 1 The latitude and longitude coordinates are given as numerical inputs. This is different from the other five datasets that all use categorical context variables.

Experiments with Different Contexts
The goal of our experiments is to show that the FactorCell model can deliver improved performance over current approaches for multiple language model applications and a variety of types of contexts. Specifically, results are reported for contextconditioned perplexity and generative model text classification accuracy, using contexts that capture a range of phenomena and dimensionalities.
Test set perplexity is the most widely accepted method for evaluating language models, both for use in recognition/translation applications and generation. It has the advantage that it is easy to measure and is widely used as a criteria for model fit, but the limitation that it is not directly matched to most tasks that language models are directly used for. Text classification using the model in a generative classifier is a simple application of Bayes rule: where w 1:T is the text sequence, p(ω) is the class prior, which we assume to be uniform. Classification accuracy provides additional information about the power of a model, even if it is not being designed explicitly for text classification. Further, it allows us to be able to directly compare our model perfor-mance against previously published text classification benchmarks.
Note that the use of classification accuracy for evaluation here involves counting errors associated with applying the generative model to independent test samples. This differs from the accuracy criterion used for evaluating context-sensitive language models for text generation based on a separate discriminative classifier trained on generated text (Ficler and Goldberg, 2017;Hu et al., 2017). We discuss this further in Section 5.
The experiments compare the FactorCell model (equations 4 and 6) to two popular alternatives, which we refer to as ConcatCell (equations 2 and 6) and SoftmaxBias (equation 6). As noted earlier, the SoftmaxBias method is a simplification of the ConcatCell model, which is in turn a simplification of the FactorCell model. The SoftmaxBias method impacts only the output layer and thus only unigram statistics. Since bag-of-word models provide strong baselines in many text classification tasks, we hypothesize that the SoftmaxBias model will capture much of the relative improvement over the unadapted model for word-based tasks. However, in small vocabulary character-based models, the unigram distribution is unlikely to carry much information about the context, so adapting the recurrent layer should become more important in characterlevel models. We expect that performance gains will be greatest for the FactorCell model for sources that have sufficient structure and data to support learning the extra degrees of freedom.
Another possible baseline would use models independently trained on the subset of data for each context. This is the "independent component" case in (Yogatama et al., 2017). This will fail when a context variable takes on many values (or continuous values) or when training data is limited, because it makes poor use of the training data, as shown in that study. While we do have some datasets where this approach is plausible, we feel that its limitations have been clearly established.

Implementation Details
The RNN variant that we use is an LSTM with coupled input and forget gates (Melis et al., 2018). The different model variants are implemented 2 using the Tensorflow library. The model is trained with the standard negative log likelihood loss function, i.e. minimizing cross entropy. Dropout is used as a regularizer in the recurrent connections as described in Semeniuta et al. (2016). Training is done using the Adam optimizer with a learning rate of 0.001. For the models with word-based vocabularies, a sampled softmax loss is used with a unigram proposal distribution and sampling 150 words at each time-step (Jean et al., 2014). The classification experiments use a sampled softmax loss with a sample size of 8,000 words. This is an order of magnitude faster to compute with a minimal effect on accuracy.
Hyperparameter tuning was done based on minimizing perplexity on the development set and using a random search. Hyperparameters included word embedding size e, recurrent state size d, context embedding size k, and weight adaptation matrix rank r, the number of training steps, recurrent dropout probability, and random initialization seed. The selected hyperparameter values are listed in Table 2. For any fixed LSTM size, the FactorCell has a higher count of learned parameters compared to the ConcatCell. However, during evaluation both models use approximately the same number of floatingpoint operations because W only needs to be computed once per sentence. Because of this, we believe limiting the recurrent layer cell size is a fair way to compare between the FactorCell and the ConcatCell.

Word-based Models
Perplexities and classification accuracies for the four word-based datasets are presented in Table 3. In each of the four datasets, the FactorCell model gives the best perplexity. For classification accuracy, there is a bigger difference between the models, and the FactorCell model is the most accurate on three out of four datasets and tied with the SoftmaxBias model on AgNews. For DBPedia and TripAdvisor, most of the improvement in perplexity relative to the unadapted case is achieved by the SoftmaxBias model with smaller relative improvements coming from the increased power of the ConcatCell and FactorCell models. For Yelp, the perplexity improvements are small; the FactorCell model is just 1.3% better than   the unadapted model.
From Yogatama et al. (2017), we see that for AG-News, much more so than for other datasets, the unigram statistics capture the discriminating information, and it is the only dataset in that work where a naive Bayes classifier is competitive with the generative LSTM for the full range of training data. The fact that the SoftmaxBias model gets the same accuracy as the FactorCell model on this task suggests that topic classification tasks may benefit less from adapting the recurrent layer.
For the DBPedia and Yelp datasets, the Factor-Cell model beats previously reported classification accuracies for generative models (Yogatama et al., 2017). However, it is not competitive with state-ofthe-art discriminative models on these tasks with the full training set. With less training data, it probably would be, based on the results in (Yogatama et al., 2017). Table 3 do not adequately convey the fact that there are hyperparameters whose effect on perplexity is greater than the sometimes small relative differences between models. Even the seed for the random weight initialization can have a "major impact" on the final performance of an LSTM (Reimers and Gurevych, 2017). We use Figure 1 to show how the three classes of models perform across a range of hyperparameters. The figure compares perplexity on the x-axis with accuracy on the y-axis with both metrics computed on the development set. Each point in this figure represents a different instance of the model trained with random hyperparameter settings and the best results are in the upper right corner of each plot. The color/shape differences of the points correspond to the three classes of models: FactorCell, ConcatCell, and SoftmaxBias. Within the same model class but across different hyperparameter settings, there is much more variation in perplexity than in accuracy. The LSTM cell size is mainly responsible for this; it has a much bigger impact on perplexity than on accuracy. It is also apparent that the models with the lowest perplexity are not always the ones with the highest accuracy. See Section 4.4 for further analysis. Figure 2 is a visualization of the per-word log likelihood ratio between a model assuming a 5 star review and the same model assuming a 1 star review. Likelihoods were computed using an ensemble of three models to reduce variance. The analysis is repeated for each class of model. Words highlighted in blue are given a higher likelihood under the 5 star assumption.

The numbers in
Unigrams with strong sentiment such as "lovely" and "friendly" are well-represented by all three models. The reader may not consider the tokens "craziness" or "5-8pm" to be strong indicators of a positive review but the way they are used in this review is representative of how they are typically used across the corpus.
As expected, the ConcatCell and FactorCell model capture the sentiment of multi-token phrases. As an example, the unigram "enough" is 3% more likely to occur in a 5 star review than in a 1 star review. However, "do enough" is 30 times more likely to appear in a 5 star review than in a 1 star review. In this example, the FactorCell model does a better job of handling the word "enough."

Character-based Models
Next, we evaluate the EuroTwitter and GeoTwitter models using both perplexity and a classification task. For EuroTwitter, the classification task is to identify the language. With GeoTwitter, it is less obvious what the classification task should be because the context values are continuous and not categorical. We selected six cities and then assigned each sentence the label of the closest city in that list while still retaining the exact coordinates of the Tweet. There are two cities from each country: Manchester, London, Madrid, Barcelona, New York City, and Los Angeles. Tweets from locations further than 300 km from the nearest city on the list were discarded when evaluating the classification accuracy.
Perplexities and classification accuracies are pre-SoftmaxBias ConcatCell FactorCell Figure 2: Log likelihood ratio between a model that assumes a 5 star review and the same model that assumes a 1 star review. Blue indicates a higher 5 star likelihood and red is a higher likelihood for the 1 star condition.
sented in Table 4. The FactorCell model has the lowest perplexity and the highest accuracy for both datasets. Again, the FactorCell model clearly improves on the ConcatCell as measured by classification accuracy. Consistent with our hypothesis, adapting the softmax bias is not effective for these small vocabulary character-based tasks. The Soft-maxBias model has small perplexity improvements (< 1%) and low classification accuracies.   sible to trade-off some perplexity for gains in classification accuracy. For EuroTwitter, if tuning is done on accuracy rather than perplexity then the accuracy of the best model is as high as 95%.

EuroTwitter GeoTwitter
Sometimes there can be little to no perplexity improvement between the unadapted model and the FactorCell model. This can be explained if the provided context variables are mostly redundant given the previous tokens in the sequence. To investigate this further, we trained a logistic regression classifier to predict the language using the state from the LSTM at the last time step on the unadapted model as a feature vector. Using just 30 labeled examples per class it is possible to get 74.6% accuracy. Furthermore, we find that a single dimension in the hidden state of the unadapted model is often enough to distinguish between different languages even though the model was not given any supervision signal. This finding is consistent with previous work that showed that individual dimensions of LSTM hidden states can be strong indicators of concepts like sentiment (Karpathy et al., 2015;Radford et al., 2017). Figure 4 visualizes the value of the dimension of the hidden layer that is the strongest indicator of Spanish on three different code-switched tweets. Code-switching is not a part of the training data but it provides a compelling visualization of the ability of the unsupervised model to quickly recognize the language. The fact that it is so easy for the unadapted model to pick-up on the identity of the contextual variable fits with our explanation for the small relative gain in perplexity from the adapted models in these two tasks.

Hyperparameter Analysis
The hyperparameter with the strongest effect on perplexity is the size of the LSTM. This was consistent across all six datasets. The effect on classification accuracy of increasing the LSTM size was mixed. Increasing the context embedding size generally helped with accuracy on all datasets, but it had a more neutral effect on TripAdvisor and Yelp and increased perplexity on the two character-based datasets. For the FactorCell model, increasing the rank of the adaptation matrix tended to lead to increased classification accuracy on all datasets and seemed to help with perplexity on AGNews, DBPedia, and TripAdvisor.  Figure 5 compares the effect on perplexity of the LSTM parameter count and the FactorCell rank hyperparameters. Each point in those plots represents a separate instance of the model with varied hy-504 perparameters. In the right subplot of Figure 5, we see that increasing the rank hyperparameter improves perplexity. This is consistent with our hypothesis that increasing the rank can let the model adapt more. The variance is large because differences in other hyperparameters (such as hidden state size) also have an impact.
In the left subplot we compare the performance of the FactorCell with the ConcatCell as the size of the word embeddings and recurrent state change. The x-axis is the size of the W recurrent weight matrix, specifically 3(e + d)d for an LSTM with 3 gates. Since the adapted weights can be precomputed, the computational cost is roughly the same for points with the same x-value. For a fixed-size hidden state, the FactorCell model has a better perplexity than the ConcatCell.
Since performance can be improved both by increasing the recurrent state dimension and/or by increasing rank, we examined the relative benefits of each. The perplexity of a FactorCell model with an LSTM size of 120K will improve by 5% when the rank is increased from 0 to 20. To get the same decrease in perplexity by changing the size of the hidden state would require 160K parameters, resulting in a significant computational advantage for the Fac-torCell model.
Using a one-hot vector for adapting the softmax bias layer in place of the context embedding when adapting the softmax bias vector tended to have a large positive effect on accuracy leaving perplexity mostly unchanged. Recall from Section 2.3 that if the number of values that a context variable can take on is small then we can allow the model to choose between using the low-dimensional context embedding or a one-hot vector. This option is not available for the TripAdvisor and the GeoTwitter datasets because the dimensionality of their one-hot vectors would be too large. The method of adapting the softmax bias is the main explanation for why some Con-catCell models performed significantly above/below the trendline for DBPedia in Figure 1.
We experimented with an additional hyperparameter on the Yelp dataset, namely the inclusion of layer normalization (Ba et al., 2016). (We had ruledout using layer normalization in preliminary work on the AGNews data before we understood that AG-News is not representative, so only one task was explored here.) Layer normalization significantly helped the perplexity on Yelp (≈ 2% relative improvement) and all of the top-performing models on the held-out development data had it enabled.

Analysis for Sparse Contexts
The TripAdvisor data is an interesting case because the original context space is high dimensional (3500 hotels × 5 user ratings) and sparse. Since the model applies end-to-end learning, we can investigate what the context embeddings learn. In particular, we looked at location (hotels are from 25 cities in the United States) and class of hotel, neither of which are input to the model. All of what it learns about these concepts come from extracting information from the text of the reviews.
To visualize the embedding, we used a 2dimensional PCA projection of the embeddings of the 3500 hotels. We found that the model learns to group the hotels based on geographic region; the projected embeddings for the largest cities are shown in Figure 6, plotting the 1.5σ ellipsoid of the Gaussian distribution of the points. (Actual points are not shown to avoid clutter.) Not only are hotels from the same city grouped together, cities that are close geographically appear close to each other in the embedding space. Cities in the Southwest appear on the left of the figure, the West coast is on top and the East coast and Midwest is on the right side. This is likely due in part to the impact of the region on activities that guests may mention, but there also appears to be a geographic sampling bias in the hotel class that may impact language use.
Class is a rating from an independent agency that indicates the level of service and amenities that customers can expect to receive at a hotel. Whereas, the star rating is the average score given to each establishment by the customers who reviewed it. Hotel class does not determine star rating although they are correlated (r = 0.54). The dataset does not contain a uniform sample of hotel classes from each city. The hotels included from Boston, Chicago, and Philly are almost exclusively high class and the ones from L.A. and San Diego happen to be low class, so the embedding distributions also reflect hotel class: lower class hotels towards the top left and higher class hotels towards the bottom right. The visualization for the ConcatCell and SoftmaxBias models Another way of understanding what the context embeddings represent is to compute the softmax bias projection Qc and examine the words that experience the biggest increase in probability. We show three examples in Table 5. In each case, the top words are strongly related to geography and include names of neighborhoods, local attractions, and other hotels in the same city. The top boosted words are relatively unaffected by changing the rating. (Recall that the hotel identifier and the user rating are the only two inputs used to create the context embedding.) This table combined with the other visualizations indicates that location effects tend to dominate in the output layer, which may explain why the two models adapting the recurrent network seem to have a bigger impact on classification performance.

Prior Work
There have been many studies of neural language models that can be dynamically adapted based on context. Methods have been referred to as contextdependent models (Mikolov and Zweig, 2012), context-aware models (Tang et al., 2016), conditioned models (Ficler and Goldberg, 2017), and controllable text generation (Hu et al., 2017). These models have been used in scoring word sequences (such as for speech recognition or machine translation), for text classification, and for generation. In some work, context corresponds to the previous word history. Here, we instead consider known factors such as user, location and domain metadata, though the framework could be used with historybased context.
The studies that most directly relate to our work are neural models that correspond to special cases of the more general FactorCell model, including those that leverage what we call the SoftmaxBias model (Dieng et al., 2017;Tang et al., 2016;Yogatama et al., 2017;Ficler and Goldberg, 2017) and others that use the ConcatCell approach (Mikolov and Zweig, 2012;Wen et al., 2013;Chen et al., 2015;Ghosh et al., 2016). One study  compares the two approaches, which they refer to as ccDCLM and coDCLM. They find that both approaches give similar perplexities, but their ConcatCell style model does better at an auxiliary sentence ordering task. This is consistent with our finding that adapting at the recurrent layer can benefit certain tasks while having only a minor impact on perplexity. They do not test any models that adapt both the recurrent and output layers. Hoang et al. (2016) also consider adapting at the hidden layer vs. at the softmax layer, but their architecture does not fit cleanly into the framework of the SoftmaxBias model because they use an extra perceptron layer; thus, it is difficult to compare the experimental findings with ours.
The FactorCell model is distinguished by having an additive (factored) context-dependent transformation of the recurrent layer weight matrix. A related additive context-dependent transformation has been proposed for log-bilinear sequence models (Eisenstein et al., 2011;Hutchinson et al., 2015), but these are less powerful than the RNN. A somewhat different use of low-rank factorization has previously been used to reduce the parameter count in an LSTM LM (Kuchaiev and Ginsburg, 2017), finding that the reduced number of parameters leads to faster training.
There is a long history of adapting n-gram language models (see DeMori and Federico (1999) or Bellegarda (2004) for a survey). One recent example is Chelba and Shazeer (2015) where a 34% relative improvement in perplexity was obtained when using geographic features for adaptation. We hypothesize that the impressive improvement in perplexity is possible because the language in their dataset of Google mobile search queries is particularly sensi-  tive to location. Compared to n-gram based LMs, our model has two advantages in the way that it handles context. First, as we showed in our GeoTwitter experiments, we can adapt to geography using GPS coordinates as input without using predefined geographic regions as in Chelba and Shazeer. Second, our model supports the joint effect of multiple contextual variables. Neural models have an advantage over discrete models as the number of context variables increases.
Much of the work on context-adaptive neural language models has focused on incorporating document or topic information (Mikolov and Zweig, 2012;Ghosh et al., 2016;Dieng et al., 2017), where context is defined in terms of word or n-gram statistics. Our work differs from these studies in that the context is defined by a variety of sources, including discrete and/or continuous metadata, which is mapped to a context vector in end-to-end training. Context-sensitive language models for text generation tend to involve other forms of context similar to the objective of our work, including speaker characteristics (Luan et al., 2016;Li et al., 2016), dialog act (Wen et al., 2015), sentiment and other factors (Tang et al., 2016;Hu et al., 2017), and style (Ficler and Goldberg, 2017). As noted earlier, some of this work has used discriminative text classification to evaluate generation. In preliminary experiments with the Yelp data set, we found that the generative classifier accuracy of our model is highly correlated with discriminative classfier accuracy (r ≈ 0.95). Thus, by this measure, we anticipate that the model would be useful for generation applications. Anecdotally, we find that the model gives more coherent generation results for DBPedia data, but further validation with human ratings is necessary to confirm the benefits on more sources.

Conclusions
In summary, this paper has introduced a new model for adapting (or controlling) a language model depending on contextual metadata. The FactorCell model extends prior work with context-dependent RNNs by using the context vector to generate a lowrank, factored, additive transformation of the recurrent cell weight matrix. Experiments with six tasks show that the FactorCell model matches or exceeds performance of alternative methods in both perplexity and text classification accuracy. Findings hold for a variety of types of context, including highdimensional contexts, and the adaptation of the recurrent layer is particularly important for characterlevel models. For many contexts, the benefit of the FactorCell model comes with essentially no additional computational cost at test time, since the transformations can be pre-computed. Analyses of a dataset with a high-dimensional sparse context vector show that the model learns context similarities to facilitate parameter sharing.
In all six tasks that are explored here, all context factors are available for all training and testing samples. In some scenarios, it may be possible for some context factors to be missing. A simple solution for handling this is to use the expected value for the missing variable(s), since this is equivalent to using a weighted combination of the adaptation matrices for the different possible values of the missing variables.
In this work, the experiment scenarios all used metadata to specify context, since this type of context can be more sensitive to data sparsity and has been less studied. In contrast, in many prior studies of language model adaptation, context is specified in terms of text samples, such as prior user queries, prior sentences in a dialog, other documents related 507 in terms of topic or style, etc. The FactorCell framework introduced here is also applicable to this type of context, but the best encoding of the text into an embedding (e.g. using bag of words, sequence models, etc.) is likely to vary with the application. The FactorCell can also be used with online learning of context vectors, e.g. to take advantage of previous text from a particular author (or speaker) (Jaech and Ostendorf, 2018).
The models evaluated here were tuned to minimize perplexity, as is typical for language modeling. In analyses of performance with different hyperparameter settings, we find that perplexity is not always positively correlated with accuracy, but the criteria are more often correlated for approaches that adapt the recurrent layer. While not surprising, the results raise concerns about using perplexity as the sole evaluation metric for context-aware language models. More work is needed to understand the relative utility of these objectives for language model design.