Linear Algebra
Matrices and matrix multiplication
Linear maps and the operation ML runs on
A matrix looks like a spreadsheet, a rectangle of numbers. But it is secretly a machine: feed it a vector and it hands you back a transformed vector. Matrix multiplication chains those machines, and it is the beating heart of every neural network.
When people say a language model is "just a lot of matmuls," this lesson is the "just." A stores a transformation as a grid: each column tells you where one input direction lands. Multiplying a matrix by a vector mixes the columns; multiplying two matrices takes a dot product of every row against every column. Two pictures, one operation.
A grid of rows and columns
A matrix with rows and columns is an matrix; you read the shape as "rows by columns," always. The entry in row , column is written . Here are the two matrices we will work with for the whole lesson.
You can slice a matrix two ways. Its rows are horizontal vectors; its columns are vertical vectors. That column view is the key to everything below.
Matrix times a vector: mix the columns
The most useful way to read is not "row by row." It is: use the entries of as weights, and add up the columns of . Each input coordinate says "how much of column " to include.
So is a linear combination of 's columns. Every output the matrix can ever produce is some weighted sum of those columns, a set called the . Turn the weights below and watch three columns get scaled and stacked tip-to-tail into a single result.
y = 1·[2, 1] + 1·[1, 4] + 1·[3, 2] = [6, 7]
Matrix times a matrix: rows meet columns
To multiply by , march through the output grid one cell at a time. The entry is the dot product of row of with column of .
This only type-checks when the shapes line up: an times an gives an . The two inner numbers must match; the outer numbers become the answer's shape. Our is and is , so is . Step through it, cell by cell.
A · 2×3
B · 3×2
C · 2×2
C11 = 2·1 = 2
Tip: hover any cell of C to spotlight the row of A and column of B that feed it.
The two views are the same view
Identity, order, and composition
The same three products in NumPy.
import numpy as np
A = np.array([[2, 1, 3], [1, 4, 2]]) # shape (2, 3)
B = np.array([[1, 2], [3, 1], [2, 1]]) # shape (3, 2)
C = A @ B # (2, 2) -> [[11, 8], [17, 8]]
x = np.array([1, 1, 1])
y = A @ x # (2,) -> [6, 7] = a1 + a2 + a3, the sum of A's columns
I = np.eye(3) # 3x3 identity
(A @ I == A).all() # True: the identity leaves A untouchedCheck yourself
You can multiply an (m x n) matrix by a (p x q) matrix only when:
Reading Ax as a mix of A's columns, the entry x_j controls:
Give the two equivalent readings of matrix multiplication C = AB.
One is per cell, one is per column. Try to state it, then check.
Lock it in
- A matrix is a transformation: each column is where an input axis lands.
- Ax mixes A's columns weighted by x; the reachable set is the column space.
- C[i][j] is row i of A dotted with column j of B; inner dimensions must match.
- Matmul is function composition: associative, not commutative.
Primary source