Chapter 05 · Linear algebra
Words turned into arrows
A computer has no idea what a cat is, but it can multiply matrices at enormous speed. Linear algebra lets us represent words, images and concepts as vectors, and measure how alike they are with a simple dot product.
In this chapter
Open up a language model and what you find are numbers arranged in matrices: hundreds of billions of them. Answering a question consists almost entirely of multiplying vectors by matrices. That is why GPUs, chips designed to multiply matrices while drawing video games, became the engine of AI.
Linear algebra is the language in which models represent the world. Hermann Grassmann formulated the idea of a space of arbitrary dimension in 1844, and Arthur Cayley defined the matrix product in 1858. Neither could have imagined they were writing the language of AI.
Vectors and the dot product
A vector is a list of numbers that can be seen as a point or as an arrow from the origin. The key operation is the dot product:
It measures how much two vectors “point the same way”. Dividing by their lengths gives the cosine similarity, , which is 1 for parallel vectors, 0 for perpendicular ones and −1 for opposite ones. That it never leaves this range is guaranteed by a classic:
For all , , with equality if and only if they are proportional.
Proof
If it is trivial. Otherwise, for every , . A quadratic in that is never negative has discriminant : .
This simple operation, multiply and add, is what the attention mechanism uses to decide which words of a sentence should look at which.
Embeddings: meaning as geometry
How do you turn a word into a vector? The answer comes from linguistics. In 1954 Zellig Harris formulated the distributional hypothesis: words that appear in similar contexts have similar meanings. John R. Firth summed it up in 1957: “You shall know a word by the company it keeps.”
An embedding assigns each word a vector so that words with similar contexts end up close together. In 2003 Yoshua Bengio and his colleagues trained such vectors for the first time, together with a neural network that predicted the next word. In 2013 Tomas Mikolov and his team at Google published word2vec, which trained embeddings on billions of words in a few hours, and discovered something unexpected: relationships between concepts turn into directions.
Matrices: functions that transform space
An matrix defines a linear transformation from to . It can be understood at a glance by looking at its columns: column is where the unit vector goes. Since the transformation respects sums and scaling, knowing where the basis goes is enough to know everything:
Multiplying matrices is composing transformations: . This innocent-looking property has a decisive consequence for neural networks:
A composition of linear transformations is linear: . Stacking a hundred linear layers gives no more expressive power than a single one.
That is why each layer of a neural network applies, after the matrix, a non-linear function :
Stretch, bend, stretch, bend… With that recipe any continuous function can be approximated, as we will see with the universal approximation theorem.
The surprising roominess of high dimensions
The embeddings of a modern LLM have between roughly a thousand and twenty thousand dimensions. Is that a lot or a little to represent every concept a model handles? Three-dimensional intuition is misleading: in high dimension there is a vast amount of room.
For every and every set of points in there is a linear map with such that, for every pair of points,
Moreover, a random projection works with high probability.
The dimension needed grows only with the logarithm of the number of points. The flip side of the same geometry: has room for exponentially many directions that are almost perpendicular to each other. The superposition hypothesis, studied by interpretability teams, holds that models exploit this to encode many more concepts than they have dimensions, each in a direction almost orthogonal to the rest.
Low rank: the essence of a matrix
Every real matrix, however large, breaks down into three simple transformations: a rotation, a stretch along the axes and another rotation.
Every can be written as , with orthogonal and diagonal, whose entries are the singular values. By the Eckart–Young theorem (1936), keeping the largest gives the best rank- approximation of .
Many real matrices have almost all their “information” concentrated in a few singular values. This idea underpins current techniques such as LoRA (2021), which adapts a huge LLM to a new task by training only a low-rank correction, , with tiny and , instead of retraining the billions of parameters in .
We now know where knowledge is stored: in the numbers of the matrices. What is missing is the procedure for choosing those numbers, and for that we need to know how the error changes when each of them moves. That is calculus.
References
- A. Cayley (1858). “A Memoir on the Theory of Matrices”. Philosophical Transactions of the Royal Society, 148.
- Z. Harris (1954). “Distributional Structure”. Word, 10(2–3).
- W. B. Johnson and J. Lindenstrauss (1984). “Extensions of Lipschitz mappings into a Hilbert space”. Contemporary Mathematics, 26.
- Y. Bengio, R. Ducharme, P. Vincent and C. Jauvin (2003). “A Neural Probabilistic Language Model”. JMLR, 3.
- T. Mikolov, K. Chen, G. Corrado and J. Dean (2013). “Efficient Estimation of Word Representations in Vector Space”. arXiv:1301.3781.
- E. J. Hu et al. (2021). “LoRA: Low-Rank Adaptation of Large Language Models”. arXiv:2106.09685.
- N. Elhage et al. (2022). “Toy Models of Superposition”. Transformer Circuits Thread.