9.5 ELMo: biLM, ScalarMix, linear probes
The word vectors so far, from word2vec to the embedding table of every model, give a word one vector whatever the sentence: “saw” in “the saw cuts wood” and in “they saw the dog” is the same point. The hidden states of the recurrent language models do depend on the sentence. ELMo (2018) made those states the representation: run a forward and a backward language model, keep every layer’s states, and let each downstream task learn a mix of them. It was the step between word2vec and BERT, and the linear probe that shows what it learned is a tool interpretability work still uses.
Concepts from other sections (12)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- embedding table: the matrix whose row is the learned vector for token , shared across every window. Read more: 8.2 Bengio’s neural probabilistic language model
- hidden states: the block inputs and outputs, before the final norm, that a VLM can tap. Read more: 14.3 Vision transformer encoder
- Linear map: a function between vector spaces with ; once bases are chosen, every linear map is a matrix. Read more: Chapter 9: Linear Algebra in Mathematics
- Standard deviation: , the typical distance of a random variable from its mean, in the same units as the variable itself. Read more: Chapter 13: Probability and Statistics in Mathematics
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- IRLS: iteratively reweighted least squares, Newton’s method on the logistic loss, where each step is a weighted least-squares solve with weights . Read more: 13.8 Logistic regression (IRLS), ROC-AUC, calibration (ECE) in Mathematics
- Logistic regression: a model in which the log-odds are linear in the features. Read more: 13.8 Logistic regression (IRLS), ROC-AUC, calibration (ECE) in Mathematics
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Bidirectional layer: two recurrent modules, one reading left to right and one right to left, with outputs concatenated per position. Read more: 9.4 Bidirectional RNN with length-aware reversal
- Hessian: the symmetric matrix of second partial derivatives, whose eigenvalues classify a critical point. Read more: 10.3 Calculus 3 problem set: vectors, gradients, the chain rule, Hessians, multiple integrals, Lagrange in Mathematics
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- Residual connection: computing so that layers learn a residual and gradients flow directly. Read more: Chapter 2: Deep Learning
This section is optional. Nothing later in the book calls this code, and it is not on the main recurrent path; a natural follow-up project is to apply the probe, layer by layer, to every checkpoint in the model zoo. It is still worth reading: the probe is the cheapest way to see what a network has learned, and the biLM’s “each direction only sees its own side” rule is exactly the causality property that Masks: causal, padding, sliding window, additive tests for transformer masks.
The biLM is built from the LSTM and the length-aware reverse_padded of the bidirectional RNN. From Part 0 it uses cross_entropy from Losses with a fused backward, Embedding, Linear, and ModuleList from the module system, the op library, and Tensor. From Mathematics it uses PCG32 from Modular arithmetic, hashing, and PCG32 for initialization and AdamW from Adam and AdamW in the learning test; Logistic regression (IRLS), ROC-AUC, calibration is useful background for the probe. The solution is elmo.py, with BiLM (layers, forward), bilm_loss, ScalarMix, fit_linear_probe, and probe_accuracy; its contract is elmo.pyi.
Learning objectives
- Build a biLM from two stacks of single-direction LSTMs that share the embedding and the softmax, and explain why the directions must not mix inside the stack.
- Write the biLM loss with shifted targets and ignored edges for both directions.
- Implement ScalarMix as a softmax-weighted sum of layers times , and compute one by hand.
- Fit a deterministic linear probe and use it to compare what different layers encode.
Context from both sides
Section titled “Context from both sides”The notation:
| Symbol | Meaning | Type / shape |
|---|---|---|
| , | token ids of a sentence, its real length | int[B, T], int[B] |
| embedding and hidden width | int | |
number of LSTM layers per direction (n_layers) | int | |
| token embedding | float32[d] | |
| , | forward and backward layer- states at position , in source order | float32[d] |
| , | the representation of layer | float32[2d] |
| , | ScalarMix parameters | float32[L + 1], float32[1] |
| , | probe features (one row per token) and labels | float[N, D], int[N] |
A forward LM’s state summarizes ; a backward LM’s state summarizes . Concatenated, describes in its whole sentence. They cannot be one bidirectional network trained as a language model: if position saw it would just copy the answer. So the two directions are trained separately, each to predict in its own direction, and only their states are joined.
The biLM
Section titled “The biLM”Each direction is a stack of single-direction LSTMs. The forward stack reads left to right; the backward stack reads reversed inside each sentence’s length with reverse_padded, so it starts at the last real token, and its outputs are reversed back to source order. The forward LM predicts from , the backward LM from , both through the same output layer:
ScalarMix
Section titled “ScalarMix”Once the biLM is trained, you have representations per token, and different layers carry different information: lower ones more about the word, higher ones more about the context. ELMo lets each task choose:
The softmax makes the weights positive and sum to 1; rescales the result to whatever the task’s next layer expects. At initialization (every layer weighted ) and , the plain average. Both are trained with the task.
Linear probes
Section titled “Linear probes”To ask “does layer encode X?”, freeze the layer, take its vectors for many tokens, and fit the simplest classifier: softmax regression. If a linear map from predicts the label, the information is there in an easily usable form; a deep probe could learn the task itself and say little about the representation (Hewitt and Liang).
fit_linear_probe standardizes the features (each column to mean 0 and standard deviation 1, so one learning rate suits every column), runs full-batch gradient descent from zero on the cross-entropy with a small L2 penalty, and folds the standardization back into and so the probe applies to raw features. From a zero start with full batches it is deterministic, so an accuracy is a property of the layer. The book’s other probe is the IRLS logistic regression of Mathematics; this section uses its own gradient-descent version.
The payoff, checked by the learning test: the embedding layer cannot tell “saw” the noun from “saw” the verb, and the first LSTM layer can.
Working it by hand
Section titled “Working it by hand”Take ScalarMix over two layers, , , with and .
- .
- The mix: .
- Times : .
With the raw scalars as weights instead of their softmax () the result would be . A correct ScalarMix must give exactly here.
Designing the interface
Section titled “Designing the interface”The module exposes two classes and three functions:
class ScalarMix(Module): def __init__(self, n_layers: int) -> None: ... def weights(self) -> NDArray: ...; def forward(self, layers: Sequence[Tensor]) -> Tensor: ...class BiLM(Module): def __init__(self, vocab: int, d: int, n_layers: int = 2, rng=None) -> None: ... def layers(self, ids, lengths=None) -> list[Tensor]: ... # [R^0 .. R^L], each [B, T, 2d] def forward(self, ids, lengths=None) -> tuple[Tensor, Tensor]: ... # fwd and bwd logits [B, T, V]def bilm_loss(model, ids, lengths=None) -> Tensor: ...def fit_linear_probe(features, labels, n_classes, l2=1e-3, steps=200, lr=0.5) -> tuple[NDArray, NDArray]: ...def probe_accuracy(W, b, features, labels) -> float: ...BiLM.layers returns the representations for probing and mixing; BiLM.forward returns the two directions’ logits for training. Ids are batch first; lengths gives each row’s real length.
Building it step by step
Section titled “Building it step by step”Six observations shape the code.
- Each direction may see only its own side. A language model trained to predict must not have in its input. So the forward stack is fed only forward states, the backward stack only backward states, and the two are joined only after the last layer, for use as features. No layer mixes them.
- The backward stack is a forward stack on reversed input. Rather than a second recurrence that runs right to left, reverse each sentence inside its length, run an ordinary
LSTM, and reverse the outputs back so position inb_layersis the state for token . Padding stays at the end the whole time, so the LSTM’slengthsmask still applies. - Targets are shifts, and their edges are ignored. Forward position predicts , which exists only for ; backward position predicts , which exists only for . Everything else gets the target , which
cross_entropyskips (ignore_index). The two means are averaged, matching the in the loss. - Layer 0 must have the same width as the others. for is , width ; the embedding is width . Repeating it, , makes every the same shape so ScalarMix can sum them.
- ScalarMix weights must be a convex combination. Softmax of free scalars gives positive weights summing to 1 with no constraint in the optimizer; restores a free overall scale. With , the start is the plain average.
- A probe must be deterministic to be a measurement. Standardize features so one learning rate suits every column, start from , use full-batch gradient descent: identical data gives identical weights. Then fold the standardization into so the returned probe works on raw features, as the caller expects.
The two stacks
Section titled “The two stacks”Everything is time first ([T, B, d]) because that is what LSTM takes:
def _stacks(self, ids, lengths): a, n = _ids(ids, self.vocab, lengths) # validated int64 ids [B, T] and lengths [B] e = self.emb(a.T) # [T, B, d] f_layers, b_layers = [], [] f = e # The backward LM reads each sentence from its last REAL token, so the # reversal stays inside each length (L3.4), not a flip of the padding. b = reverse_padded(e, n) for k in range(self.n_layers): f, _ = self.fwd[k](f, lengths=n) # layer k+1 reads only forward layer k b, _ = self.bwd[k](b, lengths=n) # layer k+1 reads only backward layer k (still reversed) f_layers.append(f) b_layers.append(reverse_padded(b, n)) # back to source order, for output only return e, f_layers, b_layers, nNote that the loop keeps b in reversed time and only the copy appended to b_layers is reversed back. The next backward layer must keep reading in reversed order.
Representations and logits
Section titled “Representations and logits”def layers(self, ids, lengths=None): e, fs, bs, _ = self._stacks(ids, lengths) # R^0 = [e; e] so every layer is 2d wide; R^k = [f^k; b^k]. reps = [F.concat([e, e], axis=-1)] + [F.concat([f, b], axis=-1) for f, b in zip(fs, bs)] return [F.transpose(r, 0, 1) for r in reps] # each [B, T, 2d]
def forward(self, ids, lengths=None): _, fs, bs, _ = self._stacks(ids, lengths) # One shared output layer for both directions, applied to the top states only. return self.out(F.transpose(fs[-1], 0, 1)), self.out(F.transpose(bs[-1], 0, 1))The loss
Section titled “The loss”The loss builds both target arrays with wherever there is nothing to predict:
def bilm_loss(model, ids, lengths=None): a, n = _ids(ids, model.vocab, lengths) B, T = a.shape fl, bl = model(a, n) t = np.arange(T)[None, :] # Forward: position t predicts t + 1 (inside the length); backward: t - 1. fwd_tgt = np.full((B, T), -100, dtype=np.int64) fwd_tgt[:, :-1] = np.where(t[:, :-1] + 1 < n[:, None], a[:, 1:], -100) bwd_tgt = np.full((B, T), -100, dtype=np.int64) bwd_tgt[:, 1:] = np.where(t[:, 1:] < n[:, None], a[:, :-1], -100) if (fwd_tgt >= 0).sum() == 0: # Every length is 1: no token to predict, and a mean over nothing is NaN. raise ValueError("bilm_loss: no row has a token to predict (every length is 1)") V = model.vocab lf = cross_entropy(F.reshape(fl, (-1, V)), fwd_tgt.reshape(-1)) lb = cross_entropy(F.reshape(bl, (-1, V)), bwd_tgt.reshape(-1)) return (lf + lb) * 0.5ScalarMix
Section titled “ScalarMix”ScalarMix is a softmax and a weighted sum, all inside autograd so , and every layer get gradients:
class ScalarMix(Module): def __init__(self, n_layers): # ... self.scalar_parameters = Tensor(np.zeros(n_layers, dtype=np.float32), requires_grad=True) self.gamma = Tensor(np.ones(1, dtype=np.float32), requires_grad=True)
def forward(self, layers): # ... (count and shape checks) # Softmax of the scalars: the mix is a convex combination, rescaled by gamma. w = F.softmax(self.scalar_parameters, axis=0) mixed = layers[0] * w[0] for k in range(1, self.n_layers): mixed = mixed + layers[k] * w[k] return mixed * self.gammaThe probe
Section titled “The probe”The probe is plain NumPy in float64, since nothing upstream needs its gradient:
def fit_linear_probe(features, labels, n_classes, l2=1e-3, steps=200, lr=0.5): X = np.asarray(features, dtype=np.float64) # ... (shape, label and n_classes checks) N, D = X.shape mu = X.mean(axis=0) sd = X.std(axis=0) sd = np.where(sd > 0, sd, 1.0) # a constant column would divide by 0 Z = (X - mu) / sd Y = np.zeros((N, n_classes)); Y[np.arange(N), y] = 1.0 W = np.zeros((D, n_classes)); b = np.zeros(n_classes) # zero start: deterministic for _ in range(steps): s = Z @ W + b s = s - s.max(axis=1, keepdims=True) # stable softmax P = np.exp(s); P /= P.sum(axis=1, keepdims=True) G = P - Y # dCE/dlogits W -= lr * (Z.T @ G / N + l2 * W) # descent, with L2 on W only b -= lr * G.sum(axis=0) / N # Fold the standardization in: Z W + b = X (W / sd) + (b - (mu / sd) W). W_raw = W / sd[:, None] return W_raw, b - mu @ W_rawprobe_accuracy is then argmax(X @ W + b) == labels, averaged. The full solution is elmo.py.
Why this design
Section titled “Why this design”Two stacks of LSTMs cost for the recurrences, plus for the shared softmax. The probe is , trivial next to the model.
Why not one bidirectional LSTM stack, a bidirectional layer per level? Layer would read , so the forward state at would depend on , which has read . The forward LM could then copy its target and the loss would go to 0 while learning nothing (the first warning below).
The embedding and the softmax are shared because that halves those parameters and forces both directions into one vocabulary space, which is what makes a single coherent vector. Only the LSTMs are direction-specific.
The probe is linear rather than an MLP because a probe that can learn the task itself measures the probe, not the layer (Hewitt and Liang); the linear one answers “is it there in an easily usable form?”. It uses gradient descent rather than a closed form or IRLS because softmax regression has no closed form, and IRLS converges faster but needs a Hessian solve per step. Fixed-step gradient descent is simple, deterministic and accurate enough to compare layers.
What can go wrong
Section titled “What can go wrong”The checks a correct solution passes:
| Case | Kind of check | Checks | Why it matters |
|---|---|---|---|
| hand example of the mix | unit | , weights | the mix is pinned to a case you can compute |
| ScalarMix starts uniform and trains | gradcheck | the average at init; gradients of , , and every layer in float64 | a task can learn its mix |
| matches torch | golden | layers, both logits, the loss, a mix, and every gradient against torch’s packed nn.LSTMs | the biLM is ELMo’s |
| forward layers never see the future | property | changing later tokens leaves bitwise equal | the forward LM cannot copy its answer |
| backward layers never see the past | property | changing earlier tokens leaves bitwise equal, with lengths 6, 4, 2 | the backward LM reads from each sentence’s own end |
| padding never leaks | property | padding content changes no real position; padded are 0; a row alone equals it in a batch | batches of mixed lengths |
| loss targets | unit | the loss recomputed with explicit targets; length-1 rows only raise | both directions are scored on the right token |
| probe separates separable data | unit | accuracy 1 on separable clusters far from the origin; folded weights work on raw features | the probe measures the features |
| probe is deterministic and validates | boundary | identical probes from identical data; bad shapes, labels, one class | probe accuracies are reproducible |
| contextual layers disambiguate | learning | after 150 steps: loss at the reference bar; probe on at the majority rate, on at least 0.95 | the reason contextual vectors exist |
The pitfalls:
Warning: A forward layer that also reads the backward states lets the forward LM see the future, and its loss collapses to 0. The forward stack reads only forward layers; directions are joined only in
layers.
Warning: Reversing the padded tensor as a whole, not reversing back, or running the backward LSTMs without lengths makes the backward LM read padding first, or puts states at the wrong positions. The solution uses
reverse_padded(e, n)in,reverse_padded(b, n)out, and passeslengths=nto every LSTM call.
Warning: Scoring a direction on the current token trains each LM to predict what it already read. The targets are
a[:, 1:]for forward anda[:, :-1]for backward, with at the edges.
Warning: Returning probe weights for standardized features makes predictions on raw features wrong. The solution folds
W / sdandb - mu @ W_rawbefore returning.
Three smaller slips are worth naming. Using the raw scalars as mix weights gives negative or unnormalized layer weights; the solution applies F.softmax(self.scalar_parameters) before mixing. Forgetting to apply means the task cannot rescale the mix; the solution ends with return mixed * self.gamma. And gradient ascent in the probe makes accuracy fall as it trains; the update W -= lr * (...) subtracts the gradient.
Where this leads
Section titled “Where this leads”This section composed the LSTM (one per layer per direction), reverse_padded from the bidirectional RNN, cross_entropy with ignore_index from Losses with a fused backward, and the module system’s Embedding, Linear, and ModuleList. Ahead, BERT: the bidirectional encoder and masked language modeling replaces two one-directional LMs with one masked LM that reads both sides at every layer, and the probe can be pointed at every checkpoint of the model zoo, layer by layer, for part of speech and position.
Production equivalents:
| This piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
BiLM | AllenNLP’s ELMo | a character CNN instead of a token embedding, projections and residual connections between layers, 4096-wide LSTMs | allennlp/modules/elmo.py, elmo_lstm.py |
ScalarMix | AllenNLP ScalarMix | optional layer normalization of each layer before mixing | allennlp/modules/scalar_mix.py |
fit_linear_probe | edge probing, control tasks | span-pair probes, selectivity against random control labels | Tenney et al. 2019; Hewitt and Liang 2019 |
| contextual vectors | BERT, sentence-transformers | one bidirectional encoder trained by masked LM, pooled sentence embeddings | BERT; the embedding endpoint in RAG ingest |
Further reading
Section titled “Further reading”- Peters et al., Deep contextualized word representations (NAACL 2018).
- Tenney et al., “What do you learn from context? Probing for sentence structure in contextualized word representations” (ICLR 2019).
- Hewitt and Liang, “Designing and Interpreting Probes with Control Tasks” (EMNLP 2019).
Key terms
Section titled “Key terms”- biLM: a forward and a backward language model over the same sentence, sharing the embedding and the softmax, trained separately in each direction.
- Contextual representation: a token vector that depends on the whole sentence, here .
- ScalarMix: , a learned per-task convex combination of layers with a scale.
- Linear probe: a softmax regression fitted on frozen features to measure what a layer encodes in easily usable form.
ignore_index: the target value () thatcross_entropyskips, used for positions with nothing to predict.
Summary
Section titled “Summary”- A biLM is two language models over the same sentence: the forward one predicts the next token, the backward one the previous token, and they share the embedding and the softmax.
- Each direction must see only its own side: forward states never depend on later tokens, backward states never on earlier ones, and the backward stack reverses inside each length.
- ScalarMix is a softmax-weighted sum of the layers times , learned per task, starting from the plain average.
- A linear probe measures what a frozen layer encodes: the embedding layer cannot tell “saw” the noun from “saw” the verb, the first LSTM layer can.
- The probe is deterministic (standardized features, zero start, full batches), so its accuracy is a property of the layer.
Review questions
Section titled “Review questions”- Why is the forward stack never fed backward states, even though BERT-style models mix both directions in every layer?
Answer
Each ELMo direction is trained as a language model. If forward layer $k+1$ read $b^k_t$, which depends on $x_{t+1}$, the forward LM would see its own target. BERT avoids this by masking the input tokens instead of separating directions. - In
_stacks, why isreverse_padded(b, n)appended tob_layersinstead of being assigned back tob?Answer
The next backward layer must keep reading in reversed time. Only the copy kept as output is put back into source order. - Why are the backward targets
a[:, :-1]placed at positions1:, and why mask witht < n?Answer
Backward position $t$ predicts the previous token $x_{t-1}$, so the target at $t$ is `a[t-1]`. Position 0 has nothing before it, and positions at or past $\ell$ are padding, so both get $-100$. - Why does ScalarMix need when the softmax weights already sum to 1?
Answer
The softmax fixes the weights to a convex combination, which keeps the mix's scale tied to the layers' scale. The task's next layer may want something larger or smaller, and $\gamma$ is a single free factor for that. - What does the fold at the end of
fit_linear_probecompute, and what breaks without it?Answer
It rewrites $Z W + b$ with $Z = (X - \mu)/\sigma$ as $X (W/\sigma) + (b - \mu (W/\sigma))$. Without it the returned weights expect standardized features, and `probe_accuracy` on raw features gives wrong predictions.