CSCE 471 Natural Language Processing · Spring 2027

Contextualized Representations

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

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.

After reading this, you should be able to explain the limitation of static word embeddings · distinguish “trained with context” from “computed in context” · describe how ELMo builds representations from forward and backward LSTM language models · write the ELMo combination of layers and explain its task-specific weights · describe how ELMo is added to an existing task model · explain why ELMo improved many NLP tasks and what its limitations were

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.

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.

Representation:
Figure 1 (interactive). Cosine similarity between the vectors of play and bank in ten sentences. Position 0 of the slider is the static embedding (the model's input embedding table): every occurrence of a word gets the identical vector. Positions 1–12 are the layers of a contextual model. The vectors were computed with 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.

Click a word
Figure 2 (interactive). What each direction has seen at the selected position. The forward LSTM's state (red) summarizes the selected word and everything to its left; the backward LSTM's state (purple) summarizes the selected word and everything to its right. ELMo concatenates the two.

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}.$$
  1. Concatenate the forward and backward states at each layer ($\mathbf{h}_{k,0},\mathbf{h}_{k,1},\mathbf{h}_{k,2}$).
  2. Multiply each layer's vector by a weight $s_j$; the weights are softmax-normalized, so they are positive and sum to 1.
  3. 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.

Raw weights:layer 0layer 1layer 2γ
Figure 3 (interactive). The ELMo combination for one token, with toy 8-dimensional layer vectors (colors show positive and negative values). The raw weights are passed through a softmax to give $s_0,s_1,s_2$, which mix the three layers; $\gamma$ rescales the result. During training on a task, these four numbers are learned while the language model stays fixed.

4How to use ELMo

ELMo is used as a feature extractor:

  1. Pretrain the forward and backward language models on a large text corpus (once).
  2. Freeze them. For a task's input sentence, run the language models and compute $\text{ELMo}_k$ for every token.
  3. 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).
  4. 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%:

TaskWhat it isPrevious bestBaselineBaseline + ELMo
SQuADquestion answering84.481.185.8
SNLInatural language inference88.688.088.7
SRLsemantic role labeling81.781.484.6
Corefcoreference resolution67.267.270.4
NERnamed entity recognition91.9390.1592.22
SST-55-class sentiment53.751.454.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:

6Summary

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.

(a) Static. (b) Contextual: it depends on the words before bank. (c) Static: the lookup table returns the same row in every sentence; only the layers above it make it contextual.

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?

$e^0, e^0, e^{\ln 2} = 1, 1, 2$, sum $4$, so $s=[0.25, 0.25, 0.5]$. ELMo $=0.25\cdot 1+0.25\cdot 2+0.5\cdot 4=2.75$.

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?

(a) Let's, stick. (b) stick, to, improvisation. (c) All words, but only through the concatenation of the two directions.

Quick quiz

8Further reading