Matrices and tensors
A matrix is a grid of numbers that transforms vectors. A tensor is the same idea with any number of dimensions. Models are mostly matrix multiplication.
Matrix times vector: edit any number
row 1: 1×3 + 0×4 + 2×5 = 13 | row 2: 0×3 + 1×4 + 1×5 = 9
Each output is a dot product of one row with the input. A neural network layer is this, plus a bend.
Words on this page (10)
- matrix
- : A grid of numbers. Multiplying a vector by a matrix transforms it. Model layers are mostly matrices.
- vector
- : An ordered list of numbers. Embeddings and model activations are vectors.
- tensor
- : A multi-dimensional array of numbers. The basic data type of deep learning libraries.
- dot product
- : Multiply matching entries of two vectors and add them up. Large when they point the same way.
- batch
- : A group of examples processed together in one step.
- GPU
- : Graphics processing unit. A chip with thousands of cores, ideal for the matrix math in AI.
- activation function
- : A simple curve such as ReLU applied after a neuron's sum. It lets stacked layers represent complex functions.
- parameter
- : One learned number inside a model. Modern models have billions of them. Training adjusts them.
- weights
- : The learned numbers that define a trained model. A model whose weights are published is called open-weight.
- loss
- : A number measuring how wrong the model's output is. Training tries to reduce it.
The idea
A with 2 rows and 3 columns turns a 3-number into a 2-number vector. Each output entry is a of one row with the input. Multiplying a whole of vectors by one matrix does many dot products at once, which is what are built for. A is an array with any number of axes: a vector has 1, a matrix has 2, a batch of images has 4 (batch, channels, height, width).
Example
Let M = [[1, 0, 2], [0, 1, 1]] and x = [3, 4, 5]. Row 1 · x = 3 + 0 + 10 = 13. Row 2 · x = 0 + 4 + 5 = 9. So M x = [13, 9]. Shapes must match: (2 by 3) times (3) gives (2). When shapes differ in a compatible way, libraries use broadcasting, which stretches the smaller array to fit: adding a bias vector of length 2 to each row of a batch happens without copying it.
Why it matters
A layer of a network is one matrix multiply plus an . A language model with billions of is mostly large matrices. Getting shapes right is the most common source of bugs when writing model code.
How matrix times matrix works
Multiplying two matrices is many matrix-times-vector steps, one for each column of the second matrix. Take A = [[1, 0, 2], [0, 1, 1]] (2 by 3) and B = [[1, 1], [2, 0], [0, 3]] (3 by 2). The result is 2 by 2, and entry (row i, column j) is row i of A dotted with column j of B.
- Entry (1,1): 1×1 + 0×2 + 2×0 = 1.
- Entry (1,2): 1×1 + 0×0 + 2×3 = 7.
- Entry (2,1): 0×1 + 1×2 + 1×0 = 2.
- Entry (2,2): 0×1 + 1×0 + 1×3 = 3. So A B = [[1, 7], [2, 3]]. Order matters: B A is 3 by 3, a different shape and different numbers. The inner sizes (3 and 3) must match, and the outer sizes (2 and 2) become the result.
Counting the work
Invented sizes for illustration: a layer that turns 1,000 input numbers into 500 output numbers has a 500 by 1,000 matrix. That is 500 × 1,000 = 500,000 , plus 500 biases (one per output), so 500,500 learned numbers. One input needs 500,000 multiplications. A batch of 32 inputs needs 32 × 500,000 = 16,000,000. That is why speed and memory are measured in matrix sizes.
What goes wrong: silent broadcasting
Most shape bugs raise an error, which is the good case. Broadcasting can hide a bug. Add a column of shape (3, 1), [1, 2, 3], to a row of shape (3), [10, 20, 30]. You expected 3 numbers. The library instead stretches both and returns a 3 by 3 grid whose first row is [11, 21, 31] and last row is [13, 23, 33]. Later code may still run and give a wrong . The symptom is a result with more entries than you expected, so print shapes after each step.
Check it by hand
Let M = [[2, 0], [1, 1], [0, 3]] and x = [4, 5]. M is 3 by 2, so the answer has 3 numbers. Row 1 gives 8, row 2 gives 4 + 5 = 9, and row 3 gives 15. You should get [8, 9, 15]. If you try x = [4, 5, 6], the inner sizes (2 and 3) clash, which is exactly the error you would see in code.
Common mistakes
Shape errors. If the matrix is 2 by 3, the vector must have 3 numbers, and the result has 2. Most beginner bugs in model code are shapes that do not line up. Print the shape of everything while you debug.
A photo editor applies a filter to a million pixels. Each new pixel is a weighted mix of the old ones. Written as math, that whole filter is one matrix multiplication. Language models are the same kind of step, repeated billions of times, which is why a graphics card that is good at pictures turned out to be good at AI.
Check yourself
M is a 2 by 3 matrix and x has 3 numbers. How many numbers does M x have?
Not quite. Each of the 2 rows gives one output number.
What does broadcasting do?
Not quite. It lets you add a bias vector to a whole batch without copying it.
You multiply a 4 by 3 matrix by a 2-number vector and get an error. What is the most likely cause?
Not quite. A matrix times a vector needs the matrix's column count to equal the vector's length.
You got 0 of 3 right.
Go deeper
- Video
3Blue1Brown · Why the rows-by-columns rule is what it is.
- CoursePyTorch tensors tutorialCourse
PyTorch · Create, reshape and multiply tensors.
- ArticleBroadcasting semanticsArticle
NumPy · The precise rules, with pictures.
Spotted a mistake or something unclear? Report it on GitHub.