Contextual BERT : Conditioning the Language Model Using a Global State (2020)

Abstract: BERT is a popular language model whose main pre-training task is to fill in the blank, i.e., predicting a word that was masked out of a sentence, based on the remaining words. In some applications, however, having an additional context can help the model make the right prediction, e.g., by taking the domain or the time of writing into account. This motivates us to advance the BERT architecture by adding a global state for conditioning on a fixed-sized context. We present our two novel approaches and apply them to an industry use-case, where we complete fashion outfits with missing articles, conditioned on a specific customer. An experimental comparison to other methods from the literature shows that our methods improve personalization significantly.

## introduction since its publication, the bert model by @xcite has enjoyed great popularity in the natural language processing (nlp) community. to apply the model to a specific problem, it is commonly pretrained on large amounts of unlabeled data, and subsequently fine-tuned on a target task. during both stages, the model's only input is a variably-sized sequence of words. there are use-cases, however, where having an additional context can help the model. consider a query intent classifier whose sole input is a user's text query. under the assumption that users from different age groups and professions express the same intent in different ways, the classifier would benefit from having access to that user context in addition to the query. alternatively, one might consider training multiple models on separate, age group-and profession-specific samples. however, this approach does not scale well, requires more training data, and does not share knowledge between the models. to the best of our knowledge, there is a shortcoming in effective methods for conditioning bert on a fixed-sized context. motivated by this, and inspired by the graph-networks perspective on self-attention models @xcite , we advance bert's architecture by adding a global state that enables conditioning. with our proposed methods @xcite and @xcite , we combine two previously independent streams of work. the first is centered around the idea of explicitly adding a global state to bert, albeit without using it for conditioning. the second is focused on injecting additional knowledge into the bert model. by using a global state for conditioning, we enable the application of bert in a range of use-cases that require the model to make context-based predictions. we use the outfit completion problem to test the performance of our new methods: the model predicts fashion items to complete an outfit and has to account for both style coherence and personalization. for the latter, we condition on a fixed-sized customer representation containing information such as customer age, style preferences, hair color, and body type. we compare our methods against two others from the literature and observe that ours are able to provide more personalized predictions. aggregates sentence-wide, global knowledge at the position of the [cls] token. this intuition was confirmed through attention score analysis @xcite , however, the bert architecture does not have an inductive bias that aids it. recent work therefore treats the [cls] token differently. @xcite constrain their bert variant big bird to local attention only, with the exception that every position may always attend to [cls] regardless of its spatial proximity. @xcite also observe that the [cls] attention exhibits peculiar patterns. this motivates them to introduce a separate set of weights for attending to and from @xcite . the authors thereby explicitly encode into the architecture that the sequence's first position has a special role and different modality than the other positions. the result is an increased performance on downstream glue tasks. it is important to note that all related work on bert's global state does not use the global state for conditioning. instead, the architectural changes are solely being introduced to improve the performance on non-contextual nlp benchmarks. conditioning on a context to the best of our knowledge, @xcite are the first to provide sentence-wide information to the model to ease the masked language model (mlm) pre-training task. the authors inject the target label (e.g., positive or negative review) of sentiment data by adding it to the [cls] token embedding. in a similar application, @xcite process the context separately and subsequently combine it with the model output to make a sentiment prediction. xia et al. ( 2020 ) condition on richer information, namely an intent, which can be thought of as a task descriptor given to the model. the intent is represented in text form, is variably sized, and prepended to the sequence. this is very similar to a wide range of gpt @xcite applications. chen et al. (2019) condition on a customer's variably-sized click history using a transformer @xcite . the most similar to our work are @xcite who personalize by concatenating every position in the input sequence with a user embedding -method [c] from section 3. their approach, however, lacks an architectural bias that makes the model treat the user embedding as global information. bert as a graph neural network (gnn) @xcite introduce a framework that unites several lines of research on gnns. in the appendix, the authors show that -within their framework -the transformer architecture is a type of gnn; joshi (2020) supports this finding. in both cases the observation is that a sentence can be seen as a graph, where words correspond to nodes and the computation of an attention score is the assignment of a weight to an edge between two words. in the gnn framework, a global state is accessible from every transfer function and can be individually updated from layer to layer. foot_0 neither transformer nor bert, however, have a global state in that sense. inspired by this observation, we introduce a global state and use it for conditioning. we explain our two novel methods in the following section, alongside with two that are derived from the literature. ## conditioning bert with a global state let w denote a sequence of n words w @xmath0. further, let w -i be the sequence without the ith word. recall that a vanilla bert model @xcite can predict the probability pr(@xmath1, conditioned on the other words in the sequence. next, we introduce four methods to additionally condition bert on a context vector @xmath2, which allows it to predict pr(@xmath3, c). @xcite , we concatenate the context vector with every position in the input sequence. let x @xmath4. the resulting input matrix is i ## empirical evaluation and discussion we evaluate the performance of our proposed methods on a real-world industry problem: personalized fashion outfit completion (see figure 2 ) for europe's largest fashion platform. our proprietary dataset consists of 380k outfits, created by professional stylists for individual customers. when styling a customer, i.e., putting together an outfit, the stylist has access to all customer features that we later use to condition the model. therefore, customer data and outfit are statistically dependent. the customer features are individually embedded using trainable, randomly initialized embedding spaces. the per-feature embedding vectors are subsequently concatenated, yielding a context vector of d @xmath5. features include the customer's age, gender, country, preferred brands/colors/styles, nogo types, clothing sizes, price preferences, and the occasion for which the outfit is needed. the second model input, namely the outfit itself, is constructed from learned embeddings for every individual fashion article, with d @xmath6. we stack @xmath7. for a masked-out item, the model predicts a probability distribution over |v| = 30 000 articles. while not being an nlp dataset, our data resembles many of the important traits of a textual corpus: the vocabulary size is comparable to the one of word-piece vocabularies commonly used with bert models. fashion outfits are similar to sentences in that some articles appear often together (match stylewise) and others do not. different is the typical sequence length which ranges from four to eight fashion articles, with an average length of exactly five. in contrast to sentences, outfits do not have an inherent order. to account for that we remove the positional encoding from bert so it treats its input as a set. the empirical evaluation reveals the effectiveness of using a context for making predictions. the model's ability to replicate the stylist behavior better, i.e., achieve a higher r@r, improves substantially with the addition of a context. on r@1 we see a relative improvement of +43% by using the [gsu] method for conditioning over using no customer context at all ([none]) and +16% compared to [np] (the best method without global state). a comparison of the four different conditioning methods shows @xcite to be most effective, followed by [gs], [np], and [c]. the methods [c] and [np] do not have any bias towards treating the context vector specially. they attend to other positions in the sequence the same way they attend to the context. the superiority of [gs] and [gsu] can presumably be explained by their explicit architectural ability to retrieve information from the global state and therefore effectively utilize the context for their prediction. we acknowledge the differences between our outfits dataset and typical nlp benchmarks. nonetheless we hypothesize that the effectiveness of our method translates to nlp. in particular when applied to usecases in which the modality of context and sequence differ, e.g., for contexts comprised of numerical or categorical meta data about the text. that is because the model's freedom to read from the context separately allows it to process the different modalities of context and input sequence adequately. ## conclusions and future work with contextual bert, we presented novel ways of conditioning the bert model. the strong performance on a real-world use-case provides evidence for the superiority of using a global state to inject context into the transformer-based architecture. our proposal enables the effective conditioning of bert, potentially leading to improvements in a range of applications where contextual information is relevant. a promising idea for follow-up work is to allow for information to flow from the sequence to the global state. further, it would be desirable to establish a contextual nlp benchmark for the research community to compete on. this benchmark would task competitors with contextualized nlp problems, e.g., social media platform-dependent text generation or named entity recognition for multiple domains.