Machine learning with football · Chapter 10 · Large language models

Transformers

Self-attention lets every word look at every other. That, at enormous scale, is a large language model.

Estimated time: 15 minutes · page 2 of 4

Every word looks at every other

Today's large language models are transformers. A full transformer has an encoder, which turns the input text into an internal representation, and a decoder, which turns that representation into output text, as in translating a match report from English to French. Encoder-only models are used for embeddings and classification. Decoder-only models, such as most chatbots, generate text.

Self-attention

The key idea is self-attention. For each token, the model asks how much every other token in the input matters for understanding it. In "The striker didn't celebrate the goal because it was offside", the word "it" should attend mostly to "goal". Change the end to "because he was injured" and "he" should attend to "striker". An encoder looks both ways, which is bidirectional. A decoder writing text can only look back, which is unidirectional.

The board tries this with plain counting. It hides one word in a real headline and guesses it twice, using word pairs from the story summaries: once from the word before, once from the words before and after. Counting makes the second guess worse, not better. A candidate only scores if both pairs appeared in the summaries, and in a few thousand words most never did. That's the sparsity problem again. A transformer doesn't look up exact pairs. It learns embeddings, as in chapter 9, so "Chelsea" sits near "Arsenal" and what it learned about one helps with the other. That's how more context makes its guesses better instead of rarer.

Each layer has several attention heads, each free to learn a different kind of link, and models stack many layers. Early ones pick up grammar, later ones meaning and theme. The cost grows with the square of the input length: every token attends to every other, so N tokens make N² pairs per head per layer. The board does the sums.

Training and generating

Training starts with a huge, filtered body of text and hidden words: "The ___ scored twice before ___ was substituted." The model learns to fill the gaps, billions of times, with backpropagation from chapter 8. Models with more parameters, now hundreds of billions, consistently predict better. To answer a question, an LLM treats it as text to continue and autocompletes it, token by token.

The problems are real. Training takes months of computing and huge amounts of electricity. LLMs hallucinate, producing fluent text that's wrong, and they inherit the biases of their training text, chapter 13's subject.

Check yourself

In "The striker didn't celebrate the goal because it was offside", which word should get the most attention when reading "it"?
Which kind of self-attention does a decoder use when it writes text?
A transformer is trained on a billion documents, thousands of them about penalties. Which are probably true? Pick all that apply.

Leagues