Sequence Models

Dive into Deep Learning · §8

Sequence Models and Recurrent Neural Networks
autoregression · BPE subword tokenization · language models · perplexity & bits per byte · RNNs · BPTT · decoding

Why Fixed-Shape Feedforward Models Fail on Sequences

Motivation & Core Problem

  • Unbounded causal context: Completing the 20-word opening sentence of H. G. Wells’ The Time Machine requires integrating nineteen preceding words in exact temporal order.
  • Combinatorial context explosion: Tracking 20 tokens over a 1,024-token vocabulary yields 1024^{20} \approx 10^{60} contexts; even for 3-word triples (n = 3), 87\% of held-out triples never appear in training.
  • Bounded-state compression: A sequence model must compress an arbitrarily long prefix x_{<t} = (x_1, \ldots, x_{t-1}) into a fixed-size representation with constant parameter count and O(1) compute per step.

Chapter 8 Roadmap: From Time Series to Decoding

Architectural Progression

  • Data & representation (§§8.1–8.2): Formulate chain-rule autoregression (m-step windows vs. latent state \mathbf{h}_t) and convert raw UTF-8 strings into subword IDs via pre-tokenized Byte Pair Encoding (BPE).
  • Probabilistic scoring & RNNs (§§8.3–8.4): Benchmark Kneser–Ney n-grams in tokenizer-invariant bits per byte (\textrm{bpb}), then replace count tables with a shared-weight \tanh recurrent cell stabilized by global gradient clipping.
  • Credit assignment & decoding (§§8.5–8.6): Derive Backpropagation Through Time (BPTT) to explain why gradients vanish or explode, and compare beam search against truncated samplers (\text{top-}k, \text{top-}p, \text{min-}p).

End-to-End Quantitative Benchmarks on Wells’ Novella

Empirical Anchors

  • Rollout drift & subword compression (§§8.1–8.2): Linear AR(4) achieves 0.05 1-step MSE on a noisy sine wave but degrades 5\times to 0.25 on 64-step rollout; BPE-1024 compresses The Time Machine to 2.78 bytes/token with zero OOVs.
  • Count vs. neural language models (§§8.3–8.4): Kneser–Ney cuts word trigram PPL from 2{,}481 \to 300 (1.90 bpb on BPE-1024); clipped RNNLMScratch (512-byte state, 89\% params in vocabulary layers) reaches 2.12 bpb.
  • Gradient horizon & generation (§§8.5–8.6): Despite \rho(\mathbf{W}_{\textrm{hh}}) \approx 1.80, \tanh' contraction (\approx 0.27) decays gradients below 1\% within 8 steps; truncated samplers restore distinct-3 \approx 0.99.

Concept Check: Designing an End-to-End Language Model

Check Your Understanding

Before diving into §8.1, test how the components of the sequence modeling pipeline interact:

  1. Tokenizer vs. Perplexity: Model A uses a 27-character vocabulary and achieves test perplexity \textrm{PPL} = 6.5. Model B uses a 1,024-token BPE vocabulary (2.78 bytes/token) and achieves \textrm{PPL} = 60.0. Which model compresses the test text better in bits per byte?
  2. State Memory vs. Sequence Length: How many bytes of hidden-state memory does a single-layer float32 RNN with h = 128 units require to process a 32-token window versus a 1,000,000-token book?
  3. Training vs. Inference Mismatch: Why can an autoregressive model achieve low 1-step cross-entropy loss during teacher-forced training yet degenerate into repetitive or off-topic text during free generation?

Answers:

  1. Model B wins: Character Model A (1 byte/char) uses \log_2(6.5) / 1.0 \approx 2.70 bits/byte, whereas BPE Model B uses \log_2(60.0) / 2.78 \approx 5.91 / 2.78 \approx 2.13 bits/byte. Raw perplexity is incomparable across different token granularities.
  2. Identical (512 bytes): 128 \times 4\text{ bytes} = 512\text{ bytes} per sequence regardless of length T, because \mathbf{h}_t overwrites \mathbf{h}_{t-1} in place at every step.
  3. Exposure bias & rollout compounding: Teacher forcing always feeds ground-truth prefixes x_{<t}, whereas generation feeds back self-sampled tokens \hat{x}_t; a single low-probability tail token or deterministic cycle pushes the state \mathbf{h}_t out of the training distribution.

Key Takeaways: The Sequence Modeling Pipeline

Chapter Synthesis

  • Universal subwords & invariant scoring (§§8.2–8.3): Pre-tokenized byte-level BPE eliminates <unk> tokens while reserving special IDs above |\mathcal{V}_{\textrm{BPE}}|; bits per byte (\textrm{bpb}) compares any tokenization on a common lossless-compression scale.
  • Constant-memory recurrence & BPTT (§§8.4–8.5): Shared weights update a fixed-size state \mathbf{H}_t \in \mathbb{R}^{b \times h} in O(1) per step; truncated BPTT bounds memory to O(T_0 bh) and global norm clipping prevents exploding gradient steps.
  • Controlled autoregressive decoding (§8.6): Length-normalized beam search serves constrained tasks, while temperature + \text{top-}p / \text{min-}p tail truncation eliminates repetition loops in open-ended generation.