ELMo: Contextual Word Embeddings
ELMo (Embeddings from Language Models) is a deep contextualized word representation technique introduced by Peters et al. (2018) that computes word embeddings as a learned linear combination of internal states from a bidirectional LSTM language model, producing representations that vary based on surrounding context rather than assigning a single static vector per word. It matters because ELMo was the first widely adopted method to demonstrate that rich, task-agnostic pre-trained language representations could dramatically improve performance across nearly every NLP benchmark simultaneously. The counterintuitive fact: ELMo does not replace your task model's embedding layer — it augments it, and the optimal weighting of its internal layers differs per task, meaning syntax and semantics are actually stored at different depths.
Every prior word embedding method (Word2Vec, GloVe) assigns one fixed vector to each word regardless of context — "bank" gets the same representation whether you're discussing finance or rivers. ELMo's breakthrough is that a word's representation is computed on the fly from the hidden states of every layer of a bidirectional language model, so the same surface form produces fundamentally different vectors in different sentences. The model is pre-trained once on a massive corpus, and then the internal representations are exported as features for any downstream task. The "aha moment" is realizing that the internal layers specialize: lower layers capture syntax, upper layers capture semantics, and the task-specific weighting learns which information it needs.
How It Works
ELMo builds on a two-stage pipeline: first, a character-level CNN converts each token's characters into an initial token representation; second, a stacked bidirectional LSTM processes the full sequence, and all internal states are linearly combined with learned weights to produce the final embedding.
Stage 1 — Character CNN Encoding Each word is encoded character-by-character using convolutional filters followed by highway networks, producing a context-independent representation for token . This sidesteps out-of-vocabulary problems entirely.
Stage 2 — Bidirectional Language Model The forward LM predicts each token given its left context:
Explanation: This is the log-likelihood objective for the forward pass — maximize the probability of each token given all preceding tokens, summed over the full sequence of length N.
- : the token at position
- : parameters of the character CNN token encoder
- : parameters of the forward LSTM stack
- : parameters of the softmax output layer
The backward LM runs the same computation in reverse:
Explanation: The backward pass predicts each token from its right context, giving the model knowledge of what comes after — the two objectives are jointly optimized. The combined BiLM objective simply maximizes the sum of both directions, tying the token encoder and softmax parameters but keeping LSTM parameters separate:
Explanation: Jointly optimizing forward and backward language modeling forces the LSTM to build representations that are useful for predicting context in both directions simultaneously.
Stage 3 — ELMo Representation For a model with BiLSTM layers, token has raw representations: the character CNN output plus the forward and backward hidden states at each layer. These are collapsed into a single task-specific vector:
Explanation: The final ELMo vector is a scalar-weighted sum of all layer representations, scaled by a task-specific amplitude γ — both the weights s and the scale γ are learned during fine-tuning on the downstream task.
- : scalar that scales the entire ELMo vector (learned per task)
- : softmax-normalized scalar weight for layer (learned per task, )
- : concatenation of forward and backward hidden states at layer for token
- : number of BiLSTM layers (2 in the original ELMo paper)
In practice the ELMo vector is concatenated with the task model's existing word embeddings rather than replacing them: ```python import torch import torch.nn as nn from allennlp.modules.elmo import Elmo, batch_to_ids
# Load pre-trained ELMo options_file = "elmo_2x4096_512_2048cnn_2xhighway_options.json" weight_file = "elmo_2x4096_512_2048cnn_2xhighway_weights.hdf5" elmo = Elmo(options_file, weight_file, num_output_representations=1, # one task-specific mix dropout=0.5) sentences = [["The", "bank", "is", "steep"], ["The", "bank", "approved", "the", "loan"]] character_ids = batch_to_ids(sentences) # shape: (B, T, 50) embeddings = elmo(character_ids)
# embeddings['elmo_representations'][0]: (B, T, 1024)
# "bank" will have different 1024-d vectors in the two sentences ```
Matthew Peters was a research scientist at the Allen Institute for AI (AI2) with a background in physics before transitioning to NLP, making him emblematic of the interdisciplinary nature of modern deep learning research. His 2018 paper "Deep Contextualized Word Representations" (ELMo) demonstrated for the first time that a single pre-trained language model could simultaneously set new state-of-the-art results on six diverse NLP benchmarks including NER, SRL, coreference resolution, and sentiment analysis. Surprisingly, Peters had published a predecessor paper in 2017 using only the top LSTM layer as context embeddings, and the leap to using all layers — the crucial insight — came from an ablation study done almost as an afterthought.
Intuition
Imagine you're a linguistics expert who reads a sentence and must label every word's meaning. You don't decide what "bank" means until you've read the whole sentence — you hold all possible meanings in your head simultaneously and resolve them from context. ELMo is like having three specialists examine each word at different levels of linguistic detail: the first specialist (the character CNN) knows how the word is spelled and can guess its part of speech even if they've never seen it before. The second specialist (LSTM layer 1) understands grammar — they see how the word fits into the sentence's syntactic skeleton. The third specialist (LSTM layer 2) understands meaning in context — they know that "bank" in a sentence about loans must be a financial institution. When you (the downstream task) ask for ELMo's opinion, you ask all three specialists, but you assign different trust weights to each depending on what you care about: a named-entity recognizer trusts the grammar expert most, while a semantic role labeler trusts the meaning expert most. Crucially, all three specialists were trained together by asking them to predict the next and previous words in billions of sentences — a surprisingly powerful self-supervised signal that forces them to build rich internal world models.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Word2Vec · GloVe · BERT: Bidirectional Encoder Representations from Transformers · Long Short-Term Memory Networks · Attention Mechanism · ULMFiT · Contextual Embeddings