Representing ELM o embeddings as two-dimensional text online (2021)

Abstract: We describe a new addition to the WebVectors toolkit which is used to serve word embedding models over the Web. The new ELMoViz module adds support for contextualized embedding architectures, in particular for ELMo models. The provided visualizations follow the metaphor of ‘two-dimensional text’ by showing lexical substitutes: words which are most semantically similar in context to the words of the input sentence. The system allows the user to change the ELMo layers from which token embeddings are inferred. It also conveys corpus information about the query words and their lexical substitutes (namely their frequency tiers and parts of speech). The module is well integrated into the rest of the WebVectors toolkit, providing lexical hyperlinks to word representations in static embedding models. Two web services have already implemented the new functionality with pre-trained ELMo models for Russian, Norwegian and English.

## introduction in this demo paper we describe a new module recently added to the free and open-source webvectors toolkit @xcite foot_0 . web-vectors allows to easily deploy services to demonstrate the abilities of static distributional word representations (word embeddings) @xcite via web browsers. it currently powers at least two embedding model hubs: figure 1 : metaphor of two-dimensional text; borrowed from @xcite . the new module (we name it elmoviz) adds the functionality to study, probe and compare recently introduced contextualized embedding (or 'token-based') models @xcite . in particular, at this point we provide support for the elmo architecture @xcite based on deep recurrent neural networks. in the future, we plan to add support for transformer-based models like bert @xcite and @xcite . elmo architecture is significantly less computationally expensive than transformers, while being almost on par in terms of performance. thus, it yields rich possibilities in the context of non-commercial web services. for analyzing elmo representations of an arbitrary input text, we offer the metaphor of 'twodimensional text' first proposed in @xcite (see figure 1 ). this allows a sort of 'visualization' for contextualized embeddings through finding words which are most semantically similar to the input words in their current contexts. from the linguistic point of view, these are 'paradigmatic replacements' @xcite -words that can to some extent substitute target words. the two dimensions here are the syntagmatic one (horizontal) which describes the linear order of the sentence, and the paradigmatic one (vertical) which describes semantic classes to which the words in the sentence belong to. the generated substitutes in the vertical axis can also be thought of as 'semantic variations' of the input sentence. the rest of the paper is organized as follows. in section 2 we describe the background for this work, including the webvectors framework, and explain the need to develop additional functionality in order to handle contextualized embeddings. section 3 describes in detail this functionality, both from the point of view of the end user and from the point of view of deployment logistics. in section 4, we conclude and outline future work. ## background since the widespread adoption of prediction-based word embeddings @xcite started, there has always been a need to efficiently serve and demonstrate these representations over the web. researchers and practitioners need this for quick experimentation and testing hypotheses by comparing different distributional models. those who teach natural language processing and computational linguistics need ways to show the students how dense distributional representations capture lexical semantics without installing any software or downloading any models (often it is desirable that this is shown for a particular language or domain). in turn, language teachers value tools to demonstrate lexical variety and degrees of similarity for words in a foreign language. to this extent, serving word embeddings over the web can help both the teachers with preparing educational materials and the students with grasping the concepts in a foreign language. the webvectors framework we presented in (kutuzov and kuzmenko, 2017) is aimed at all these purposes. it allows to quickly deploy a stable and robust web service featuring operations on vector semantic models, including querying, visualization and comparison, all available to users of any computer literacy level. it extended already existing embedding visualization services like embedding projector foot_3 by providing users with the ability to find nearest semantic neighbors of query words, perform vector math operations over embeddings, etc. since being first presented in 2016, webvectors keeps adding new functionality, and now it offers filtering nearest associates by part of speech tags or corpus frequency, and can generate semantic ego graphs, among other features (see figure 2 ). until the introduction of elmoviz, these features were limited to the so-called 'static word embeddings', that is, architectures like word2vec @xcite , fasttext @xcite or glove @xcite . in these architectures, after the training is finished, each word type in the vocabulary is rigidly associated with a single dense vector. however, in the recent years nlp saw a surge of pre-trained 'contextualized' embedding architectures, like elmo @xcite , bert @xcite , gpt-3 (brown et al., 2020) and many others. one of the changes these deep learning models brought was that even at inference time, each word token representation (embedding) depends on its immediate context. this means that ambiguous words will receive different representations depending on the sense in which they are used, which opens rich new possibilities for natural language understanding. libraries used in webvectors to deal with static word embeddings (gensim, @xcite ) were not fit to power operations on contextualized models. that is why we decided to implement an entirely new webvectors module, which would take a query phrase as an input, and produce paradigmatic replacements (lexical substitutions) for each content word in this phrase, based on a given pre-trained contextualized elmo language model. one can find a number of existing frameworks for online experimentation with contextualized models: among others, we should mention language interpretability tool @xcite , exbert by @xcite and the hosted infer-ence api at the huggingface community model hub @xcite . however, these projects are aimed exclusively at the transformer-based architectures. the system we present in this demo paper, on the other hand, is aimed more towards rnn-based architectures like elmo. as it was shown, for example, in the field of semantic change detection @xcite , elmo can often outperform bert or be on par with it, while requiring significantly less computational resources. we believe it is especially important for teaching activities. additionally, our system is more lexically oriented and is integrated with the existing webvectors functionality, as we will show in the next section. ## system description after turning on the contextualized embedding related functionality in the webvectors configuration file, foot_4 the person deploying the service has to provide three data sources for each elmo model: the last item of this list requires some explanation. our aim is to provide the end user with a set of lexical substitutes for each word token in context from the input sentence (see figure 3 ). with static embedding architectures, this boils down to looking up the vector of the target word x and then finding n other words in the model vocabulary with the vectors closest to x. however, this is obviously impossible with contextualized language models: there are no static vector lookup tables to begin with. one can easily infer contextualized representations for each word in the input sentence: but what to compare them with in order to illustrate their meaning? to cope with this issue, we adopted the approach described in @xcite . they employed the so called type-level context averaging in order to align pre-trained contextualized models crosslinguistically. in our case, we needed only the first stage of their workflow. the idea is to obtain static type-level word representations located in the same vector space as the contextualized embeddings. given a large enough reference text corpus and a pre-trained contextualized language model, one takes the average of all token representations for each target word occurrence in the corpus. this averaged type embedding is comparable to contextualized token embeddings routinely produced by the model. in practice, we found that one does not even need to average token embeddings: it is enough to sum them, and then unit-normalize the resulting summed vector. as for the list of target words, we simply use top 10 000 (or any other amount found suitable) most frequent words from the corresponding elmo model vocabulary or from a reference corpus (excluding functional parts of speech and digits). low frequent words are usually not needed in this case anyway, since the quality of their embeddings is also lower. we provide a simple script to extract type embeddings from an elmo model and a given corpus in our github repository. 6 as a result, when an end user enters an input phrase or sentence (typically from 5 to 15 words), webvectors produces contextualized token embeddings for each token in the query, and finds top n words in the type embedding model, which are the closest (by cosine similarity) to each of the token embeddings. these predictions are lexical substitutes or paradigmatic replacements; they demonstrate what other words could fill these positions in the query, depending on the context. another option to produce such substitutes would be to feed the input sentence to the elmo model and then for each word token choose the strongest activations at the final softmax layer of the language model and map them to words in the model vocabulary. however, in practice we found that this approach is slightly slower than the one described above. additionally, elmo models are often published online without the vocabulary they were trained on. since the input layer of elmo is purely character-based, it does not hinder inferring token embeddings, but it effectively blocks using these weights as language models per se. our approach allows one to use any given elmo model with any desired corpus to produce a set of reference type embeddings. system maintainers can provide several models for the service to work with, including models for different languages; one of the models should be specified in the configuration files as the default one. when entering the query sentence, users can choose the model which will process the input. apart from choosing between different models, webvectors also allows users to choose the exact elmo layer from which token representations will be inferred; it was shown in @xcite that different neural network layers convey information related to different linguistic tiers: syntax, semantics, pragmatics, etc. at this point, one can choose between the top elmo layer and the average of all layers. note that for all operations with pre-trained elmo models we use simple_elmo: a lightweight tensorflow-based python package also developed by us. 7 if need be, simple_elmo can also be used as a standalone library to handle elmo models. both the words from the input sentence and the lexical substitutes are colored according to their frequency tier in the reference corpus (green for 'high', blue for 'mid' and red for 'low'), in accordance with other webvectors components. similarly, each word is hyperlinked to its 'landing page' 7 https://pypi.org/project/simple-elmo/ bound to one of the static embedding models served by a particular webvectors installation (like the one in figure 2 ), allowing easy and playful exploration of the semantic space. the font size of the lexical substitute corresponds to cosine similarity between the token embedding and the substitute type embedding: thus, users can instantly see what word tokens the model is unsure about. the service performs fast under-the-hood part-of-speech tagging of the query, foot_6 so for functional words we always yield themselves as substitutes (see 'her', 'that' and 'can' in figure 3 ). they are also uncolored and not hyperlinked, so that a user might focus on content words, while at the same time still having an impression of 'full sentence variations'. the users should be aware that the lexical substitutes potentially contain all the biases inherited from the corpus the model was trained on. thus, the paradigmatic axis might include slander words and stereotypes, if they were frequent enough in the data. we did not address this issue in the present work, but we advise the users to take this into account when dealing with any unsupervised language models. importantly, we keep a short history of substitute queries, so that it is possible to see at a glance the changes brought by a different context, a different word order or a different contextualized model (if the web service offers several models). figure 4 shows an example from our russian live demo at the rusvectōrēs web service. in the first sentence, the word закладку 'zakladku' is used in the newer sense of 'a secret place to store illegal drugs', while in the second sentence it is used in the older sense of 'the act of founding a building'. the generated substitutes reflect the differences in word meaning depending on the context. in the first example the substitutes include such words as 'meeting, sale, operation', and in the second example the substitutes are 'opening, building, repair'. ## conclusion the described system for generating twodimensional text using pre-trained elmo models is now deployed at the two model hubs mentioned in section 1. nlpl webvectors features elmo models trained on english wikipedia and on norwegian corpora foot_7 , while rusvectōrēs features a model trained on concatenated russian wikipedia and russian national corpus. foot_8 the presented component for the webvectors framework allows users to explore pre-trained elmo models and to visualize contextualized embeddings as a two-dimensional text for faster analysis of early research prototypes. while previously the framework provided interface only to static vector semantic models, introducing support for contextualized architectures allows for more intricate exploration of linguistic phenomena, such as lexical ambiguity and contextual semantic change. we hope that the new functionality will provide language teachers, nlp researchers and practitioners with a powerful tool to study word meaning in context and at the same time keep the audience up-to-date with recent advances in the field of distributional semantics and deep learning based nlp. a separate important contribution is our simple_elmo library which makes using elmo models in python much easier, especially for researchers with linguistic background. in the future, we plan to add support for other contextualized embedding architectures like bert, to allow inter-architectural comparisons. another interesting room for future work is integrating with other exploratory services for neural nlp models, like the ones mentioned in section 2.