ML Foundations
Maths, machine learning, deep learning and LLMs, from scratch. Free, step by step: every lesson is complete, with an animation, a worked example and exercises with answers.
Unit 0: Your toolkit
Get fast with the tools you will use every day. This unit is short: do it alongside the start of Unit 1.
- Meet machine learning: the big picture: Machine learning is getting a computer to learn a pattern from examples, instead of a person writing the rules by hand.
- Python basics: variables, lists, loops and functions: Python is the programming language of machine learning. You store values in variables, keep many values in a list, repeat work with a loop, and package steps into a function you can reuse.
- NumPy arrays, shapes and indexing: A NumPy array is a grid of numbers, all of the same type, with a shape that says how long it is in each direction.
- Broadcasting and vectorisation: Vectorisation means doing maths on whole arrays in one operation; broadcasting is NumPy's rule for combining arrays of different shapes by stretching any direction of size 1.
- Plotting results with Matplotlib: Matplotlib is the standard Python library for drawing charts; in ML you use it mostly to see loss curves, data and predictions.
- Git and GitHub for your projects: Git saves snapshots of your project, called commits, so you can go back, branch off and merge; GitHub stores those repositories online.
- Testing numerical code (np.allclose, pytest): A test is a small program that runs your code on a case where you already know the answer, and fails loudly when the result is wrong. Decimals are compared with a small tolerance.
Unit 1: Math foundations
The language of ML. You will see matrices as movements of space, gradients as directions, and loss functions as probabilities.
- Vectors, span and basis: A vector is a list of numbers that you can draw as an arrow. The span of some vectors is every point you can reach by scaling and adding them, and a basis is the smallest set that reaches the whole space.
- Matrices as transformations; matrix multiplication: A matrix is a recipe for moving every vector in space at once; multiplying two matrices means doing one movement after the other.
- Determinants: The determinant of a square matrix is one number: how much the matrix scales area (or volume), with a minus sign if it flips space over.
- Inverse, rank and solving Ax = b: The inverse of a matrix undoes what the matrix does. The rank counts how many independent directions survive it, and when the rank is full, Ax = b has exactly one solution.
- Dot product, projection and orthogonality: The dot product multiplies two vectors entry by entry and adds the results. It measures how much they point the same way, and it gives the length of one vector's shadow on another.
- Eigenvalues and eigenvectors: An eigenvector of a matrix is a direction the matrix does not turn, only stretches. Its eigenvalue is how much it is stretched.
- SVD, and the maths behind PCA: SVD splits any matrix into three simple moves: rotate, stretch, rotate. PCA uses it to find the directions in which data varies most, so you can keep a few and drop the rest.
- Derivatives and the chain rule: A derivative is the rate at which an output changes when you nudge the input; the chain rule says that for a function inside a function, you multiply the rates.
- Partial derivatives, gradients and Jacobians: A partial derivative is the rate of change when you nudge one input and hold the others still. The gradient puts all of them into one vector, which points in the direction of steepest increase.
- Gradient descent and the learning rate: Gradient descent makes a function smaller by taking many small steps downhill, against its gradient. The learning rate sets the size of each step.
- Random variables, distributions, expectation and variance: A random variable is a number decided by chance; its distribution says how likely each value is, its expectation is the long-run average, and its variance measures how spread out it is.
- Conditional probability and Bayes’ rule: Conditional probability is the chance of A once you know that B happened. Bayes' rule flips it round: from the chance of B given A, it gives the chance of A given B, using how common A was to begin with.
- Maximum likelihood estimation: Maximum likelihood estimation chooses the parameter values that make the data you actually observed as probable as possible.
- Statistics: sampling, confidence intervals, hypothesis tests: Statistics measures how far a result from a sample can be trusted. A confidence interval gives a likely range for the true value, and a hypothesis test asks whether a difference could just be luck.
- Entropy, cross-entropy and KL divergence: Entropy measures how uncertain a distribution is. Cross-entropy measures how surprised a model is by the real data, and KL divergence is the extra surprise that comes from the model being wrong.
- Convex optimisation: constraints, Lagrange multipliers and duality: A convex function is bowl-shaped, so any local minimum is the global one. A problem with a constraint is solved by adding the constraint with a Lagrange multiplier, and duality looks at the same problem from the constraint's side.
Unit 2: Classical ML, built by you
Build every classic algorithm yourself in NumPy. After this unit, a library is only a shortcut for something you already understand.
- The ML setup: features, labels, loss, train/validation/test: Supervised learning turns examples, each with features and a label, into a model. You fit it on a training set, make your choices on a validation set, and measure the final result once on a test set.
- Linear regression: normal equation and gradient descent: Linear regression predicts a number as a weighted sum of the features. It chooses the weights that make the mean squared error as small as possible.
- Logistic regression and the sigmoid: Logistic regression answers yes/no questions. It computes a score, squashes it into a probability with the sigmoid, and is trained with cross-entropy loss.
- Regularisation: L1 and L2: Regularisation adds a penalty for large weights to the loss. L2 shrinks every weight, while L1 pushes the weak ones to exactly zero.
- Bias–variance trade-off and overfitting: Bias is error from a model too simple to capture the pattern. Variance is error from a model so flexible that it chases the noise in its training data. Good models balance the two.
- Metrics: precision, recall, F1, ROC-AUC, confusion matrix: Classification metrics count the four outcomes (true and false positives and negatives) and summarise them: precision, recall, F1, accuracy, and ROC-AUC across all thresholds.
- Cross-validation and data leakage: Cross-validation scores a model by rotating which slice of the data is held out, so every row is tested once. Data leakage is any way that information from the held-out data sneaks into training.
- Feature scaling and encoding: Feature scaling puts numeric features on similar ranges, and encoding turns categories into numbers, so that models treat every feature fairly and don't invent orders that aren't there.
- k-nearest neighbours: k-nearest neighbours predicts by finding the k training points closest to a new point and letting them vote (or averaging their values, for regression).
- Decision trees: entropy and Gini: A decision tree predicts by asking a sequence of yes/no questions about single features, each chosen to make the resulting groups as pure as possible.
- Ensembles: bagging, random forests, gradient boosting: An ensemble combines many models. Bagging and random forests average trees trained on random resamples, which cuts variance. Boosting adds small trees one after another, each fixing the errors left so far, which cuts bias.
- Support vector machines: margin and hinge loss: A support vector machine separates two classes with the line (or plane) that leaves the widest possible margin to the nearest points, trained with the hinge loss.
- Naive Bayes: Naive Bayes classifies with Bayes' rule, assuming the features are independent once you know the class, so each feature's evidence can simply be multiplied in.
- k-means clustering: k-means groups unlabelled points into k clusters by repeating two steps: assign every point to its nearest centre, then move each centre to the average of its points.
- PCA in practice: In practice, PCA is used to compress features, remove noise, speed up other models and plot high-dimensional data, keeping just enough components to hold most of the variance.
- Gaussian mixture models and EM: A Gaussian mixture model describes data as a blend of several bell curves, and the EM algorithm fits it by alternating soft assignment of points (E-step) with refitting each curve (M-step).
- Generalisation: VC dimension and double descent: Generalisation is doing well on unseen data. The VC dimension measures how much a family of models is able to fit. Double descent is the surprise that test error can fall again once models grow big enough to fit the training data exactly.
Unit 3: Deep learning foundations
Build neural networks from a single neuron up. Write backprop by hand first. Then you can trust PyTorch, because you know what it does.
- The neuron and the multi-layer perceptron: A neuron computes a weighted sum of its inputs plus a bias, and passes it through an activation. A multi-layer perceptron (MLP) stacks layers of neurons, each layer feeding the next.
- Activation functions, and why non-linearity matters: An activation function is the non-linear step applied after each layer's weighted sum; without it, any number of layers collapses into a single linear layer.
- Backpropagation by hand: Backpropagation computes the gradient of the loss with respect to every weight by applying the chain rule from the output backwards, reusing each intermediate result.
- Autograd and computational graphs: Autograd records every operation into a computational graph as your code runs, then loss.backward() walks the graph in reverse to fill in every gradient automatically.
- Softmax and cross-entropy loss: Softmax turns a vector of raw scores (logits) into probabilities that sum to 1. Cross-entropy loss is −log of the probability given to the correct class. Together, their gradient is simply p − y.
- Optimisers: SGD, momentum, Adam; learning-rate schedules: An optimiser turns gradients into weight updates. SGD steps straight down the gradient, momentum adds velocity, and Adam also adapts the step size for every weight. A learning-rate schedule changes the step size during training.
- Weight initialisation; vanishing and exploding gradients: Weight initialisation chooses the random starting weights so that signals and gradients keep roughly the same size from layer to layer, instead of vanishing towards zero or exploding towards infinity.
- Batch norm and layer norm: A normalisation layer rescales activations to mean 0 and variance 1, then lets the network scale and shift them again. Batch norm does this for each feature across the batch; layer norm does it for each example across its features.
- Regularisation: dropout, weight decay, augmentation, early stopping: Regularisation in deep learning is any trick that stops a network memorising its training data: dropout, weight decay, data augmentation and early stopping are the main ones.
- PyTorch: nn.Module, DataLoader and the training loop: In PyTorch, an nn.Module holds the layers and weights, a DataLoader serves shuffled mini-batches, and the training loop repeats the same few steps: forward, loss, zero_grad, backward, step.
- Convolutional networks: convolution, pooling, receptive field: A convolutional layer slides small learned filters across an image, computing a dot product at every position, so the same pattern detector is reused everywhere. Stacking layers builds up from edges to whole objects.
- Residual connections (ResNet): A residual connection adds a block's input to its output. The block then only has to learn a change, and gradients get a direct path back through the network.
- RNNs, and why long sequences are hard: A recurrent neural network (RNN) reads a sequence one step at a time. It keeps a hidden state as its memory, updates it at every step, and uses the same weights each time.
- Embeddings: representing things as vectors: An embedding gives each item, such as a word, a token, a user or a product, a learned vector of numbers, stored as one row of a table. Similar items end up with vectors that are close together.
- Debugging training: overfit one batch, read loss curves: Debugging training means checking, in a fixed order, that the model can learn at all — sensible starting loss, overfitting one small batch — before reading the loss curves of real runs.
- LSTMs and GRUs: gates for long memory: An LSTM is a recurrent cell with a separate memory line, the cell state, and three learned gates that decide what to erase, what to write and what to reveal. A GRU does the same job with two gates.
Unit 4: Transformers and LLMs
Build a small GPT from scratch: tokenizer, attention, training and fine-tuning. This is how modern AI works inside.
- Tokenisation and byte-pair encoding (BPE): Tokenisation splits text into the units a language model reads, called tokens. Byte-pair encoding (BPE) builds those units by starting from single characters (or bytes) and repeatedly merging the most frequent neighbouring pair.
- Token embeddings and positional encoding: Token embeddings turn each token id into a learned vector, and positional encodings add information about where each token sits, because attention on its own ignores order.
- Self-attention: queries, keys and values: Self-attention lets every token look at all the tokens and build a new version of itself as a weighted mix of their values. The weights come from how well its query matches each token's key.
- Multi-head attention and the transformer block: Multi-head attention runs several smaller attentions side by side and joins their results. A transformer block wraps attention and an MLP, each with a layer norm and a residual connection, and a model stacks many of these blocks.
- GPT-style models and next-token prediction: A GPT is a stack of transformer blocks with a causal mask, trained to predict the next token; it generates text by predicting a token, appending it and repeating.
- Encoder models (BERT) vs decoder models (GPT): Encoder models, like BERT, let every token look in both directions and learn by filling in masked words, which suits understanding text. Decoder models, like GPT, look only backwards and learn by predicting the next word, which suits writing it.
- Vision transformers and CLIP: A vision transformer (ViT) cuts an image into patches and feeds them to a standard transformer as if they were tokens. CLIP trains an image encoder and a text encoder together, so that a picture and its caption get similar vectors.
- Sampling: greedy, temperature, top-k, top-p: Decoding turns the model's next-token probabilities into an actual choice: greedy takes the top token, temperature reshapes the distribution, and top-k and top-p cut off unlikely tokens before sampling.
- KV cache and the cost of generation: The KV cache stores the keys and values of every token already processed. Generating a new token then computes only that token's keys and values, instead of recomputing them for the whole sequence.
- Pretraining data and scaling laws: Pretraining is next-token prediction on an enormous text collection, and scaling laws describe how the resulting loss falls smoothly and predictably as model size, data and compute grow.
- Mixture of experts: A mixture-of-experts (MoE) layer replaces one big MLP with many expert MLPs, plus a router that sends each token to only a few of them. The model has many parameters, but uses only a fraction of them for each token.
- Fine-tuning and LoRA: Fine-tuning continues training a pretrained model on your own data. LoRA does it cheaply: it freezes the original weights W and trains a small low-rank update, so the layer uses W + B·A.
- Instruction tuning and preference tuning (RLHF, DPO): Instruction tuning trains a pretrained model on example conversations so it follows requests, and preference tuning (RLHF or DPO) then trains it on pairs of answers so it prefers the better one.
- Reasoning models and test-time compute: A reasoning model is trained to write out its steps before answering. Test-time compute means spending more computation at answer time, through longer reasoning or several samples and a vote, to get better answers.
Unit 5: AI engineering
Turn foundation models into real products: retrieval, evaluation, agents and deployment. This is the core of most AI engineer jobs.
- Prompting: clear instructions, examples, structured output: Prompting is writing the instructions, context and examples an LLM receives on each call; a good prompt is a complete, specific briefing that leaves the model little to guess.
- Structured outputs and function calling: Structured output makes the model reply in a fixed format that code can read, usually JSON that matches a schema. Function calling lets the model ask your code to run one of your functions, with the arguments it chose.
- Embeddings and semantic search: Semantic search finds texts with similar meaning by turning texts into embedding vectors and returning the ones whose vectors are closest to the query's vector.
- Vector databases and approximate nearest-neighbour search: A vector database stores embeddings with their metadata and an approximate nearest-neighbour (ANN) index, such as HNSW or IVF, that finds almost-the-closest vectors without comparing the query with every vector.
- RAG: chunking, retrieval and re-ranking: Retrieval-augmented generation (RAG) answers questions by retrieving relevant pieces of your documents and giving them to the model in the prompt, so it answers from that evidence.
- Context engineering: long context and memory: Context engineering is choosing exactly what goes into the model's context window on each call: instructions, examples, retrieved documents, history and tool results. The aim is to give it what it needs and nothing that distracts it.
- Evaluation: test sets, LLM-as-judge, error analysis: Evaluation measures whether an LLM system does its job. It uses a fixed test set of real inputs, automatic checks or judges for scoring, and careful reading of the failures to find what to fix.
- Agents and tool use: An agent is an LLM running in a loop. It decides an action (usually a tool call), sees the result, and decides again, until the task is done or a limit is reached.
- MCP: connecting models to tools and data: The Model Context Protocol (MCP) is an open standard that lets any AI application connect to tools and data through MCP servers. Each integration is built once, and works with every MCP-compatible app.
- Choosing between prompting, RAG and fine-tuning: Prompting, RAG and fine-tuning are three ways to adapt an LLM: prompting changes the instructions, RAG supplies knowledge at question time, and fine-tuning changes the model's weights to change its behaviour.
- Dataset engineering for fine-tuning: Dataset engineering is designing, collecting, cleaning and checking the examples you fine-tune on, because a fine-tuned model learns exactly what its data shows, mistakes included.
- Serving: APIs, Docker, latency and cost: Serving puts a model behind an API so applications can use it, packaged (often in a Docker container) with everything it needs, and measured for latency, throughput and cost.
- Caching, model routing and cost control: Cost control for LLM apps uses caching (reuse answers or processed prompts), routing (send easy requests to cheaper models) and budgets (limits, timeouts and per-request cost tracking).
- Observability: tracing and debugging LLM apps: Observability means recording what your LLM app did on every request: prompts, retrieved chunks, tool calls, outputs, tokens, time and cost. The record is a trace you can inspect, search and learn from.
- Safety: guardrails and prompt injection: Prompt injection is text hidden inside data the model reads (a web page, email or document) that tries to override its instructions. Guardrails are the checks, permission limits and approvals that limit the damage.
- ML system design: pipelines, training vs serving, drift: ML system design is building everything around a model — data collection, labelling, features, training, serving, monitoring and retraining — so it keeps working in production as the world changes.
Unit 6: Systems and scale
Make models fast and cheap. Understand what limits speed and memory on a GPU, and how training works across many GPUs.
- How a GPU works: compute vs memory: A GPU runs thousands of simple cores in parallel. How fast your code runs depends on whether it is limited by the maths (compute-bound) or by moving data to the cores (memory-bound).
- Mixed precision: fp16 and bf16: Mixed precision trains with 16-bit numbers (bf16 or fp16) for most of the maths while keeping a few sensitive values in 32-bit, roughly halving memory traffic and greatly speeding up training.
- Training memory: weights, gradients, optimiser states, activations: Training memory is the sum of the weights, the gradients, the optimiser states and the activations saved for backprop. With Adam and mixed precision, it is about 16 bytes per parameter before activations.
- Parallelism: data, tensor and pipeline (the ideas): Parallelism spreads training across many GPUs: data parallelism splits the batch, tensor parallelism splits each layer's matrices, pipeline parallelism splits the layers, and FSDP/ZeRO shard the training state.
- Inference speed: batching, quantisation, FlashAttention: Inference is sped up by serving many requests together (batching), storing weights in fewer bits (quantisation), and computing attention without writing the full score matrix to memory (FlashAttention).
- Profiling PyTorch code: Profiling measures where time is actually spent in your code — CPU, GPU, data loading, copies — so you optimise the real bottleneck instead of guessing.
- GPU kernels with Triton: A GPU kernel is a function that runs as many parallel instances on the GPU, and Triton lets you write kernels in Python-like code. The biggest win is usually fusion: doing several steps in one kernel, so that the data leaves slow memory only once.
- Pruning and distillation: Pruning removes weights, neurons or heads that matter little; distillation trains a small student model to imitate a large teacher's full output probabilities; both make models smaller and cheaper to run.
- Speculative decoding: Speculative decoding speeds up generation: a small draft model proposes several tokens, and the large model verifies them in a single pass. The output is exactly what the large model would have produced on its own.
Unit 7: Generative models and reinforcement learning
Models that create new data, and models that learn by trial and error. These ideas power image generators, and the reinforcement learning behind RLHF.
- Autoencoders and VAEs (the ELBO): An autoencoder squeezes data into a small code and rebuilds it. A variational autoencoder (VAE) makes the code a probability distribution, pulled towards a simple bell curve, so that you can sample new codes and decode them into new data.
- GANs: a generator against a discriminator: A generative adversarial network (GAN) trains two networks against each other: a generator that turns random noise into fake samples, and a discriminator that tries to tell fake from real. Each improves by beating the other.
- Diffusion models: A diffusion model generates data by learning to remove noise. It is trained to predict the noise added to real data. Then it generates by starting from pure noise and denoising step by step.
- Reinforcement learning basics: states, actions, rewards, values: In reinforcement learning, an agent takes actions in an environment, receives rewards, and learns a policy — what to do in each state — that maximises the total (discounted) reward over time.
- Deep Q-networks (DQN): Q-learning with a neural network: A deep Q-network replaces the Q-table of Q-learning with a neural network that takes a state and outputs a value for every action. It is trained on remembered experience, towards targets computed by a slowly updated copy of itself.
- Policy gradients and PPO: Policy-gradient methods train a policy network directly by making actions that led to better-than-expected returns more likely; PPO adds a clipped objective that keeps each update small and safe.