- Key idea
- A matrix is a grid of numbers with shape rows × columns; entry A[i, j]; rows and columns are vectors; the transpose swaps rows and columns
- Real world
- A spreadsheet of daily weather: one row per day, one column per measurement (temperature, rain, wind). Each row is a day’s vector; each column is one measurement across the week.
- In an LLM
- A sentence of 5 tokens, each a vector of 768 numbers, is a 5 × 768 matrix. GPT-2’s whole vocabulary is a 50,257 × 768 matrix, the embedding table: one row per token.
A rectangle of numbers
A is numbers arranged in rows and columns. Its shape is written rows × columns: a matrix with 5 rows and 4 columns is “5 by 4”, , and holds 20 numbers. The entry in row and column is written or .
This is the toy input from the Transformers course: one row per token of “The cat sat on the”, one column per meaning (thing, action, small word, first token). says that token 2, “cat”, is fully a thing.
Three ways to read it
- A table. Look up one number by its row and column.
- A stack of rows. Each row is a vector. Here each row is one token’s embedding, so the matrix is the whole sentence. This is how a transformer holds its input: one row per token.
- A row of columns. Each column is also a vector: one meaning, measured for every token.
None of these is the “real” one. Matrix multiplication, in the next lesson, uses the rows of the left matrix and the columns of the right one, so you need both.
The transpose
The of , written , flips it over its main diagonal (the cells from top left downwards). Row becomes column , and the shape turns round: a matrix becomes . In symbols, .
Switch views, then press “transpose” and watch every cell move across the dashed diagonal:
A week of weather in a spreadsheet has 7 rows (days) and 3 columns (temperature, rain, wind): a matrix. Read along a row and you get one day’s vector, . Read down a column and you get the week’s temperatures. Transpose it and the days run across the top instead, a matrix with the same 21 numbers. Spreadsheets call this “paste special, transpose”.
An embedding table is a matrix
A model’s vocabulary is stored as a matrix with one row per token: the embedding table. Looking a token up means taking its row. You can write that lookup as a multiplication: a row vector of zeros with a single 1 in the token’s position (a one-hot vector) times gives exactly that row.
GPT-2 small’s embedding table has shape : 38.6 million numbers, almost a third of the model’s 124 million parameters. Reading “The cat sat on the” takes rows 464, 3797, 3332, 319 and 262 and stacks them into a matrix. That matrix is what the first transformer block receives. Because is so tall, real code reads rows by index (E[ids]) instead of multiplying by one-hot vectors, but the result is identical.
Shapes everywhere
From here on, almost every object in a transformer is a matrix, and the most common bug is a shape that doesn’t fit. The habit to build now: whenever you see a matrix, say its shape out loud and what its rows and columns mean.
| Matrix | Shape in GPT-2 small | Rows are | Columns are |
|---|---|---|---|
| Embedding table | 50,257 × 768 | tokens in the vocabulary | embedding dimensions |
| Input for tokens | × 768 | positions in the text | embedding dimensions |
| Query weights (one head) | 768 × 64 | input dimensions | query dimensions |
| Attention weights | × | the token looking | the token looked at |
Notation
An matrix of real numbers belongs to . Row is written and column is , the same slicing that numpy uses. Maths counts rows and columns from 1; Python counts from 0, so above is X[1, 0] in code.
Useful facts about the transpose: , , and (the order reverses). A matrix with is symmetric; the cosine-similarity table in the dot product lesson is one.
In code
import numpy as np
X = np.array([[0.1, 0.0, 1.0, 1.0],
[1.0, 0.1, 0.0, 0.0],
[0.2, 1.0, 0.0, 0.0],
[0.0, 0.3, 0.8, 0.0],
[0.1, 0.0, 1.0, 0.0]])
X.shape # (5, 4)
X[1] # row for "cat": array([1. , 0.1, 0. , 0. ])
X[:, 0] # column "thing": array([0.1, 1. , 0.2, 0. , 0.1])
X.T.shape # (4, 5)
import torch
E = torch.nn.Embedding(50257, 768) # the table, E.weight has shape (50257, 768)
E(torch.tensor([464, 3797, 3332, 319, 262])).shape # torch.Size([5, 768])Further reading
- 3Blue1Brown, Essence of linear algebra, chapter 3.
- The embedding table as the first layer of a language model: Bengio et al., 2003.