Sequence models: the pre-transformer lineage

How sequences were modelled before the transformer: recurrent networks and the gradient through time, the LSTM and GRU gates, seq2seq and the attention that removed its bottleneck, convolutions for sequences, why the transformer replaced them, and the static word vectors they all read. Every animation runs real trained weights. Part of Topic: ml-fundamentals. Data as of October 2026 (toy runs of 3 October 2026). Reading: 24 min, plus 5 in closed details. Best resources: 3h 00m.

The idea in one screen

A recurrent network carries the whole past in one state, updated one step at a time, so the first token reaches the last output only through one multiplication per step, forward and backward. Each model in this lineage shortened or protected that path: gates gave it an additive lane (LSTM, GRU), attention let the output read every state directly (Bahdanau 2014), dilated convolutions cut it to log2 n layers with no recurrence (WaveNet 2016), and the transformer kept only attention: one hop between any two positions, every position computed at once in training.

The figure beside this is the whole page in five lanes: each design below it in the order the page takes them, with the path from the first token to the last output drawn in blue.

You need: backpropagation as the chain rule (the parent's Topic: ml-fundamentals Reading tab) and the slopes of sigmoid and tanh (Activation functions). Attention in its modern forms (multi-head, GQA, MLA) is Attention variants's; this page has only the 2014 form that bridged RNNs to transformers.

BPTT
backpropagation through time: backpropagate through the network unrolled over the sequence.
Gate
a sigmoid output in (0, 1) that multiplies a stream element by element.
Cell state
the LSTM's second state, updated by addition.
Seq2seq
an encoder network reads, a decoder network writes.
Receptive field
how many past inputs one output of a convolution stack sees.

The animations and 3D figures run real networks trained offline for this page (PyTorch, minutes on a laptop CPU; src/model/). Drag a 3D figure to turn it.

The path from the first token to the last output shrinks from 8 hops to 1, and the steps that must run in order from 8 to none

Blue: the path from x1 to the output at position 8. For length n, the transformer paper's Table 1: path O(n) recurrent, O(logk n) dilated convolution, O(1) self-attention; sequential steps O(n), O(1), O(1) (Vaswani et al. 2017). "In order" means in training; generation is one token at a time for all of them.

Recurrence: one state, carried forward

The running example is a task that needs memory and nothing else: a key (a, b, c or d), then a run of random distractors (w, x, y, z), then "?", and the network must answer the key. An 8-unit network reading b w w z y y x w w ? has to carry "b" across every distractor in 8 numbers. A recurrent network does it with one update applied at every step (Elman 1990):

at = f(U xt + W at−1 + b),   yt = g(V at)
at
the hidden state, the network's whole memory of the past
U, W, V
input, recurrent and output weights, the same at every step: a fixed number of parameters whatever the sequence length (the toy plain RNN has 188 with its 4-way readout)
f, g
usually tanh for the state; softmax for a distribution over outputs

Fig. 2 runs that sequence through a trained plain RNN, the one trained on sequences of 5 to 10 symbols (the Measured section has why that matters), with its real weights and states.

  1. Ten symbols, one per step. The key "b", eight distractors and "?" enter from below, left to right. Nothing else reaches the network.
  2. One cell. At step 1 the input weights U (orange glass) read the key and the recurrent weights W (blue glass) read the previous state, all zeros: the new state a1 is 8 numbers, drawn as bars above and below a white plinth.
  3. The same cell at every step. Ten copies of one cell with the same U and W: the state flows from each copy into the next, and each new state mixes the old one with one more symbol. This is the unrolled network; its size grows with the sequence, its weights do not.
  4. The answer at "?". V reads the last state and a softmax gives the four keys' probabilities: "b" gets 0.999. The key crossed nine updates to get there.
  5. Backpropagation through time. The gradient goes back from the answer through the same ten copies, once through W and the tanh slope per step. Each column is the factor it has been multiplied by on the way, ‖∂a10/∂ak‖ on this sequence (log scale): 0.16 three steps back, 0.011 at its smallest and 0.023 at the key.
  6. Every copy adds to the same weights. The copies share W, so the gradient each copy contributes is added into the one matrix: the update that teaches the network to store "b" arrives from the key's copy, already shrunk.

It is trained by backpropagation through time (BPTT; Werbos 1990): unroll the network into one layer per time step, all sharing weights, run ordinary backpropagation, and add up the gradients each copy contributes to the shared weights. Long sequences are usually cut into windows (truncated BPTT), which also caps how far back any gradient can reach.

The gradient shrinks by a roughly constant factor per step, and the LSTM's factor is closest to 1

Measured: norm of the loss gradient with respect to the hidden state k steps before the answer, relative to the answer's, mean of the same 256 sequences of length 100, untrained networks (seed 0; the LSTM's forget-gate bias starts at 1, so its gates begin near σ(1) ≈ 0.73), PyTorch autograd (grad_init, src/model/train_memory.py).

The gradient through time: vanishing and exploding

The loss at "?" teaches the network to store the key only through the gradient that reaches the key's step, and that gradient crosses one Jacobian per step: ∂at/∂ak = ∏j diag(f′)W. Fig. 2 showed it on one sequence; the chart beside this measures it over 100 steps on the toy's untrained networks: the plain RNN before, the LSTM after.

Predict how many orders of magnitude separate the plain RNN from the LSTM 50 steps back, then watch it.

  1. At the answer. The gradient of the loss with respect to the last hidden state is the unit: every value on the chart is relative to it.
  2. 10 steps back. Each step multiplies by one Jacobian. On average the plain RNN loses a factor of 0.58 per step, the GRU 0.64 and the LSTM 0.79 derived (from the curves between 10 and 50 steps back).
  3. 30 steps back. A constant factor per step compounds: the curves are straight lines on a log scale, which is what "exponential" means. The gap is already 3.0 orders of magnitude.
  4. 50 steps back: the key of a 50-symbol sequence. This is what the training signal must cross to teach the network to store the key: 10−11.8 for the plain RNN, 10−9.6 for the GRU, 10−6.1 for the LSTM, 5.7 orders of magnitude apart.
  5. 86 steps back. The plain RNN's recorded gradient is now exactly zero, too small for the arithmetic to hold; the LSTM's is still 10−9.4.
  6. 99 steps back. Even the LSTM is at 10−10.7, far below 1: untrained, its forget gates start near σ(1) ≈ 0.73, so the gate path protects the gradient but does not keep it whole until training opens the gates.

The rule behind the picture (Pascanu, Mikolov and Bengio 2013, §2.1): if the largest eigenvalue of W (in absolute value) is below 1/γ, where γ bounds |f′| (1 for tanh, 1/4 for the sigmoid), the long-term terms vanish exponentially; above it is necessary, not sufficient, for them to explode. Exploding gradients have a cheap fix the same paper proposed, clipping the gradient's norm (the toys clip at 1); vanishing ones need a change of architecture. The difficulty was first identified in Hochreiter's 1991 diploma thesis and by Bengio, Simard and Frasconi 1994.

The parent page has the scalar version of this, one 60-step sequence with the recurrent weight and the forget-gate bias as sliders (Topic: ml-fundamentals, Reading tab, thread 3, "Vanishing gradients, seen four ways"); deep feed-forward stacks have the same problem through depth (Normalisation and initialisation).

The LSTM, gate by gate

The long short-term memory network gives each unit a memory cell C whose update is an addition, guarded by sigmoid gates (Hochreiter and Schmidhuber 1997). Fig. 3 is one step of the toy LSTM (8 units, trained the same way as the other cells on sequences of 5 to 50 symbols), with its real activations on the running example at length 40: b w w z y y x w w ... ?. The input xt and the previous hidden state ht−1 are concatenated and fed to all four in parallel; the cell state runs along the back like a conveyor.

  1. Four layers read the same input. The key "b" and the previous state (zeros) go into four glass plates at once: the forget gate f, the input gate i and the output gate o (sigmoids, bars from 0 to 1) and the candidate C̃ (tanh, bars above or below the plinth). Each makes 8 numbers, one per unit.
  2. The key is written. On unit 4 (outlined) the input gate is 0.80 and the candidate +0.82, so i ⊙ C̃ puts +0.65 on the conveyor. With key "a" the same unit's candidate is −0.59: the key is written as a sign.
  3. A distractor: keep, and add again. At "w" the forget gate keeps 0.90 of the cell and the input gate, 0.86, is not shut: 0.90 × 0.65 + 0.86 × 0.75 = 1.23. The candidate has the key's sign because it reads h, which carries the key.
  4. Forty steps on the conveyor. Unit 4's cell at every step, key "b" in front and key "a" behind, on the same distractors: one grows to +15.2, the other to −5.8. The forget gates average 0.95, so the first write alone would have shrunk to 0.15 of its size; the memory is the running sum, re-written in the key's direction at every step.
  5. At "?", the output gate reads it out. o = 0.99 on unit 4 and tanh of a large cell is ±1, so h = 0.99; the readout over all eight units answers "b" with probability 1.00.

Not the textbook picture: this trained LSTM never shuts its input gate on the distractors (it averages 0.88 on unit 4); it keeps writing in the key's direction, and a saturated tanh reads a large cell the same way. The protected path is what matters for learning: along the cell, ∂Ct/∂Ct−1 is the forget gate alone.

The same four layers as formulas:

PartFormulaRole
Forget gateft = σ(Wf[ht−1, xt] + bf)how much of the old cell Ct−1 to keep
Input gateit = σ(Wi[ht−1, xt] + bi)which units to write
CandidateC̃t = tanh(WC[ht−1, xt] + bC)what would be written
Output gateot = σ(Wo[ht−1, xt] + bo)what part of the cell to expose
UpdatesCt = ft ⊙ Ct−1 + it ⊙ C̃t;   ht = ot ⊙ tanh(Ct)the cell is added to, not rewritten

Why it works. Along the cell, ∂Ct/∂Ct−1 = diag(ft) plus terms through the gates' dependence on h: no weight matrix and no squashing slope on the direct path, only the forget gate, which the network can hold near 1. It is a skip connection through time, the idea residual networks later used through depth. It does not abolish vanishing gradients: the path through h still multiplies Jacobians, and a forget gate of 0.9 still shrinks the cell path by 0.950 ≈ 0.005 over 50 steps. Setting the forget-gate bias to 1 or more at initialisation, so that the cell starts by remembering, is the standard recommendation (Jozefowicz, Zaremba and Sutskever 2015) and is used in the toy.

Correction: the 1997 LSTM had no forget gate
Its input and output gates guarded a "constant error carrousel", a cell whose self-connection was fixed at 1.0 (§3.2, §4). The forget gate came with Gers, Schmidhuber and Cummins 2000, and that version is the LSTM in every library today.
Peepholes, and the history of the variants (under 1 min)

"Peephole" connections from the cell to the gates were added by Gers and Schmidhuber in 2000 too; library LSTMs usually leave them out. Greff et al. 2015, §III, has the history of the variants.

The GRU: two gates, one state

The gated recurrent unit merges the cell and the hidden state and uses two gates (Cho et al. 2014, who called it a new hidden unit; the name is from Chung et al. 2014).

ht = (1 − zt) ⊙ ht−1 + zt ⊙ h̃t,   h̃t = tanh(W xt + U(rt ⊙ ht−1))
zt
update gate: one gate doing the forget and input jobs together (keep 1 − z, write z)
rt
reset gate: how much of the old state the candidate may look at

The same interpolating, additive trick with three weight blocks instead of four, so at equal width a GRU has exactly 3/4 of an LSTM's parameters: 3n(n + m + 1) against 4n(n + m + 1) for n units and m inputs derived (PyTorch keeps two bias vectors, so +2 instead of +1: the toy's recurrent layers, n = 8, m = 9, have 456 against 608). Quality is usually comparable, so the GRU's real advantage is a quarter fewer parameters at the same width.

GRU against LSTM: what the side-by-side studies found (under 1 min)

Chung et al. 2014 "could not make concrete conclusion on which of the two gating units was better"; Jozefowicz et al. 2015 found that "adding a bias of 1 to the LSTM's forget gate closes the gap between the LSTM and the GRU"; Greff et al. 2015's search over eight LSTM variants found none significantly better than the standard LSTM, with the forget gate and the output activation its most critical parts.

All four cells on any sequence. The widget beside this runs the LSTM, the GRU, the plain RNN and the plain RNN trained only on short sequences, gate by gate, on a key and length you choose; the bottom line shows what each would answer if "?" came now. Switch to the plain RNN: no gate, and the answer drifts to a guess. Predict whether the fourth model, a plain RNN trained only on short sequences, holds the key at length 120.

All four cells on any sequence: the gated cells hold the key, the plain RNN trained on the same lengths loses it

Trained weights (8 units; LSTM 644 parameters, GRU 492, plain RNN 188), PyTorch's arithmetic (checked to 7.5e-8, src/check_cells.py). A widget: pick a cell, a key and a length, then step through; it opens on the answer. Gate by gate at the key, the first distractor and "?". Gates white (0) to green (1); states blue (+) and orange (−). GRU z in Cho et al.'s convention (share written; PyTorch's z is 1 − z).

Measured: who learns to remember

The memory task as an experiment: each cell trained from scratch on sequences up to 10, 20, 50 or 100 symbols long, at three learning rates (0.003, 0.01, 0.03) and three seeds, 3,000 Adam steps of 64 sequences, gradient norm clipped at 1; then tested on 2,000 fresh sequences at each length from 5 to 200.

The plain RNN can hold a memory but cannot learn one from long sequences: trained on lengths up to 10 it solved the task in of 9 runs, and two of those hold the key correctly at every test length up to 200; trained on lengths up to 50 or 100 it solved it in none of 18, ending at 25% (chance) or 50% (one bit of the key kept). The gated cells learn it at every training length: . This is Bengio et al.'s 1994 diagnosis on a laptop: the trouble is the gradient that must find the key (the gradient section above), not the capacity to store it.

Toy scale, said plainly: 8 units, one task, nine runs per setting; it shows the mechanism, not how real language models compare. Scripts and every run's numbers: src/model/train_memory.py, runs/memory_results.json.

The plain RNN learns the task only from short sequences; the gated cells learn it at every training length

LSTMGRUplain RNN

Seq2seq, the bottleneck, and attention

Translation maps a sequence to a sequence of a different length. The encoder-decoder answer of 2014: an encoder RNN reads the source and its final state becomes a fixed-size vector c; a decoder RNN, started from c, writes the target one word at a time, feeding back the word it just wrote (Cho et al. 2014, who also gave c to the decoder at every step; Sutskever, Vinyals and Le 2014). Everything about the source passes through one vector of fixed size, however long the sentence: Sutskever et al.'s 4-layer, 1,000-cell LSTM (384M parameters) had to fit every sentence in 8,000 numbers (cell and hidden state of every layer). Cho et al. found their smaller encoder-decoder's performance "degrades rapidly as the length of the sentence and the number of unknown words increase", and Bahdanau et al. built attention on that observation.

Sutskever et al.'s result (under 1 min)

The model used 1,000-dimension embeddings. An ensemble of five scored 34.81 BLEU on WMT'14 English to French against 33.30 for the phrase-based baseline, "the first time that a pure neural translation system outperforms a phrase-based SMT baseline on a large scale MT task".

The toy: a translation that only memory can solve, copying a string of digits. Two models with the same GRU encoder and decoder (16-dimension embeddings, 32 units) and the same training, differing only in the context vector: the encoder's last state at every step (the fixed vector, as Cho et al.) or attention over all encoder states. Fig. 4 runs both on 12 digits of π with their trained weights (13,836 and 15,948 parameters, 8-bit; accuracy within a few points of full precision; greedy decoding; outputs equal to PyTorch's on 8 references).

Predict how many of the 12 digits the fixed vector gets right before step 3. Then type 25 digits, past the longest training string (20).

  1. The encoder reads the digits. A GRU reads one digit per step, and each state is a strip of 32 numbers (blue positive, orange negative): 32 numbers per digit, each strip built from the one before.
  2. The bottleneck. The fixed-vector model hands the decoder only the last strip, h12: 32 numbers must carry all 12 digits. The other strips fade: the decoder never sees them.
  3. The fixed vector writes the copy. Every output reads the same vector (the streams all leave the last strip). It gets 8 of 12 digits right; the first wrong one is output 7, where it wrote 5 for 2.
  4. Attention keeps every state. The attention model scores all 12 strips for output 7 and takes a softmax: each column is a weight, each stream as dense as its weight. 60% of the weight is on input 7 ("2"), so the right digit is read where it is.
  5. One row of weights per output. Every output does the same: the rows form the alignment matrix, and on a copy task it is a diagonal, each output reading its own input. The last row, the end marker, spreads its weight.
  6. Attention writes the copy. 12 of 12 digits right, and the end marker in place: nothing had to be squeezed through one vector.

A one-way encoder where Bahdanau et al. used a bidirectional one. Strips: 32 numbers per digit. Streams: the attention weights (density) or the single vector.

The formula. Bahdanau, Cho and Bengio (2014, published at ICLR 2015) kept every encoder state hj (from a bidirectional RNN, so each state sees the whole sentence) and, at every output step i, let the decoder choose which to read:

eij = v⊤ tanh(W si−1 + U hj),   αij = softmaxj(eij),   ci = Σj αij hj
si−1
the decoder's state before writing word i
eij
"alignment" score: how well source position j matches what the decoder needs now; a small network, so this is called additive attention
ci
a fresh context vector for every output word, fed to the decoder with the previous word

The decoder now reads as many numbers as the source has states, so nothing has to be squeezed. In the paper's Table 1, trained on sentences up to 50 words, the attention model (RNNsearch-50) scored 26.75 BLEU against 17.82 without it (RNNencdec-50), and even the attention model trained up to 30 words (21.50) beat the plain one trained up to 50; RNNsearch-50 "shows no performance deterioration even with sentences of length 50 or more" (Figure 2). The weights α are soft word alignments, readable as a matrix. A year later Luong, Pham and Manning scored with a plain dot product, ht⊤h̄s; the transformer's softmax(QK⊤/√dk)V is that dot score with a scale and separate projections for queries, keys and values.

What the toy reproduces: the fixed vector copies 6 digits almost always (96%) and 12 almost never (4%), so 32 numbers are not enough for a dozen digits; reversing the source alone lifts 10 digits from 24% to 68% (Sutskever et al.'s effect); attention copies every trained length perfectly. What it adds: attention fixed the bottleneck, not length generalisation. Past 20 digits it degrades too (93% whole strings at 22, 1% at 26), and Bahdanau et al. claim no deterioration only for RNNsearch-50, the model trained on the longer sentences. Toy scale: digits, not language, two seeds.

Correction: "seq2seq quality collapses on long inputs" was not universal
Sutskever et al.: "Surprisingly, the LSTM did not suffer on very long sentences", which they credit to feeding the source reversed, so the first source words sit next to the first target words ("many short term dependencies"; test BLEU 25.9 to 30.6, perplexity 5.8 to 4.7, §3.3). The collapse belongs to smaller, unreversed encoder-decoders (Cho et al.'s, Bahdanau et al.'s RNNencdec).

Reversing the source helps the fixed vector; attention copies every trained length and slips only past 20

fixed vectorfixed vector, source reversedattention

1,000 fresh strings per length, two seeds per model (the fainter line is the second), 6,000 Adam steps of 128 strings, lengths 3 to 20. The reversed model is the fixed-vector model fed the digits backwards, Sutskever et al.'s trick.

Convolutions for sequences: WaveNet

Recurrence is sequential: step t waits for step t − 1, in training as in use. WaveNet (van den Oord et al., DeepMind 2016) generated raw audio one sample at a time (16,000 samples a second in its speech experiments) with a stack of causal convolutions: each output sees only the past, and every position of a training clip is computed in parallel. A plain causal stack grows its receptive field by one sample per layer (the paper's Figure 2: 4 layers, filter 2, field 5). Dilating the filters (skipping 1, 2, 4, ... inputs) doubles the field every layer instead.

Predict the receptive field of 4 dilated layers before the last step. Fig. 5 traces one output back through the paper's Figure 3 stack (behind) and a plain stack of the same depth (in front).

  1. One output, two stacks. The newest output sample (right) of a plain causal stack (front) and a dilated one (back), 4 layers each. Filters have width 2 and are causal: a unit sees its own time step and one earlier one, never the future.
  2. Layer 4. The output reaches its own step and the one 8 steps earlier in the dilated stack, 1 step earlier in the plain one: 2 units at this depth in each.
  3. Layer 3. Dilation 4 against 1: the output now depends on 4 units in the dilated stack and 3 in the plain one.
  4. Layer 2. Dilation 2 against 1: 8 units against 4.
  5. Layer 1, the input. Dilation 1 in both: 16 input samples against 5, with no sample counted twice.
  6. Receptive field: 16 samples against 5. With dilations 1, 2, 4, 8 the field doubles every layer, 24 = 16 samples (1.00 ms at 16 kHz); without dilation each layer adds one, 4 + 1 = 5 (the paper's Figure 2; 0.31 ms).

Filter width 2, as in the paper. Receptive field = 1 + (filter width − 1) × sum of dilations derived.

The paper repeats blocks of dilations 1, 2, 4, ..., 512: "each 1, 2, 4, ..., 512 block has receptive field of size 1024" (§2.1). Stack them:

Training is parallel, but generation is still one sample at a time, because each sample feeds the next. WaveNet's lesson for text came quickly: ByteNet and ConvS2S translated with convolutions, whose path between two positions is O(logk n) layers for dilated and O(n/k) for contiguous filters, against self-attention's O(1) (the transformer paper, §4; Attention Is All You Need).

Correction: the WaveNet in production was not the 2016 model
The old page said WaveNet "powered Google TTS"; one sample at a time is far too slow to serve. What shipped in the Google Assistant in 2017 was Parallel WaveNet, a feed-forward student distilled from a trained WaveNet ("probability density distillation"), more than 20 times faster than real time, "deployed online by Google Assistant, including serving multiple English and Japanese voices".
WaveNet's other three pieces: μ-law output, the gated activation, the listening test (1 min)

Audio samples were μ-law companded and quantised to 256 values, so the output is a softmax over 256 classes instead of 65,536 (§2.2). Each layer uses the gated activation z = tanh(Wf ∗ x) ⊙ σ(Wg ∗ x), from gated PixelCNN, which "worked significantly better than the rectified linear activation" for audio (§2.3): the LSTM's gating idea without the recurrence, and an ancestor of the GLU family that ended in SwiGLU (Activation functions). Conditioned on linguistic features, its text-to-speech scored a mean opinion score of 4.21 in US English and 4.08 in Mandarin, against 3.86 and 3.79 for the best earlier systems and 4.55 and 4.21 for natural 16-bit speech (Table 1). The text-to-speech models had a 240 ms receptive field = 3,840 samples at 16 kHz.

Why the transformer replaced them

The hero figure's last lane. The transformer (2017) kept only attention (self-attention in the encoder and in the decoder, cross-attention between them) and so removed both costs at once: parallelism, since a recurrent layer needs n sequential steps in training and self-attention one, and path length, one hop between any two positions instead of n or logk n. The price is a layer cost of O(n2·d) against the RNN's O(n·d2), cheaper while the sequence is shorter than the width (Table 1 and §4). Its paper page animates one sentence through a recurrent layer (11 sequential steps) against one self-attention layer and runs a small trained transformer (Attention Is All You Need, Reading tab and "Run a Transformer").

Two things the RNN gave for free had to be added back: word order, since attention is blind to it (Positional Encodings), and a bound on memory per token, since attention keeps every past state (the KV cache: The transformer from zero, section 9). Everything after lives in Topic: llm-architecture.

Recurrence returns. Constant memory and compute per generated token, the RNN's advantage, came back with recurrences that train in parallel over the sequence: state-space models such as Mamba, linear attention, RWKV and the hybrids in production are on Beyond attention, whose section 3 also traces the LSTM's descendants (Mamba's Theorem 1, which makes the gated RNN a special case of selection; xLSTM; "Were RNNs all we needed?").

The inputs: one vector per word

Every model above reads words as vectors (the toys use one-hot symbols). Before 2013 a word was a one-hot vector, in which every pair of words is equally far apart. word2vec (Mikolov et al. 2013a) learned a dense vector per word by predicting neighbouring words with a deliberately shallow model, so it could be trained on billions of words; GloVe (Pennington, Socher and Manning 2014) fitted vectors whose dot products match the log co-occurrence counts. The two learn nearly the same thing (details closed below). Both were judged on the analogy test, "a is to b as c is to ?", answered by the word nearest to b − a + c; run here on the real 50-dimension GloVe vectors, all 19,544 questions, all 400,000 words:

What "king − man + woman = queen" leaves out
The nearest word to king − man + woman, on these vectors, is king (cosine 0.885); queen is second (0.852). Every published analogy score forbids the three question words as answers, and without that rule the answer is a question word in nearly half of all questions (Linzen 2016). The same rule manufactures bias examples: man : doctor :: woman : ? returns doctor, and "nurse" appears only once doctor is forbidden (Nissim, van Noord and van der Goot 2020). The bias in the vectors is real and measurable by other means (Bolukbasi et al. 2016); the analogy is a poor way to show it.

Static, then contextual. One vector per word cannot tell the river bank from the money bank, and a word missing from the vocabulary has no vector at all. ELMo (Peters et al. 2018) replaced the lookup with the states of a bidirectional LSTM language model, so each word's vector is "a function of the entire input sentence"; BERT did the same with a transformer, and subword pieces solved the vocabulary problem (Tokenizers). The embedding matrix at the bottom of every LLM is the direct descendant of word2vec's input vectors. Query the 2,000 real vectors in the Word vectors tab.

How word2vec and GloVe are trained, and why they agree (2 min)

Skip-gram predicts the words around a word from the word; CBOW (continuous bag of words) predicts a word from the average of its neighbours. The full softmax over a vocabulary of millions is too expensive, so the first word2vec paper used a hierarchical softmax (a Huffman tree, about log2 V outputs per word) and the second (Mikolov et al. 2013b) replaced it with negative sampling: push the true (word, context) pair together and k random words apart.

log σ(v′c · vw) + Σi=1..k Eni ~ Pn log σ(−v′ni · vw)
vw, v′c
input vector of the word, output vector of a context word
k
negatives per pair: 5 to 20 for small corpora, 2 to 5 for large ones
Pn
the unigram distribution raised to the 3/4 power, which beat both the plain unigram and the uniform distribution (§2.2); frequent words are also subsampled (§2.3)

GloVe counts first, then fits: build the global word-by-word co-occurrence matrix X (pairs d words apart counted as 1/d), then fit vectors to the log counts by weighted least squares:

J = Σi,j f(Xij) (wi · w̃j + bi + b̃j − log Xij)2,   f(x) = min(1, (x/xmax)α)
f
down-weights rare pairs and caps frequent ones; the paper uses xmax = 100, α = 3/4, AdaGrad, and the sum W + W̃ as the final vectors

Why they agree. Skip-gram with negative sampling implicitly factorises the word-context pointwise mutual information matrix shifted by log k (Levy and Goldberg 2014), so "predictive" (word2vec) against "count-based" (GloVe) is a difference of method more than of what is learned. On the analogy test, 300-dimension GloVe trained on 6 billion tokens scored 71.7%, against 69.1% for skip-gram and 65.7% for CBOW trained by the GloVe authors on the same corpus (GloVe paper, Table 2). On these 50-dimension GloVe vectors man : programmer :: woman : ? gives "prodigy", not Bolukbasi's "homemaker", which came from word2vec's Google News vectors and a constrained search.

Negative sampling's descendant is InfoNCE, with the batch as the set of negatives (Contrastive and self-supervised learning).

Depth

Luong's variants: three scores, global against local (under 1 min)

Luong, Pham and Manning 2015 scored with the decoder's current state ht rather than the previous one, and compared three score functions, dot ht⊤h̄s, general ht⊤Wah̄s and concat (Bahdanau's form), plus "global" attention over all source words against "local" attention over a window. The transformer paper still describes its dot-product attention against "additive attention", Bahdanau's form.

Common misreadings, each answered on this page (1 min)
  • "The LSTM solved vanishing gradients." It gave the gradient one protected path, scaled only by the forget gate; untrained, the toy LSTM's gradient 50 steps back is still about 10−6 of its size at the answer. Training opens the path; it does not start open.
  • "A plain RNN cannot remember for long." It can represent a long memory (the short-trained plain RNN holds the key for 200 steps); it cannot learn that memory from long sequences. Representation and learnability are different questions.
  • "The LSTM had forget, input and output gates in 1997." The forget gate came in 2000.
  • "king − man + woman = queen." Only once king, man and woman are forbidden as answers; without the rule GloVe 50d scores 30.4% on the analogy test instead of 46.2%.
  • "Seq2seq always collapses on long sentences." Small, unreversed encoder-decoders did; Sutskever et al.'s reversed deep LSTM did not.
  • "Attention was invented by the transformer." It was added to RNN translation in 2014 and refined in 2015; the transformer's contribution was to remove everything else.
  • "GRUs are better than LSTMs" (or the reverse). Side-by-side studies did not settle it; the GRU's real advantage is a quarter fewer parameters at the same width.
  • "WaveNet is a recurrent network." It has no recurrence; only its generation is sequential.
What reproduces what (under 1 min)

The analogy scores (46.2%, 30.4%, 49.7%) are measured here on the released GloVe 6B 50d vectors, independently, with no published 50-dimension figure to compare against; the WaveNet receptive fields (5 for the paper's Figure 2, 1,024 per block of dilations 1 to 512) reproduce the paper independently from the formula; the toy models reproduce Bahdanau et al.'s and Sutskever et al.'s qualitative findings, not their numbers. Every number is recomputed by src/recompute.py.