~/teaching · machine learning · reference
Deep learning, the essentials: backpropagation, batch norm, ResNet, convolution, LSTM, attention, VAE and GAN, derived and checked
Nine notions in the order they depend on each other, each with a hand computation, its derivation and ten lines of PyTorch.
Abstract
This is a reference deck of the Machine Learning course at Centrale Méditerranée (option DDEFI, 2026-2027), not a session. It is the document to open when a paper, a job interview or a colleague mentions a skip connection, a batch norm or attention, and you want to know in a few minutes what the thing is, why it was invented and how to check it by hand. It picks up where the session on neural networks stops: the neuron, backpropagation, Adam, initialisation and dropout are assumed.
Nine notions are covered, in the order of their dependencies rather than the order of textbooks: the computational graph, what a network trained with squared error on 0/1 targets estimates, the gradient step and the condition number, normalisation and batch norm, depth and ResNet, convolution, recurrent networks and the LSTM, attention, and the generative models (autoencoder, VAE, GAN). Each one follows the same grid: the idea with a small numeric example, the formula symbol by symbol, the derivation alone as on a board, the same lines explained, a check, a short PyTorch listing, the history and the current state of the art.
The deck is written for a group with two halves, those who prefer derivations and those who prefer code, and asks each half to read the other's track. After it you can redo each derivation on paper, predict the number a listing will print, and read an architecture in a paper or a model card knowing why it has that shape.
Plan of the deck
- How to read the deck. Two tracks, maths and code, the dependencies on session 8, and why deep learning matters on text, documents and simulation while boosting still wins on tables.
- Computational graph. A formula as a graph, the chain rule edge by edge, the backward pass on a single neuron, and reverse-mode automatic differentiation from 1964 to PyTorch.
- MSE and probability. Why squared error on 0/1 targets estimates the posterior probability, and what cross-entropy and calibration change.
- Gradient step. A narrow quadratic valley, the Hessian, the largest stable step, the optimal step and the rate set by the condition number.
- Normalise. The variance of a weighted sum, the Glorot and He constants, batch norm, the gradient that crosses it, and its cousins.
- Depth. Why one more layer multiplies the linear pieces, the degradation problem, the vanishing gradient and the residual connection.
- Convolution. A shared local filter computed by hand, the output size, the parameter count, the receptive field and the history from 1980 to vision transformers.
- Sequences. The recurrent network, the gradient through time, the LSTM gates and word embeddings.
- Attention. A similarity-weighted average, the formula of 2017 term by term, self-attention as a representation in context, and the transformer.
- Generative models and mini tutorial. Autoencoder, the VAE bound, the GAN game and its optimal discriminator, then six exercises in a maths and a code version.
The computational graph: backpropagation is a walk backwards
A formula can be drawn as a chain of small operations; to get every derivative at once, you walk that chain from the end, each box multiplying by its own slope.
Everything a deep learning library does when you call loss.backward() rests on one decomposition. The forward pass records each elementary operation as a node of a directed acyclic graph and fills in its value. The backward pass starts from the loss, whose derivative with respect to itself is 1, and visits the nodes in reverse order. Each node only needs its local derivative: a sum hands the incoming gradient to each input unchanged, a dot product multiplies it by the other factor, a square multiplies it by twice its argument. When a quantity reaches the loss through several paths, the contributions are added, which is why gradients accumulate in PyTorch and must be reset before each step.
The deck's neuron: , , , target
- Forward. The dot product gives , the bias brings the prediction to , and the squared loss is .
- Loss node. The derivative of the square is . The sign says that raising the prediction would lower the loss: we sit below the target.
- Sum node. It passes unchanged to the bias and to , so the gradient of is .
- Dot product node. It multiplies by the other factor, , so the gradient of is .
- One step. With a learning rate of 0.1, becomes and becomes 0.8, the prediction rises to 0.6 and the loss falls from 1 to 0.16.
import torch x = torch.tensor([1.0, 1.0]) w = torch.tensor([0.8, -1.4], requires_grad=True) b = torch.tensor(0.6, requires_grad=True) loss = (1.0 - (w @ x + b)) ** 2 # the graph is recorded here loss.backward() # reverse walk over the graph print(w.grad, b.grad) # tensor([-2., -2.]) tensor(-2.)
The decisive property is the cost. Reverse mode (Linnainmaa, 1970, made famous for networks by Rumelhart, Hinton and Williams in 1986) returns the gradient with respect to every parameter for roughly the price of one forward pass, while finite differences would need one pass per parameter. That is what makes a billion-weight model trainable. The same technique, under the name adjoint algorithmic differentiation, computes all the sensitivities of a portfolio in one backward sweep on trading floors. Its practical limit is memory, not compute: the graph keeps every intermediate activation, which is why gradient checkpointing exists.
What the loss estimates, and how fast the step gets there
A classifier trained with squared error on 0/1 labels ends up predicting a probability, and gradient descent moves at the pace of the flattest direction of the valley while its step is capped by the steepest one.
Squared error on 0/1 targets learns the posterior probability
Fix an input and suppose a share of the clients with exactly that profile default. The mean squared error of a prediction on these clients is a parabola in whose minimum sits at . With , the risk is 0.250 at , 0.210 at and 0.250 again at . A model flexible enough to choose its value freely at each therefore converges to . Nothing here is specific to networks, and a constrained model only reaches the best compromise inside its family.
Cross-entropy has the same minimiser. What differs is the gradient with respect to the logit: with squared error it carries the factor , which crushes learning when a neuron is confidently wrong, while with cross-entropy it is simply . Two practical warnings follow. A score is a probability only if the model is calibrated, which you check on a reliability diagram as in the session on decision thresholds. And oversampling the defaults changes the the model learns, so its output must be corrected before being read as a probability.
The Hessian and the condition number
Near a minimum, the loss behaves like a quadratic bowl described by its Hessian . Written in the basis of its eigenvectors, one gradient step multiplies the distance to the minimum along axis by , so each axis follows its own geometric sequence. The steepest axis sets the largest step that does not diverge, the flattest axis sets the speed, and their ratio, the condition number , sums up how hard the problem is.
| Step | Fate | |||
|---|---|---|---|---|
| 0.10 | (9.00; 0.00) | (5.90; 0.00) | (1.22; 0.00) | steep axis settled at once, flat axis crawls |
| 2/11, optimal | (8.18; −8.18) | (3.67; −3.67) | (0.18; 0.18) | both axes shrink by 9/11 per step |
| 0.21 | (7.90; −11.00) | (3.08; −16.11) | (0.09; 67.27) | beyond 2/10 = 0.2, the steep axis diverges |
The arithmetic checks out: and . The lesson for a project is that convergence speed depends on , not on the number of parameters. A loss that explodes points to a step above , a loss that no longer moves points to a large , which is exactly what normalisation and Adam's per-coordinate steps attack.
Normalisation and batch norm, with the gradient that crosses it
Each layer should hand the next one numbers of a sensible size; careful initialisation sets that scale at the start, and batch norm restores it during training, batch after batch.
A pre-activation is a sum of products of independent centred terms, so its variance is when the weights have standard deviation . With 100 standardised inputs and , the deck's simulation gives a variance near 120: a sigmoid then receives values around ±10, saturates and passes no gradient. Choosing brings the variance back to 1. Over layers the variance is multiplied by , so this is the forward-pass face of the vanishing gradient.
Glorot and Bengio (2010) average the forward constraint () and the backward one (, the fan-out); the uniform version of their rule has the same variance. He and co-authors (2015) double the variance for ReLU, which switches off half of the units. Initialisation only fixes the scale at the start, though. Batch norm (Ioffe and Szegedy, 2015) centres and scales each unit over the current mini-batch, then lets the network rescale with two learned parameters and , so it can undo the normalisation where that helps.
The classic interview question is the backward pass. The trap is that each input acts on the loss three ways: directly, through the batch mean and through the batch variance, since both depend on every . Adding the three paths and factoring gives a compact result: the outgoing gradient is the incoming one, minus its mean, minus its projection on , at scale . That matches the layer's invariance to shifting and rescaling its inputs.
The deck's batch of four: , , , incoming gradient
- Statistics. The batch mean is 3 and the variance (divided by ) is 2.5, so and .
- Parameters. The gradient of is the sum of the , 2; the gradient of is .
- Inputs. The scale factor is , and the bracket gives the input gradient .
- Check. PyTorch's autograd returns the same four numbers to within , which closes the loop with the computational graph.
Remark (The model.eval() trap). At inference a single example has no batch, so the layer uses running averages collected in training. Forgetting to switch the model to evaluation mode makes predictions depend on the other examples in the batch. With batches under about 16, batch statistics are noise: layer norm (per example, over its units) or group norm are the alternatives, and transformers always use layer norm or its RMSNorm variant.
Depth, the vanishing gradient and the residual connection
Extra layers let a network carve the input into far more pieces than extra neurons do, but the error signal fades as it travels down, and ResNet fixes this by adding each block's input to its output.
A ReLU network is piecewise affine, and each hidden layer folds the input space, which multiplies the number of linear regions instead of adding to it (Pascanu, Montúfar and Bengio, 2014). Take two inputs and twelve ReLU units in total: arranged as one layer they cut the plane into 79 regions, arranged as three layers of four they produce at least 176.
Yet stacking plain layers fails. He, Zhang, Ren and Sun (2015) showed a 56-layer network with a higher training error than a 20-layer one, although it could copy the shallow network by setting its extra layers to the identity: the capacity is there, the optimiser does not find it. The cause is a product. The gradient reaching the first layer is the output gradient times one Jacobian per layer, and with a sigmoid each factor is at most a quarter of the weight norm.
Ten layers with unit-scale weights thus deliver a gradient a million times smaller at the bottom, and factors above 1 make it explode instead. The remedies are ReLU (a factor of 0 or 1), normalisation, and the residual connection: each block learns a correction and adds its input back. Its Jacobian is the identity plus something, so the product always contains a path of norm 1. Initialising the last batch norm of each block at makes the network start as the identity, which is how 152 layers were trained for ImageNet 2015.
DenseNet, U-Net and GoogLeNet's auxiliary losses are variations on the same short path for the gradient, and every transformer is a stack of residual blocks. The deck closes the chapter with two results that unsettle the classical view of overfitting (networks can fit random labels; in large networks most local minima are nearly as good as the global one) and a five-question grid for reading any model card: depth and width, activation, where the normalisation sits, residual connections, and the loss.
Convolution: one small filter, the same everywhere
A convolutional layer slides one small detector across the whole image, so it needs very few weights and finds a pattern wherever it appears.
A dense neuron looks at every pixel with its own weight; a convolutional filter looks at a small window and reuses the same weights at every position. Two assumptions justify it: useful patterns are local (an edge fits in 3 × 3 pixels) and they mean the same thing anywhere in the image. The deck computes a vertical-edge filter by hand on a 4 × 4 image: at the top left, the left column minus the right column gives , no edge; one pixel to the right it gives .
| Case | |||||
|---|---|---|---|---|---|
| LeNet, no padding | 32 | 5 | 0 | 1 | 28 |
| "same" padding | 32 | 3 | 1 | 1 | 32 |
| stride 2 halves | 32 | 3 | 1 | 2 | 16 |
| first layer of ResNet | 224 | 7 | 3 | 2 | 112 |
Weight sharing is what makes the layer frugal. Six 3 × 3 filters on a colour image cost parameters, where a dense layer joining the same 32 × 32 × 3 inputs to the same 6 × 28 × 28 outputs would need about 14.5 million. The computation stays substantial, about 127,000 multiply-adds for that one small image, because every filter is evaluated at every position: convolutions carry the compute, final dense layers carry the parameters, and global average pooling removes most of the latter.
The receptive field, the patch of image a deep unit depends on, grows by per layer, multiplied by the strides accumulated below. Three stacked 3 × 3 layers see 7 pixels, like one 7 × 7 filter, for 27 weights against 49 and with two extra non-linearities, the argument on which VGG was built. The history runs from Fukushima's neocognitron (1980) and LeCun's LeNet-5 reading bank cheques (1998) to AlexNet in 2012, when the ImageNet top-5 error fell from 26 % to 15 %, then ResNet and vision transformers. In business, the uses are document reading (KYC files, invoices), satellite images, and fine-tuning a pre-trained network on a few thousand in-house images.
Sequences and attention: from the LSTM to the transformer
A recurrent network reads a sequence one step at a time and keeps a running summary, the LSTM lets that summary be copied forward without fading, and attention replaces the summary with a weighted look at every element.
A recurrent network updates a fixed-size hidden state from the previous state and the current input, with the same three matrices at every step. Unrolled in time, it is an ordinary deep network whose layers share their weights, trained by backpropagation through time. The gradient from step back to step 1 is again a product of Jacobians, but now with the same matrix each time, so its norm behaves like a power of . In practice a plain RNN does not learn dependencies longer than ten or twenty steps. Gradient clipping handles explosion; changing the cell handles the fading.
The LSTM (Hochreiter and Schmidhuber, 1997) keeps a memory updated by addition, controlled by sigmoid gates for forgetting, writing and output. With previous memory 3 and candidate −1, the gates copy the past (), replace it (), and mix the two (). When the forget gate is open, the gradient crosses a time step multiplied by a number close to 1: the additive path ResNet would rediscover for depth. Word embeddings (word2vec, 2013) turn each word into a learned row of a table, so that words used in similar contexts end up close.
Attention is a softened lookup: for a query, score every element by similarity, turn the scores into weights with a softmax, and return the weighted average. In the deck's example the three elements are , and and the query is the third one.
| Element | Vector | Score | Weight |
|---|---|---|---|
| 1 | (1, 0) | 1 | 0.212 |
| 2 | (0, 1) | 1 | 0.212 |
| 3 | (1, 1) | 2 | 0.576 |
| Output | (0.788, 0.788) | 1.000 |
In self-attention every word is in turn the query, and three learned projections let it play three roles: asking, being found, being read. The division by is the variance argument of the normalisation section again: a dot product of unit-variance terms has variance , and when its inputs are large the softmax collapses into a hard argmax whose gradient is zero. The deck's sentence example shows the payoff: next to "orange", the vector of "Apple" moves towards the fruit, next to "Microsoft" towards the company, which fixed embeddings cannot do. The cost is quadratic in the context length. The full worked example, with every matrix, is in the article Attention, step by step.
import torch, math def attention(Q, K, V): # scores scaled by sqrt(d_k), softmax per row, then weighted average of V w = torch.softmax(Q @ K.T / math.sqrt(K.shape[1]), dim=-1) return w @ V E = torch.tensor([[1., 0.], [0., 1.], [1., 1.]]) print(torch.softmax(E @ E[2], dim=0)) # unscaled weights: 0.212, 0.212, 0.576 print(attention(E, E, E)[2]) # scaled output: about (0.752, 0.752)
Generative models: autoencoder, VAE and GAN
Generative models learn what normal data looks like, either to draw new samples from it or to flag what does not fit, and the VAE and the GAN reach that goal by two very different routes.
An autoencoder compresses into a short code and reconstructs it; the bottleneck is mandatory, since otherwise copying the input is a perfect and useless solution. With linear layers and squared loss it recovers principal component analysis. Trained on normal transactions, it reconstructs poorly what it has never seen, which makes the reconstruction error a fraud or failure signal; its alarm threshold is chosen like any score threshold, by weighing false alerts against missed cases. It cannot generate, though: the codes do not fill a regular region, so a random decodes into nothing meaningful.
The VAE (Kingma and Welling, 2013) makes the encoder return a Gaussian over and puts a standard Gaussian prior on the codes. The likelihood itself is intractable, so the model maximises a lower bound obtained in four lines: integrate over , multiply and divide by the encoder's density, apply Jensen's inequality to the concave logarithm, and recognise a Kullback-Leibler divergence.
The first term wants the code to say everything about , the second wants it to say nothing in particular; their compromise is the bottleneck in probabilistic form. The deck's check of the closed form: for one coordinate with and , the divergence is . Training by gradient uses the reparameterisation with standard Gaussian noise , so that the randomness sits outside the parameters.
The GAN (Goodfellow and co-authors, 2014) writes no density at all. A generator turns noise into samples, a discriminator is an ordinary real-or-fake classifier trained with the log-likelihood, and the two play a min-max game. For a fixed generator, the best discriminator follows from maximising the integrand point by point.
In the deck's illustration, a point where the data density is 0.3 and the generator's is 0.1 gets , and at equilibrium the value of the game is . With the best discriminator, the generator therefore minimises a Jensen-Shannon divergence, which is zero only when it reproduces the data distribution. In practice the VAE tends to blur and the GAN to drop modes; Wasserstein losses and spectral normalisation stabilise GANs, FID measures quality alongside a human eye. Since 2021, diffusion models have taken over images, sound and video, while GANs and VAEs remain in tabular synthetic data tools such as CTGAN, used in finance to share a dataset without exposing clients and to build stress scenarios.
Remark (The mini tutorial). The deck ends with six fifteen-minute exercises, one per computable chapter (the graph, the gradient step, batch norm, convolution sizes and receptive field, attention with a temperature, the optimal discriminator and a VAE divergence), each in a maths version on paper and a code version in a notebook, and both versions are expected to give the same result. They are meant to be done in mixed pairs; their answers are kept in the deck and not reproduced here.
Key takeaways
- Backpropagation walks the computational graph backwards: each node multiplies the gradient by its local derivative, and the paths that meet are added.
- Minimising squared error, or cross-entropy, against a 0/1 label estimates its probability; cross-entropy learns faster when a neuron is confidently wrong, and calibration must still be checked.
- The gradient step is capped by the steepest direction of the loss and its speed is set by the flattest one, so everything depends on the condition number.
- Keep activations at unit variance layer after layer; batch norm learns its scale and shift and sends down a gradient centred and decorrelated from the normalised inputs.
- Gradients cross layers by multiplication, which makes them vanish or explode; the residual connection adds an identity path that always lets them through.
- A convolution is a shared local filter with few parameters and a receptive field that grows with depth; an LSTM copies its memory forward through an addition.
- Attention is a softmax-weighted average of values by query-key similarity; the VAE maximises a bound on the likelihood, while the GAN trains a generator against a classifier.
Glossary
- Computational graph
- The directed acyclic graph of elementary operations recorded during the forward pass and walked backwards to obtain gradients.
- Automatic differentiation
- Exact computation of derivatives by applying the chain rule node by node; reverse mode gives all the gradients of one output in a single backward pass.
- Condition number
- The ratio of the largest to the smallest eigenvalue of the Hessian; it sets the convergence rate of gradient descent.
- Batch norm
- A layer that centres and scales each unit with the statistics of the mini-batch, then rescales it with two learned parameters.
- Vanishing gradient
- The shrinking of the gradient as it is multiplied by one Jacobian per layer, or per time step, on its way back.
- Residual connection
- Adding a block's input to its output, so that the block learns a correction and the gradient keeps a direct path.
- Convolutional neural network (CNN)
- A network built from small filters whose weights are shared across all positions of an image or a signal.
- Receptive field
- The region of the input that influences a given unit; it grows with the kernel sizes and the accumulated strides.
- Recurrent neural network (RNN)
- A network that updates a hidden state at each step of a sequence with the same weights.
- Embedding
- A learned vector representing a category, for instance a word, stored as a row of a trainable table.
- Attention
- A weighted average of values, with weights given by a softmax of the similarities between a query and keys.
- Evidence lower bound (ELBO)
- The tractable lower bound on the log-likelihood that a VAE maximises: reconstruction minus a Kullback-Leibler term.
- Variational autoencoder (VAE)
- An autoencoder whose encoder returns a distribution over codes, trained on the ELBO, from which new samples can be drawn.
- Generative adversarial network (GAN)
- A generator and a discriminator trained against each other; at the optimum the generator reproduces the data distribution.
The other notes of the course
One public summary per deck, in the order of the course.
Previous: Python code for every ML modelNext: Open data ML projects by sector
- IntroductionAI winters, jobs and method
- ToolsKaggle, GitHub, Python: the tools
- Session 1Project framing and least squares
- Session 2Maximum likelihood and logistic regression
- Session 3Gradient descent, ridge and lasso
- Session 4Decision threshold and reproducible ML
- HomeworkA defensible baseline model
- Session 5Kernels, SVM and decision trees
- Session 6Forests, boosting, honest comparison
- Session 7SHAP, fairness, PCA and clustering
- HomeworkA documented champion model
- Session 8Neural networks, drift, conformal prediction
- HomeworkA delivered machine learning project
- Session 9Defending a machine learning project
- ReferenceCredit default scoring project
- ReferenceAsset returns: XGBoost and sentiment
- ReferencePython code for every ML model
- ReferenceDeep learning essentials
- ReferenceOpen data ML projects by sector
- ReferenceData and AI engineer handbook
- ReferenceData engineering, SRE and cloud
- ReferencePanorama of machine learning models
About these notes
Written by the lecturer from the slides of the deck, as a public summary: the key ideas, the formulas and one worked example. The exercises, their solutions, the tests and the grading stay in the deck, for the students of the course.
Revision: .