The word play means very different things in “a spectacular play on the grounder” (a move in baseball), “a Broadway play” (a theater performance), and “concepts play an important role” (a verb). Yet word embeddings such as word2vec and GloVe give play exactly one vector, the same in every sentence.
Contextualized word representations fix this: the vector for a word is computed from the whole sentence it appears in, so the same word gets different vectors in different contexts. This reading explains the idea and its first influential model, ELMo (Embeddings from Language Models; Peters et al., 2018), which obtains contextual vectors from a pretrained bidirectional language model.
1From static to contextual embeddings
The neural models we have seen so far (recurrent networks, Transformers) take word embeddings as their input features: each token is first looked up in an embedding table, often initialized with word2vec or GloVe. These are static embeddings: there is one vector per word type.
- mouse in “a mouse controlling a computer system” and in “a quiet animal like a mouse” get the same vector;
- bank in “a bank can hold the investments” and in “the east bank of the river” get the same vector.
The vector ends up as a blend of all the word's meanings, weighted by how often each occurs in the training corpus.
Trained with context vs. computed in context
The distributional hypothesis, “You shall know a word by the company it keeps” (Firth, 1957), says that words occurring in similar contexts tend to have similar meanings. Static embeddings already rely on it: word2vec learns each vector by predicting the words in a fixed window around it. But the context is used only during training. Once trained, the table holds one vector per word, no matter the sentence.
Contextualized embeddings use the context at the time they are computed: the representation of a word token is a function of the entire sentence, so each occurrence of play gets its own vector.
bert-base-uncased, a later contextualized model with 12 Transformer layers; ELMo's LSTM layers behave similarly. Colored tags mark the sense of each occurrence.2ELMo: embeddings from language models
Where can a model learn how context changes a word's meaning, without any labeled data? ELMo's answer: from language modeling. To predict the next word well, a language model must keep track of what the sentence is about, so its hidden states are rich, context-dependent representations of each word.
A deep forward language model
ELMo's forward language model is a stack of two LSTM layers. Each token first gets a context-independent representation (computed from its characters by a small convolutional network, so even unseen words get a vector). The LSTM layers then read the sentence left to right, and the top layer predicts the next word with a softmax over the vocabulary. At each position $k$ there are three representations: the token layer $\mathbf{h}_{k,0}$ and the two LSTM layers $\mathbf{h}_{k,1}$, $\mathbf{h}_{k,2}$.
Forward and backward
A forward language model at position $k$ has only seen the words to its left. To also capture the right context, ELMo trains a second, backward language model that reads the sentence from right to left and predicts the previous word. The two are trained separately (sharing only the token representations and the softmax layer), and their hidden states are concatenated at each layer:
$$\mathbf{h}_{k,j} = \big[\,\overrightarrow{\mathbf{h}}_{k,j}\,;\,\overleftarrow{\mathbf{h}}_{k,j}\,\big].$$Note what this means: within each LSTM layer, the forward part never sees the right context and the backward part never sees the left context. The two directions only meet when their outputs are concatenated. This is sometimes called shallow bidirectionality.
3Combining the layers
Which layer should we use? ELMo's answer is all of them, mixed with weights that are learned for each downstream task:
$$\text{ELMo}_k = \gamma\sum_{j=0}^{2} s_j\,\mathbf{h}_{k,j}.$$- Concatenate the forward and backward states at each layer ($\mathbf{h}_{k,0},\mathbf{h}_{k,1},\mathbf{h}_{k,2}$).
- Multiply each layer's vector by a weight $s_j$; the weights are softmax-normalized, so they are positive and sum to 1.
- Sum the weighted vectors, and scale the result by $\gamma$.
Why learn the mixture instead of taking the top layer? Different layers capture different information. The ELMo paper found that the first LSTM layer is better for syntactic tasks such as part-of-speech tagging, while the second layer is better for semantic tasks such as word sense disambiguation. Each task can choose its own mixture.
4How to use ELMo
ELMo is used as a feature extractor:
- Pretrain the forward and backward language models on a large text corpus (once).
- Freeze them. For a task's input sentence, run the language models and compute $\text{ELMo}_k$ for every token.
- Concatenate each token's ELMo vector with its usual word embedding, and feed the result into the existing task model (e.g., a BiLSTM-CRF for named entity recognition).
- Train the task model as usual; the only ELMo parameters that are learned are the task-specific $s_j$ and $\gamma$.
Nothing about the task model's architecture has to change: ELMo simply provides better input features.
Contextual nearest neighbors
The ELMo paper compares the nearest neighbors of play. With GloVe, the neighbors mix every sense: playing, game, games, played, players, plays, player, Play, football, multiplayer. With the biLM, the nearest neighbor of the baseball sentence “Chico Ruiz made a spectacular play on Alusik's grounder” is another sports sentence, “…commended for his ability to hit in the clutch, as well as his all-round excellent play”, and the nearest neighbor of the theater sentence “Olivia De Havilland signed to do a Broadway play” is “…actors who had been handed fat roles in a successful play”. The contextual vectors separate the senses.
5Results and limitations
Simply adding ELMo vectors to existing models improved the state of the art across six diverse tasks (Peters et al., 2018, Table 1), with relative error reductions of roughly 6–20%:
| Task | What it is | Previous best | Baseline | Baseline + ELMo |
|---|---|---|---|---|
| SQuAD | question answering | 84.4 | 81.1 | 85.8 |
| SNLI | natural language inference | 88.6 | 88.0 | 88.7 |
| SRL | semantic role labeling | 81.7 | 81.4 | 84.6 |
| Coref | coreference resolution | 67.2 | 67.2 | 70.4 |
| NER | named entity recognition | 91.93 | 90.15 | 92.22 |
| SST-5 | 5-class sentiment | 53.7 | 51.4 | 54.7 |
ELMo showed that representations learned by a language model on unlabeled text transfer to almost any task. Its limitations pointed the way to what came next:
- LSTMs are sequential: each position waits for the previous one, so training and inference are slow, and long-range information must pass through many steps. Transformers remove both problems.
- Shallow bidirectionality: left and right context are combined only at the end, never inside the layers.
- Feature extraction only: the language model is frozen, and a separate task model must still be designed and trained. Later models instead fine-tune the entire pretrained network on each task.
6Summary
- Static embeddings give one vector per word type, blending all its meanings.
- Contextualized embeddings are computed from the whole sentence, so each occurrence of a word gets its own vector.
- ELMo trains a forward and a backward two-layer LSTM language model and concatenates their states at each layer.
- $\text{ELMo}_k=\gamma\sum_j s_j\mathbf{h}_{k,j}$ mixes the layers with task-specific weights; lower layers carry more syntax, higher layers more semantics.
- ELMo vectors are added to the inputs of existing task models with the language model frozen, improving results across many tasks.
7Practice
Exercise 1: static or contextual?
For each, say whether the representation is static or contextual: (a) a GloVe vector for bank; (b) the hidden state of a forward LSTM language model at the word bank; (c) the row of a Transformer's input embedding table for bank.
Exercise 2: the combination
Raw weights for three layers are $[0, 0, \ln 2]$ and $\gamma=1$. What are $s_0, s_1, s_2$? If the layer vectors (1-dimensional, for simplicity) are $1, 2, 4$, what is the ELMo value?
Exercise 3: what each direction sees
In “Let's stick to improvisation”, which words have influenced (a) the first-layer forward state at stick; (b) the first-layer backward state at stick; (c) ELMo's representation of stick?
Quick quiz
8Further reading
- Peters, Neumann, Iyyer, Gardner, Clark, Lee & Zettlemoyer (2018). Deep Contextualized Word Representations. NAACL.
- Alammar. The Illustrated BERT, ELMo, and co. (The source of the ELMo figures in the lecture.)
- Smith (2019). Contextual Word Representations: A Contextual Introduction. (A gentle overview written for newcomers.)
- Ethayarajh (2019). How Contextual are Contextualized Word Representations? EMNLP.