Concept · Chapter 2: The Math Toolkit
Matrix Multiplication
Multiplying a vector by a matrix transforms it — every output number is a dot product of one matrix row with the input — and that is exactly what a neural-network layer does.
The problem
We need one operation that can take a vector of features and produce a new vector of features — rotating, stretching, mixing and selecting information — with learnable parameters.
The solution
Arrange the parameters in a grid W. Each output entry is the dot product of one row of W with the input, so W·x computes many weighted sums at once.
The consequence
Neural networks become stacks of matrix multiplications with nonlinearities between them, and GPUs — machines built for fast matmul — become the engines of modern AI.
You should understand first
- Vectors
- Dot Product
- Matrix Multiplication
Intuition
A matrix is a machine that takes a vector in and gives a vector out. Feed it the feature vector of a house, and it can produce a new vector whose first entry is "price-relevant signal", whose second entry is "size-to-age ratio signal", and so on — each one a different weighted sum of the inputs.
Geometrically, a matrix moves space around: it can rotate, stretch, shear, or flatten it. Lines stay lines and the origin stays put — that's what "linear" means.
Tiny numeric example
Set up
Row 1 · x
Row 2 · x
Result
— two dot products, one per row.
The equation
Try it
Try it
Edit a 2×2 matrix and watch it stretch, rotate, shear or collapse the plane — then add ReLU and see how a neural-network layer folds space.
Where it appears in AI
- A layer: . mixes the input features; the nonlinearity (e.g. ReLU) keeps stacked layers from collapsing into one big matrix.
- Attention: , , , and the score table — all matrix products.
- Batching: put 64 inputs as the rows of and one matrix product processes all of them at once. This is why GPUs are so effective.
Deep diveEigenvectors in one paragraphShould know
Most vectors change direction when a matrix is applied. Eigenvectors are the special directions that only get stretched: , where the stretch factor is the eigenvalue. They reveal what a matrix "really does". In ML you'll meet them in PCA (the top eigenvectors of a data covariance matrix are the directions of greatest variance) and in analyses of training stability, where very large eigenvalues of the loss curvature limit how big a learning rate can be. Understanding-level is enough for now.
Why should I care?
As a researcher
Papers describe models almost entirely as matrix products (XW, QKᵀ, W₂φ(W₁x)). Reading them requires tracking what each matrix multiplies and what shapes come out.
As an engineer
Nearly all the compute in training and serving a model is matrix multiplication; its cost is what you pay for in GPU hours, and its shapes decide memory use.
Modern systems that depend on it
- Every neural-network layer
- Q/K/V projections in attention
- Embedding lookups (a one-hot vector times a matrix)
- GPU and TPU hardware design
Historical context
Before
Linear algebra dates to the 19th century; early ML used it for linear models and PCA, with features chosen by hand.
After
Deep learning stacked learned matrices with nonlinearities, and hardware (GPUs, TPUs, tensor cores) was specialized for matrix multiplication.
Used today
Every forward and backward pass of every neural network. Most of the arithmetic in an LLM is matrix multiplication.
What to remember
- W·x: each output entry = (row of W) · x.
- Shapes: [m × n] · [n × p] = [m × p]; the inner dimensions must match.
- The columns of W are where the basis vectors land.
- Multiplying matrices = applying one transformation after another.
- A neural-network layer is σ(Wx + b): a linear transformation, then a nonlinearity.
Watch
3Blue1Brown
Linear transformations and matrices | Chapter 3, Essence of linear algebra
The single best explanation of why a matrix is a transformation of space — which is exactly what a neural-network layer does.
3Blue1Brown
Matrix multiplication as composition | Chapter 4, Essence of linear algebra
Shows why multiplying matrices means applying one transformation after another — exactly what stacking neural-network layers does.
3Blue1Brown
Eigenvectors and eigenvalues | Chapter 14, Essence of linear algebra
The clearest visual intuition for eigenvectors: directions a transformation only stretches.