Maths toolkit3 / 17

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 w=(4,6)\mathbf{w} = (4, 6): 4 minutes per km and 6 minutes per traffic level. Now three deliveries are waiting:

DeliveryDistance (km)Traffic levelTime: w⋅x\mathbf{w} \cdot \mathbf{x}
A204×2+6×0=84 \times 2 + 6 \times 0 = 8
B514×5+6×1=264 \times 5 + 6 \times 1 = 26
C324×3+6×2=244 \times 3 + 6 \times 2 = 24

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:

X=(205132)X = \begin{pmatrix} 2 & 0 \\ 5 & 1 \\ 3 & 2 \end{pmatrix}

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 XX is (3×2)(3 \times 2). An entry has two subscripts, in the same order: X21X_{21} is row 2, column 1, which is delivery B's distance, 5. Tap the entries below.

kmtraffic
A
B
C
X has shape (3 × 2): 3 rows, 2 columns

X₂₁ = 5

Row 2 is delivery B: (5, 1)

Column 1 is distance (km) for every delivery: (2, 5, 3)

Tap any entry.

Each row of X is one delivery. Each column is one feature, for every delivery. An entry sits where a row meets a column.

Predict first

What is X₃₂?

A matrix times a vector

The matrix-vector product XwX\mathbf{w} takes the dot product of each row of XX with w\mathbf{w}. You get one number per row. Press play below to compute it one row at a time. The sliders under the figure change w\mathbf{w}.

kmtraffic
A20
B51
C32
X (3 × 2)
w
4
6
w (2)
min
?
?
?
X w (3)

Press play. Each row of X meets w in a dot product.

Step 0 of 6
Row by row: each output is one dot product, a row of X with w. Column by column: the output is a weighted sum of X's columns. Both give the same answer.
Xw=(205132)(46)=(2×4+0×65×4+1×63×4+2×6)=(82624)X\mathbf{w} = \begin{pmatrix} 2 & 0 \\ 5 & 1 \\ 3 & 2 \end{pmatrix} \begin{pmatrix} 4 \\ 6 \end{pmatrix} = \begin{pmatrix} 2 \times 4 + 0 \times 6 \\ 5 \times 4 + 1 \times 6 \\ 3 \times 4 + 2 \times 6 \end{pmatrix} = \begin{pmatrix} 8 \\ 26 \\ 24 \end{pmatrix}

These are the three delivery times from the table. For the product to work, each row of XX must have as many entries as w\mathbf{w}. A (3×2)(3 \times 2) matrix times a vector with 2 entries gives a vector with 3 entries.

In sum notation, entry ii of the answer is:

(Xw)i=∑j=12Xij wj(X\mathbf{w})_i = \sum_{j=1}^{2} X_{ij}\, w_j

Read it as: for row ii, multiply each entry XijX_{ij} by the matching weight wjw_j, then add. That's the dot product of row ii with w\mathbf{w}.

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, XwX\mathbf{w} is built from the columns of XX:

Xw=4(253)+6(012)=(82012)+(0612)=(82624)X\mathbf{w} = 4 \begin{pmatrix} 2 \\ 5 \\ 3 \end{pmatrix} + 6 \begin{pmatrix} 0 \\ 1 \\ 2 \end{pmatrix} = \begin{pmatrix} 8 \\ 20 \\ 12 \end{pmatrix} + \begin{pmatrix} 0 \\ 6 \\ 12 \end{pmatrix} = \begin{pmatrix} 8 \\ 26 \\ 24 \end{pmatrix}

Every distance is scaled by 4 minutes per km, and every traffic level by 6. So XwX\mathbf{w} is a weighted sum of the columns of XX. 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:

W=(4562)W = \begin{pmatrix} 4 & 5 \\ 6 & 2 \end{pmatrix}

Column 1 is the scooter's weights, (4,6)(4, 6). Column 2 is the bike's, (5,2)(5, 2). Now one product gives every delivery's time with every vehicle.

kmtraffic
A20
B51
C32
X (3 × 2)
scooterbike
45
62
W (2 × 2)
scooterbike
X W (3 × 2)

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.

Each entry of X W is one dot product: a row of X (a delivery) with a column of W (a vehicle). Tap any entry of the answer. Its whole column is X times one column of W.

Matrix multiplication XWXW takes the dot product of every row of XX with every column of WW. The answer has one row per delivery and one column per vehicle:

XW=(205132)(4562)=(81026272419)XW = \begin{pmatrix} 2 & 0 \\ 5 & 1 \\ 3 & 2 \end{pmatrix} \begin{pmatrix} 4 & 5 \\ 6 & 2 \end{pmatrix} = \begin{pmatrix} 8 & 10 \\ 26 & 27 \\ 24 & 19 \end{pmatrix}

Worked example

Delivery C by bike: entry (3, 2) of XW

  1. Take row 3 of XX, delivery C: (3,2)(3, 2).

In symbols, entry (i,j)(i, j) is row ii of XX dotted with column jj of WW:

(XW)ij=∑k=12Xik Wkj(XW)_{ij} = \sum_{k=1}^{2} X_{ik}\, W_{kj}

Here kk runs along the shared inner size: across row ii of XX and down column jj of WW at the same time. Each column of the answer is XX times one column of WW. 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 XX, written X⊤X^\top, swaps rows and columns. Row 1 of XX becomes column 1 of X⊤X^\top, and so on.

X has shape (3 × 2)

row 1 = delivery A · row 2 = delivery B · row 3 = delivery C

Transposing flips the matrix across its diagonal. Each delivery keeps its colour: its row becomes a column. Entries on the dashed diagonal stay put.
X=(205132)X⊤=(253012)X = \begin{pmatrix} 2 & 0 \\ 5 & 1 \\ 3 & 2 \end{pmatrix} \qquad X^\top = \begin{pmatrix} 2 & 5 & 3 \\ 0 & 1 & 2 \end{pmatrix}

Entry (i,j)(i, j) of X⊤X^\top is entry (j,i)(j, i) of XX, and the shape flips from (3×2)(3 \times 2) to (2×3)(2 \times 3). The transpose shows up mostly to make shapes fit.

Shapes must fit

Each entry of ABAB is a row of AA dotted with a column of BB, so those must have the same length. In shapes:

(n×p‾)  (p‾×m)  →  (n×m)(n \times \underline{p}) \; (\underline{p} \times m) \;\to\; (n \times m)

The two inner sizes must match. The answer takes the two outer sizes. For our deliveries: (3×2)(2×2)→(3×2)(3 \times 2)(2 \times 2) \to (3 \times 2).

Predict first

Can you multiply a (4 × 2) matrix by another (4 × 2) matrix?

A (4 × 2)
B (4 × 2)

(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.

A
rows4
cols2
B
rows4
cols2
Snap two matrices together. The product AB works only when A's columns match B's rows. The answer takes the outer sizes.

The order matters too. ABAB and BABA are usually different, even when both exist:

(1201)(0110)=(2110)(0110)(1201)=(0112)\begin{pmatrix} 1 & 2 \\ 0 & 1 \end{pmatrix}\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} 2 & 1 \\ 1 & 0 \end{pmatrix} \qquad \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\begin{pmatrix} 1 & 2 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 0 & 1 \\ 1 & 2 \end{pmatrix}

With ordinary numbers, 3×5=5×33 \times 5 = 5 \times 3. 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 b=(5,7)\mathbf{b} = (5, 7), one entry per column of XWXW, and add b\mathbf{b} to every row:

XW+b=(8+510+726+527+724+519+7)=(131731342926)XW + \mathbf{b} = \begin{pmatrix} 8 + 5 & 10 + 7 \\ 26 + 5 & 27 + 7 \\ 24 + 5 & 19 + 7 \end{pmatrix} = \begin{pmatrix} 13 & 17 \\ 31 & 34 \\ 29 & 26 \end{pmatrix}

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, XW+bXW + \mathbf{b}. A group of examples processed together is called a batch, and each row of XX 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 (n×p)(n \times p) matrix by a (p×m)(p \times m) one takes n×mn \times m dot products, each with pp multiplications. For two (1000×1000)(1000 \times 1000) matrices, that's a billion multiply-and-adds.

No dot product depends on another. Entry (1,1)(1, 1) doesn't need entry (1,2)(1, 2), 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

"XwX\mathbf{w} needs XX to be square." It doesn't. It only needs each row of XX to have as many entries as w\mathbf{w}. A (4×2)(4 \times 2) matrix times a 2-entry vector is fine.

"Entry XijX_{ij} is column ii, row jj." It's row first, then column. X32X_{32} is row 3, column 2.

"AB=BAAB = BA." 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.
  • XwX\mathbf{w} is one dot product per row of XX. It's also a weighted sum of the columns of XX.
  • ABAB dots every row of AA with every column of BB. The shapes must fit: (n×p)(p×m)→(n×m)(n \times p)(p \times m) \to (n \times m).
  • The transpose X⊤X^\top swaps rows and columns.
  • XW+bXW + \mathbf{b} 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.