Stacked RNNs › ELMo as a Stacked Bidirectional LSTM Example

Michael BrenndoerferMay 13, 20255 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 82Stacked RNNs › ELMo as a Stacked Bidirectional LSTM Example
Reading section 9 of 16 · ELMo as a Stacked Bidirectional LSTM Example · 5 minSection 9 of 16 · 5 min

Book contents

Section 9 of 16 · 5 min read

Stacked RNNs › ELMo as a Stacked Bidirectional LSTM ExampleOne section · Sections in this chapter9 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

One of the most influential applications of stacked RNNs is ELMo (Embeddings from Language Models), introduced by Peters et al. in 2018. ELMo uses a 2-layer stacked bidirectional LSTM trained as a language model, and the resulting representations became one of the first major successes of contextualized word embeddings before the transformer era.

ELMo Architecture

ELMo combines several ideas discussed in earlier chapters:

  1. Bidirectional RNN (see Chapter 8 of this part): Two separate LSTMs, one reading forward and one reading backward
  2. Stacking: 2 layers of bidirectional LSTMs, where each direction is stacked independently
  3. Layer combination: ELMo doesn't use just the top layer. It uses a learned weighted combination of all layers

The architecture processes each sentence through:

  • A character-level CNN to produce token embeddings (avoiding the fixed vocabulary problem)
  • A forward 2-layer stacked LSTM reading left to right
  • A backward 2-layer stacked LSTM reading right to left
  • Concatenation of forward and backward states at each layer

For a token tt in a sequence of length TT, ELMo produces representations at three levels:

  • E0(t)E_0(t): The character-CNN embedding (context-independent)
  • E1(t)E_1(t): The concatenated forward/backward states from layer 1 (local context)
  • E2(t)E_2(t): The concatenated forward/backward states from layer 2 (global context)

The final ELMo embedding for downstream tasks is:

ELMo(t)=γ∑ℓ=02sℓ⋅Eℓ(t)\text{ELMo}(t) = \gamma \sum_{\ell=0}^{2} s_\ell \cdot E_\ell(t)

where:

  • sℓs_\ell are task-specific softmax weights (learned scalars that sum to 1)
  • γ\gamma is a task-specific scalar that scales the overall magnitude
  • The sum runs over all three levels: character embedding + 2 LSTM layers

The key insight is that different layers capture different types of linguistic information:

  • Layer 1 captures syntax: part-of-speech, dependency structure
  • Layer 2 captures semantics: word sense disambiguation, coreference

By learning to combine layers, different downstream tasks can emphasize the information that matters most. A POS tagger uses mostly layer 1. A coreference model uses mostly layer 2. The task itself determines which level of the hierarchy is most relevant.

Out[10]:
Visualization
Diagram showing ELMo layer combination with token representations at three levels combined via learned weights.
Simplified ELMo representation extraction. At each token position, ELMo combines representations from three levels: the character-level embedding, the first biLSTM layer output, and the second biLSTM layer output. The layer weights (s0, s1, s2) are learned per task, allowing different tasks to emphasize different levels of abstraction.

Why ELMo's Multi-Layer Approach Mattered

Before ELMo, most NLP systems used static word embeddings like Word2Vec or GloVe, which give every token the same representation regardless of context. ELMo demonstrated that contextual representations from stacked recurrent networks could dramatically improve downstream tasks. Improvements of 4–14% absolute on tasks like question answering, textual entailment, and coreference resolution were reported at the time of publication.

ELMo also highlighted an important property of stacked RNNs that pure depth metrics miss: different layers specialize. The lower layers specialize in syntax, the upper layers in semantics. Using all layers together, rather than just the final layer, extracted the most value from the stack.

Out[11]:
Visualization
Grouped bar chart showing ELMo layer weights for four NLP tasks across three layers.
Approximate learned ELMo layer weights for different NLP tasks, based on reported patterns in Peters et al. (2018). Tasks that require syntactic understanding (POS tagging, NER) rely more heavily on the lower biLSTM layer, while tasks requiring deeper semantic understanding (coreference, question answering) weight the upper layer more strongly. The character CNN layer contributes a small but consistent baseline across tasks.

The visualization confirms the layer specialization pattern. Syntactic tasks like POS tagging heavily favor Layer 1, while semantic tasks like coreference resolution rely primarily on Layer 2. This task-adaptive weighting is what makes ELMo more powerful than simply using the final layer's representation.