Move with the arrow keys, a presentation clicker, or the buttons at the bottom left; the gear at the bottom right opens the slide settings.
Today we cross that wall twice: with a network that reads token by token and carries a memory, and with a network that reads the whole window at once.
A neural network computes next token probabilities with arithmetic: multiplications and additions arranged in layers, steered by millions of adjustable numbers, called weights.
Training shows the network a context from the corpus, compares its probabilities with the token that actually came next, and nudges every weight so that the true token scores higher. After millions of such nudges, made by a procedure called gradient descent, the corpus becomes probable under the network.
Because the weights are shared across all contexts, similar contexts receive similar predictions: the network can score contexts it has never seen.
A compressed summary of everything so far: who the characters are, what just happened, how the current sentence is shaped. Each new word updates the summary; the summary, and only the summary, carries the past forward.
A recurrent neural network, RNN for short, is built on exactly this plan.
The RNN keeps a memory, called the hidden state: a list of numbers of fixed length.
The same function f, with the same weights, runs at every step. A second function maps the memory to next token probabilities at any point.
Each step feeds on the result of the step before it. This looping pattern is called recurrence, a form of recursion, and it gives the network its name.
Unrolling the loop draws one copy of f for every token; all copies share the same weights. The memory persists and is updated from copy to copy.
The hidden state has a fixed length, so a long document must fit into it: early details fade as new tokens keep arriving.
Training operates the same way. Because the correction signal must travel backward through every step, it weakens along the way; this behavior is known as the vanishing gradient problem.
Gated designs, among them the LSTM from 1997, guard the memory with learned gates and hold information across many more steps.
Each RNN step needs the memory produced by the step before it, so a text of 1,000 tokens takes 1,000 steps in order. The order cannot be broken; every step waits.
Modern AI hardware, the GPU, performs enormous batches of arithmetic at the same time. Because a computation that must proceed step by step leaves most of that capacity idle, training an RNN on a very large corpus becomes slow.
In 2017, a research team at Google published Attention Is All You Need, the paper that introduced the transformer.
The transformer, a neural network for sequences, has two defining properties: (i) it processes every token in its window at the same time; (ii) it uses no recurrence: no step waits for a previous step.
Every position can look directly at every other position in the window. The component that chooses where to look is called attention; today we only need its name, and a later lesson opens it up.
The window has a fixed maximum size, called the context window; Gemini 1.5, a 2024 model from Google, fits one million tokens in it.
Part 1 ended with a verb that depends on a word seven tokens back. A transformer position reads all earlier positions directly, near or far; the arcs below sketch one such set of looks.
A large language model, LLM for short, is a transformer with a very large number of weights, trained on next token prediction over a very large collection of text.
To write, the model predicts probabilities for the next token, samples one, and, after appending it to the window, predicts again.
This loop matches the Markov chain walk from Part 1: the game never changed. The probabilities now come from a deep network that reads a very long window.
1. A model predicts the next token by reading only the current token. Which family does it belong to?
A Markov chain; the Markov property says the next state depends only on the current state.
2. What does an RNN carry from one step to the next, and what limits it?
The hidden state, a list of numbers with a fixed length; everything read so far must fit inside it.
3. In a transformer window of 1,000 tokens, how many sequential steps does information from token 1 cross to reach position 1,000?
None along the sequence: any position reads any other position directly through attention.
Next lesson: the transformer's two ends: how tokens become lists of numbers, and how a small feedforward machine reads the answer out.
Slides: Mehmet Kerem Turkcan, Center for Smart Streetscapes, Columbia University; keremturkcan.com; mkt2126@columbia.edu