Textbook › Part 0 Mathematical Foundations
CHAPTER 0-1

Laid Out in a Grid, It Computes — Linear Algebra

FREE10 min

Read deep-learning code and almost every line is either "multiply matrices" or "multiply element by element". This chapter puts both down from the definition, states when shapes are allowed to meet, and adds two ways of looking at a matrix: eigenvalues and rank. The backpropagation of chapter 3 and the attention of chapter 8 are written in exactly the notation set here.

1. The inner product — how much two directions agree, as one number

Definition. For x,y∈Rnx, y \in \mathbb{R}^n the inner product is

x⊤y  =  ∑i=1nxiyix^{\top} y \;=\; \sum_{i=1}^{n} x_i y_i
(1)

and ∥x∥=x⊤x\|x\| = \sqrt{x^{\top}x} is the Euclidean norm of xx.

Geometrically x⊤y=∥x∥ ∥y∥cos⁡θx^{\top}y = \|x\|\,\|y\|\cos\theta, where θ\theta is the angle between the two directions. Rearranging,

cos⁡θ  =  x⊤y∥x∥ ∥y∥\cos\theta \;=\; \frac{x^{\top}y}{\|x\|\,\|y\|}
(2)

which cancels the lengths and leaves agreement of direction alone. Equation (2) is cosine similarity, the quantity used to compare embeddings (chapter 10).

FIG 1Turn b and stretch it: the inner product and the cosine move independently. Change only the length and the cosine does not budge — the cancellation in equation (2), made visible

Worked example. For x=(1,2,3)⊤x = (1,2,3)^{\top} and y=(7,9,11)⊤y = (7,9,11)^{\top}, x⊤y=1⋅7+2⋅9+3⋅11=58x^{\top}y = 1\cdot7 + 2\cdot9 + 3\cdot11 = 58.

2. The matrix product — inner products, laid out in a grid

Definition. For A∈Rm×kA \in \mathbb{R}^{m \times k} and B∈Rk×nB \in \mathbb{R}^{k \times n}, the product AB∈Rm×nAB \in \mathbb{R}^{m \times n} has entries

(AB)ij  =  ∑p=1kAipBpj(AB)_{ij} \;=\; \sum_{p=1}^{k} A_{ip} B_{pj}
(3)

The right-hand side of equation (3) is the inner product of row ii of AA with column jj of BB. So a matrix product is just m×nm \times n inner products arranged in a table. It also explains why the column count of AA must equal the row count of BB: the summation index pp appears on both sides, so its range has to be the same or the sum cannot be written.

Worked example.

A=(123456),B=(789101112),AB=(5864139154)A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix},\quad B = \begin{pmatrix} 7 & 8 \\ 9 & 10 \\ 11 & 12 \end{pmatrix},\quad AB = \begin{pmatrix} 58 & 64 \\ 139 & 154 \end{pmatrix}

(2,3)×(3,2)→(2,2)(2,3) \times (3,2) \to (2,2): the shape contracted. Check one entry: (AB)22=4⋅8+5⋅10+6⋅12=32+50+72=154(AB)_{22} = 4\cdot8 + 5\cdot10 + 6\cdot12 = 32 + 50 + 72 = 154.

Note. ABAB and BABA are different in general. Above, BABA is a (3,3)(3,3) matrix — not even the same shape.

3. The Hadamard product — same shape or nothing

Definition. For C,D∈Rm×nC, D \in \mathbb{R}^{m \times n} of equal shape, the Hadamard (element-wise) product is

(C∘D)ij  =  CijDij(C \circ D)_{ij} \;=\; C_{ij} D_{ij}
(4)

Equations (3) and (4) look alike but demand opposite things of the shapes: the matrix product needs the inner dimensions to agree, the Hadamard product needs all of them to agree.

Worked example.

(123456)∘(201132)=(20341512)\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} \circ \begin{pmatrix} 2 & 0 & 1 \\ 1 & 3 & 2 \end{pmatrix} = \begin{pmatrix} 2 & 0 & 3 \\ 4 & 15 & 12 \end{pmatrix}

The Hadamard product takes the lead in two fixed places: multiplying a gradient by the derivative of an activation (chapter 3 has ∘\circ in its equations), and gating how much to let through (the LSTM of chapter 5).

4. Rank — how many directions there really are

Definition. The rank of AA is the dimension of the space spanned by its columns.

When rows or columns can be written as multiples of each other, the number of genuine directions falls below the apparent size.

Worked example.

E=(123246101)E = \begin{pmatrix} 1 & 2 & 3 \\ 2 & 4 & 6 \\ 1 & 0 & 1 \end{pmatrix}

Row 2 is twice row 1. It is 3×33 \times 3, yet only two rows are independent. Computing the singular values gives 8.4354, 0.9183, 0.0000 — two of them nonzero, so rank(E)=2\mathrm{rank}(E) = 2 (the rank equals the number of nonzero singular values).

FIG 2A 28×28 matrix rebuilt from only its top r singular directions. Raise the rank slider and the picture returns. If a small r already looks close, that matrix really only holds that many directions

This view pays off when a large matrix can be replaced by a product of two small ones. Restricting ΔW∈Rd×d\Delta W \in \mathbb{R}^{d \times d} to rank rr lets us write ΔW=BA\Delta W = BA with BB of shape d×rd \times r and AA of shape r×dr \times d, cutting the stored numbers from d2d^2 to 2dr2dr. That is what LoRA does (chapter 9).

5. Eigenvalues and diagonalisation — the directions a matrix cannot turn

Definition. For a square AA, a scalar λ\lambda with Av=λvAv = \lambda v for some v≠0v \neq 0 is an eigenvalue and vv an eigenvector.

An eigenvector's direction survives multiplication by AA unchanged; only its length scales, by λ\lambda.

FIG 3A 2×2 matrix deforms the unit circle into an ellipse. Rotate v and you find the directions that stay on the same line after the deformation — the eigenvectors, stretched by the eigenvalue

Derivation. Av=λvAv = \lambda v is (A−λI)v=0(A - \lambda I)v = 0. A nonzero solution exists exactly when A−λIA - \lambda I has no inverse, i.e. when det⁡(A−λI)=0\det(A - \lambda I) = 0. Writing it out for 2×22 \times 2,

det⁡ ⁣(a−λbcd−λ)=λ2−(a+d)λ+(ad−bc)=0\det\!\begin{pmatrix} a-\lambda & b \\ c & d-\lambda \end{pmatrix} = \lambda^2 - (a+d)\lambda + (ad - bc) = 0
(5)

that is, λ2−(trA)λ+det⁡A=0\lambda^2 - (\mathrm{tr}A)\lambda + \det A = 0.

Worked example. For F=(2112)F = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} we have trF=4\mathrm{tr}F = 4 and det⁡F=3\det F = 3, so λ2−4λ+3=0\lambda^2 - 4\lambda + 3 = 0 and λ1=3, λ2=1\lambda_1 = 3,\ \lambda_2 = 1, with eigenvectors (1,1)⊤/2(1,1)^{\top}/\sqrt{2} and (−1,1)⊤/2(-1,1)^{\top}/\sqrt{2}.

Diagonalisation. Put the eigenvectors in the columns of PP and the eigenvalues on the diagonal of Λ\Lambda; then A=PΛP−1A = P\Lambda P^{-1}. Since FF is symmetric, PP is orthogonal and P−1=P⊤P^{-1} = P^{\top}. Computing PΛP⊤P \Lambda P^{\top} returns the original (2112)\begin{pmatrix} 2&1\\1&2\end{pmatrix}.

From here, repeated multiplication becomes cheap.

Ak=(PΛP−1)k=PΛP−1PΛP−1⋯PΛP−1=P Λk P−1A^{k} = (P\Lambda P^{-1})^{k} = P\Lambda P^{-1}P\Lambda P^{-1}\cdots P\Lambda P^{-1} = P\,\Lambda^{k}\,P^{-1}
(6)

Every interior P−1PP^{-1}P collapses to II, leaving the kk-th power of a diagonal matrix — each eigenvalue raised to kk. Check it: multiplying FF by itself four times gives (41404041)\begin{pmatrix} 41 & 40 \\ 40 & 41 \end{pmatrix}, and PΛ4P⊤P\Lambda^4 P^{\top} with λ4=81,1\lambda^4 = 81, 1 gives the same (41404041)\begin{pmatrix} 41 & 40 \\ 40 & 41 \end{pmatrix}. They agree.

Equation (6) pays off in chapter 5. An RNN multiplies by the same weights once per time step, so eigenvalues above 1 make λk\lambda^k explode and eigenvalues below 1 crush it to zero. Exploding and vanishing gradients are a statement about Λk\Lambda^k in equation (6).

6. Matching tensor shapes — nine-tenths of implementation

A "tensor" here is an array of numbers with three or more indices. What actually breaks in implementation is shape mismatch, so here is one layer, concretely.

A linear layer taking a batch of 32 inputs of 784 dimensions down to 128:

X⏟(32, 784)  W⏟(784, 128)  →  XW⏟(32, 128)\underbrace{X}_{(32,\,784)} \; \underbrace{W}_{(784,\,128)} \;\to\; \underbrace{XW}_{(32,\,128)}

The inner 784 disappears; the outer 32 and 128 remain — exactly the shape rule of equation (3). The number of multiplications is 32×784×128=3,211,26432 \times 784 \times 128 = 3{,}211{,}264.

The bias bb has shape (128,)(128,) — a single vector — yet it is added to all 32 rows. That is broadcasting: a missing axis is treated as a repetition of the same value. (32,128)+(128,)→(32,128)(32,128) + (128,) \to (32,128).

What this chapter does not claim

  • Diagonalisation of non-symmetric matrices is not covered. A=PΛP−1A = P\Lambda P^{-1} requires a full set of nn eigenvectors, and some matrices do not have one (Jordan form is not used in this book).
  • Singular value decomposition is used only for the fact "rank = number of nonzero singular values"; it is not derived. The definition of rank taken here is the dimension of the column space.
  • Numerical error (rounding, conditioning) is not covered. The singular value printed as 0.0000 above is not exactly zero; it computes to around 10−1610^{-16}.

Every number in this chapter was computed and checked (matrix product, Hadamard product, singular values, eigenvalues, and the agreement of F4F^4 with PΛ4P⊤P\Lambda^4P^{\top}).

Going deeper

Comments

Sign in to comment