Lesson 3 · Linear algebra
Matrices: tables of numbers
The problem
In the vectors chapter, one dot product gave one delivery's time. The weights were : 4 minutes per km and 6 minutes per traffic level. Now three deliveries are waiting:
| Delivery | Distance (km) | Traffic level | Time: |
|---|---|---|---|
| A | 2 | 0 | |
| B | 5 | 1 | |
| C | 3 | 2 |
That's three dot products with the same weights. A model in training might compute outputs for thousands of examples at once. We want one operation that does all of them.
A matrix is a table of numbers
Stack the three deliveries as rows:
A matrix is a table of numbers in rows and columns, named with a capital letter. Here each row is one delivery's feature vector, and each column is one feature for every delivery. One example per row is the usual layout in ML.
The shape of a matrix is (rows × columns), so is . An entry has two subscripts, in the same order: is row 2, column 1, which is delivery B's distance, 5. Tap the entries below.
X₂₁ = 5
Row 2 is delivery B: (5, 1)
Column 1 is distance (km) for every delivery: (2, 5, 3)
Tap any entry.
Predict first
What is X₃₂?
A matrix times a vector
The matrix-vector product takes the dot product of each row of with . You get one number per row. Press play below to compute it one row at a time. The sliders under the figure change .
Press play. Each row of X meets w in a dot product.
These are the three delivery times from the table. For the product to work, each row of must have as many entries as . A matrix times a vector with 2 entries gives a vector with 3 entries.
In sum notation, entry of the answer is:
Read it as: for row , multiply each entry by the matching weight , then add. That's the dot product of row with .
Predict first
A fourth delivery D comes in: 1 km, traffic level 3. You add it as a new row of X. What does X w give now?
Two ways to see the same product
Switch the figure to Column by column. There, is built from the columns of :
Every distance is scaled by 4 minutes per km, and every traffic level by 6. So is a weighted sum of the columns of . Both views do the same multiplications in a different order. The column view becomes the main picture when we treat a matrix as a transformation in the next chapter.
A matrix times a matrix
The shop also has a bike. Bikes are slower per km, but traffic barely slows them. Say the bike's weights are 5 minutes per km and 2 per traffic level. Put the two weight vectors side by side as columns:
Column 1 is the scooter's weights, . Column 2 is the bike's, . Now one product gives every delivery's time with every vehicle.
Row B of X · bike column of W
5 × 5 + 1 × 2 = 27 min
The whole bike column of the answer is X times the bike column of W: (10, 27, 19). Tap any entry of the answer.
Matrix multiplication takes the dot product of every row of with every column of . The answer has one row per delivery and one column per vehicle:
Worked example
Delivery C by bike: entry (3, 2) of XW
Take row 3 of , delivery C: .
In symbols, entry is row of dotted with column of :
Here runs along the shared inner size: across row of and down column of at the same time. Each column of the answer is times one column of . The figure highlights this when you tap an entry. So matrix multiplication is several matrix-vector products side by side.
The transpose
The transpose of , written , swaps rows and columns. Row 1 of becomes column 1 of , and so on.
X has shape (3 × 2)
row 1 = delivery A · row 2 = delivery B · row 3 = delivery C
Entry of is entry of , and the shape flips from to . The transpose shows up mostly to make shapes fit.
Shapes must fit
Each entry of is a row of dotted with a column of , so those must have the same length. In shapes:
The two inner sizes must match. The answer takes the two outer sizes. For our deliveries: .
Predict first
Can you multiply a (4 × 2) matrix by another (4 × 2) matrix?
(4 × 2) · (4 × 2)
The inner sizes don't match: A's rows have 2 entries, but B's columns have 4. No dot product fits.
The order matters too. and are usually different, even when both exist:
With ordinary numbers, . With matrices, swapping the order changes which rows meet which columns.
Putting it together
Each vehicle also needs a few minutes before it leaves: 5 for the scooter and 7 for the bike. Put those in , one entry per column of , and add to every row:
Row 2 says delivery B takes 31 minutes by scooter and 34 by bike, start-up included.
A neural network layer computes this same expression, . A group of examples processed together is called a batch, and each row of is one example in it. Whether the batch holds 3 deliveries or 3,000, the formula is the same: only the number of rows changes.
DigressionWhy GPUs are fast at matrix multiplicationEvery entry of the answer can be worked out at the same time. · 2 min
Multiplying an matrix by a one takes dot products, each with multiplications. For two matrices, that's a billion multiply-and-adds.
No dot product depends on another. Entry doesn't need entry , so all of them can be computed at once.
A GPU (graphics processing unit) is built for this. It has thousands of small cores that do the same arithmetic on different numbers at the same time. That's why deep learning runs on GPUs. It's also why ML code is written as a few big matrix products, not as loops over examples.
Common misconceptions
" needs to be square." It doesn't. It only needs each row of to have as many entries as . A matrix times a 2-entry vector is fine.
"Entry is column , row ." It's row first, then column. is row 3, column 2.
"." Usually not. Often only one of them even has shapes that fit.
Recap
- A matrix is a table of numbers. Its shape is (rows × columns), and in ML each row is usually one example.
- is one dot product per row of . It's also a weighted sum of the columns of .
- dots every row of with every column of . The shapes must fit: .
- The transpose swaps rows and columns.
- scores every example against every column of weights at once. It's what a neural-network layer computes.
Section 2 shows how to compute these in PyTorch.