8  Sequence Models

Many learning problems involve ordered observations: words in a sentence, notes in a melody, or measurements in a time series. Earlier chapters mapped a fixed-length input to an output and usually treated examples as independent. Sequence models must instead represent dependence across positions and inputs whose lengths may vary.

Earlier objectives usually treated examples as exchangeable, fixed-shape records. Sequence tasks retain the order within each example: conditioning on earlier words, notes, or measurements can change the distribution of later ones. Sequence lengths may also vary, even though implementations often pad or batch them into equal-sized arrays.

The chapter develops two main ideas. The first is autoregressive factorization. Rather than model the probability of a whole sequence at once, we factor it into a product of one-step-ahead predictions: the probability of each element given the elements that precede it. This turns an unwieldy generative problem into an ordinary supervised one, in which the input is a prefix and the label is the next element. Every position in a sequence thus becomes another training signal. Generation repeats this prediction while feeding each generated output back as the next input.

The second idea is the hidden state. A prefix grows with time, but a recurrent model carries a fixed-size state \(h_t=f(x_t,h_{t-1})\). The state is a function of the prefix, and training determines which predictive information it retains. Its fixed size makes incremental inference inexpensive but may discard information from the distant past.

Language modeling provides the running application. We turn text into tokens, factor sequence probabilities into next-token conditionals, and replace a fixed context window with a recurrent state. The implementation then exposes two remaining problems: gradients must propagate through repeated state updates, and a probability distribution still needs a decoding rule to produce text.

Recurrent networks powered the deep-learning breakthroughs of the 2010s in speech recognition and machine translation, and for a while they were the default model for anything sequential. Transformers later displaced them at scale, and much of today’s attention goes there. Yet recurrence has returned in modern guise, which we take up in Chapter 12, precisely because a bounded-memory state makes inference cheap when the alternative grows with the length of the sequence. Whichever architecture wins a given task, the later material on large language models uses the same concepts introduced here: autoregressive factorization, perplexity, backpropagation through time, and decoding. They are worth learning once, and learning carefully.

Resources and Further Reading

The references below retrace this chapter’s arc — text into tokens, a counting baseline, the recurrence that replaces it, the gradients that make training hard, and the decoding that turns predictions back into text. Because tokenization, perplexity, and sampling are practiced today essentially as these sources describe, the list runs unusually far back and unusually far forward: Shannon’s 1951 guessing game and a 2025 sampling rule sit here comfortably side by side. All are freely accessible online except where noted.

Books

Courses and video lectures

Foundational papers

Tutorials, notes, and interactive