Matrices and matrix multiplication
Be able to multiply a matrix by a vector and a matrix by a matrix, know which dimensions fit together, and understand that a layer in a neural network is a matrix multiplication.
Practise in Mattegrafen ↗ · Begreppet linjärt ekvationssystemPrerequisites
- DVectorsrequiredPractise in Mattegrafen ↗
Intuition
A matrix is a table of numbers — rows and columns. Multiply it by a vector and you get a new vector: every row of the matrix is dotted with the vector.
[2 0] [3] [2·3 + 0·1] [6]
[1 1] · [1] = [1·3 + 1·1] = [4]
That is exactly what a layer in a neural network does: the weights are the matrix, the input is the vector, the output is the new vector. A network = matrix multiplication, bend, matrix multiplication, bend …
Formal
A is m×n (m rows, n columns), B is n×p. Then AB is an m×p matrix with (AB)ᵢⱼ = Σₖ Aᵢₖ Bₖⱼ — row i of A times column j of B.
The dimension rule: the inner numbers must match (n = n), the outer ones give the result (m×p). A 3×4 matrix times a 4×2 gives 3×2. A 3×4 times a 3×4 does not work.
Matrix multiplication is not commutative: AB ≠ BA in general. The identity matrix I (ones on the diagonal) is neutral: AI = IA = A. The transpose Aᵀ swaps rows for columns.
A batch of 32 examples of 784 pixels is a 32×784 matrix; a layer of 128 neurons has a 784×128 weight matrix; the product is 32×128 — 128 numbers per example.
Mastery means
- Calculates a matrix-vector product by hand
- Works out the dimension of the result from the dimensions of the factors
Sign in to do the exercises and build your mastery up.
Sources
Leads to
- CImages as matrices
- DAttention
- DLinear regression with several features
- DNeural networks — the forward pass with matrices
- DPyTorch — tensors and autograd
- EConvolutional networks (CNNs)
- EJacobians and Hessians
- ELinear maps
- EMatrix factorisation and low-rank approximation
- EParallelism and why GPUs
- FQuantisation
Part of the goals (34)
- The mathematics behind the models
- Systems knowledge for AI engineers
- Seeing and hearing with AI
- Image classification with convolutional networks
- Run models more cheaply: quantisation
- Build a transformer from scratch
- Understand how generative AI works
- Interpreting a language model
- Train your first neural network
- Training neural networks for real
- Fine-tune a model with LoRA
- Classical ML for real
- Language models in practice
- Deep reinforcement learning
- AI safety in practice
- Build a voice interface
- Generative models in depth
- Frontier Lab — an independent research project
- Fine-tune and run your own models
- Classical machine learning in practice
- Build a RAG system you can trust
- Multimodal systems
- Responsible AI in practice
- Evals in practice
- Reproduce a paper
- Statistics for experiments
- Data: collect, clean, document
- AI in production
- Build an agent you can trust
- Build an NLP system end to end
- An AI service in operation
- Build a memory system for an agent
- Build an AI service that survives production
- AI, ethics and society