Lecture 5
Introduction to Machine Learning
KU Leuven, campuses Diepenbeek and Geel
Academic year 2026–2027
Lecture 4 ended with a chessboard: two classes in alternating quadrants.
A model that builds its own features, and learns them by gradient descent.
composed nonlinear layers
layers of weighted sums and nonlinearities: only the model changes
cross-entropy, squared error
the losses of lectures 3 and 1
gradient descent
the same descent, now on minibatches
Exercise 3, after lecture 6, implements the forward pass and backpropagation of today in numpy.
Lecture 2 fitted a curve by building the features \(x, x^2, \dots, x^p\) and then a line.
Someone chose powers as the family, and \(p\) had to be searched for.
Lecture 4 took the other road: a tree carves the space instead of transforming it.
The features are weights like any other, and the same gradient descent finds them.
What a hidden unit computes is learned, not chosen.
Block 1
Weights, a bias, a weighted sum and a nonlinear function.
Lecture 1 wrote the intercept as \(w_0\) with \(x_0 = 1\), and lecture 3 kept it. A neuron calls it the bias \(b\).
Hidden: any layer that is not the input or the output. Its values are never compared with a target.
A network of 2 inputs, a hidden layer of 2 units and 1 output is written 2-2-1. Block 2 works that one by hand.
one weight per input per unit, plus one bias per unit
| network | parameters |
|---|---|
| 2-2-1 | 6 + 3 = 9 |
| 2-16-1 | 48 + 17 = 65 |
| 784-128-10 | 101,770 |
The last one reads a 28 × 28 image and names one of ten classes. Every parameter is found by gradient descent.
\(\mathrm{ReLU}(z) = \max(0, z)\)
The four corners give 0, 1, 1, 0: XOR, exactly.
\(s = x_1 + x_2\), \(\quad h_1 = \mathrm{ReLU}(s)\), \(\quad h_2 = \mathrm{ReLU}(s - 1)\), \(\quad \yhat = h_1 - 2h_2\)
| \(x_1\) | \(x_2\) | \(s\) | \(h_1\) | \(h_2\) | \(\yhat\) | \(y\) |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 2 | 2 | 1 | 0 | 0 |
Both units read the same number \(s\). The second switches on one step later than the first.
The same 2-2-1 network, weights started at random and trained on the four corners.
It found a different answer: \(h_1\) fires when \(x_2 > x_1\), \(h_2\) when \(x_1 > x_2\).
"The inputs differ", instead of "the sum is one". Both separate the classes.
Five random starts, same network, same 4,000 epochs.
One of the five solves XOR. The other four stop with at least one corner wrong.
Two hidden units is the smallest network that can solve XOR, and nothing promises that descent reaches it.
Block 3 explains what goes wrong and what to do about it.
For one example: \(\vh = g(\mW\T\vx + \vb)\)
Two linear layers are one linear layer. Depth adds nothing without a nonlinear \(g\) between the layers.
Three ReLU units, one input: the wind speed.
Each unit adds a ramp that starts at its own speed.
One ramp points down from 12.1 m/s and flattens the curve at the rated power.
| unit | switches on | slope | at 6 m/s | at 10 m/s | at 14 m/s |
|---|---|---|---|---|---|
| 1 | 4.3 m/s | 86 kW per m/s | 151 | 496 | 841 |
| 2 | 7.1 m/s | 236 kW per m/s | 0 | 683 | 1,628 |
| 3 | 12.1 m/s | −297 kW per m/s | 0 | 0 | −559 |
| output bias: 34 kW | 185 | 1,213 | 1,944 | ||
Below 4.3 m/s every unit is off and the network predicts its bias alone.
For any continuous \(f\) on a bounded range and any \(\varepsilon\), there exist weights within \(\varepsilon\).
Existence, not a method. The five XOR starts already showed the second gap.
These data need one. Three units already come close to the noise floor of the turbine, and four solve the chessboard.
Depth earns its place when the features are raw and plentiful.
An image has no useful hand-made feature. Early layers find edges, later ones find parts, later still whole objects.
Lecture 7 builds such a stack. Block 3 is about keeping one trainable.
Checkpoint
Block 2
Gradient descent, lecture 1
\(\nabla\loss\) needs \(\partial \loss / \partial w\) for every weight in every layer.
The 2-2-1 network of the next slide has 9 weights and biases. A network for images has millions.
Two stages, two factors, multiplied. Check it without the rule: \(\loss = 9w^2 + 6w + 1\), so \(d\loss/dw = 18w + 6 = 42\).
Read \(d\mZ\) as the error of a layer: how wrong its pre-activations are.
Output layer: \(p - y\), the error of lecture 3.
Every layer does the same two things, whatever its depth. That is the whole algorithm.
\(-0.373\) was computed once and used four times: for both output weights, and for both hidden units.
Each node passes its derivative to the nodes before it: one sweep from the loss to the inputs.
One backward pass costs a small multiple of one forward pass, whatever the number of weights. This is backpropagation.
For one hidden layer. The count does not grow with the weights, though each product does.
\(\mZ_1\), \(\mH_1\) and \(\mZ_2\) are kept until the backward pass has used them: \(n(2m + 1)\) numbers.
\(n = 32\), \(m = 16\): 1,056. \(n = 256\), \(m = 128\): 65,792.
forward, keep Z and H:
Z1 ← X W1 + b1
H1 ← ReLU(Z1)
P ← σ(H1 W2 + b2)
backward:
dZ2 ← (P − y) / n
dW2, db2 from H1, dZ2
dZ1 ← dZ2 W2ᵀ ⊙ [Z1 > 0]
dW1, db1 from X, dZ1
# forward
Z1 = X @ W1 + b1
H1 = np.maximum(Z1, 0)
Z2 = H1 @ W2 + b2
P = 1 / (1 + np.exp(-Z2))
# backward
dZ2 = (P - y) / len(y)
dW2 = H1.T @ dZ2
db2 = dZ2.sum(axis=0)
dH1 = dZ2 @ W2.T
dZ1 = dH1 * (Z1 > 0)
dW1 = X.T @ dZ1
db1 = dZ1.sum(axis=0)
\(\mX\) is \(n \times d\), and \(y\) a column of shape \(n \times 1\): y[:, None].
\(\varepsilon = 10^{-6}\), one weight at a time
Below \(10^{-6}\): the backward pass is right.
On the worked example: about \(10^{-10}\).
Exercise 3 checks its backpropagation this way.
| mistake | symptom |
|---|---|
y is flat, not a column | P - y becomes \(n \times n\); shapes fail, or the loss is nonsense |
| no division by \(n\) | gradients \(n\) times too large; the check fails by a factor of \(n\) |
| the ReLU mask forgotten | relative error of order one, only in the first layer |
Run the gradient check first, on three examples and two hidden units. It finds all three in a second.
composed layers
the only new part
cross-entropy
as in lecture 3
gradient descent
backpropagation computes the gradient, it is not an optimiser
Checkpoint
Break
Block 3
| range | largest derivative | use | |
|---|---|---|---|
| sigmoid | (0, 1) | 0.25 | output, for a probability |
| tanh | (−1, 1) | 1 | hidden, before 2010; still in the cells of lecture 8 |
| ReLU | [0, ∞) | 1 | hidden, the default |
| leaky ReLU | (−∞, ∞) | 1 | hidden, when units die |
tanh beats sigmoid as a hidden activation on both counts: it is centred on zero, and its derivative reaches 1 rather than 0.25.
Backward through \(L\) sigmoid layers multiplies \(L\) factors \(\sigma'(z) \le 0.25\).
Eight hidden layers at initialisation: the gradient of the first layer is more than 100,000 times smaller than that of the last.
ReLU with He initialisation: a factor of about 3.5.
Worse than the bound, since \(\sigma'\) reaches 0.25 only at \(z = 0\), and the weights shrink the signal too.
An active unit passes the gradient through unchanged.
A unit that is inactive for every example receives no gradient and stays dead. The initial weights and the learning rate decide how many die.
The same product can grow instead of shrink.
Factors above one, raised to the depth, give a gradient too large to step with: the loss jumps, or becomes nan.
The cure is to cap the size of the whole gradient before the step.
Lecture 8 clips, where 48 time steps act like 48 layers.
Start every weight at the same value, for example zero.
Every hidden unit computes the same output, receives the same gradient and takes the same step.
After training, the units are still identical: the layer acts as one unit.
The initial weights are therefore random. Randomness breaks the symmetry.
Four hidden units, every weight started at 0.1, 20 epochs on the bearings.
\[ \mW_1 = \begin{pmatrix} 0.911 & 0.911 & 0.911 & 0.911 \\ 0.449 & 0.449 & 0.449 & 0.449 \end{pmatrix} \]They moved, and they moved together. Four units, one distinct column.
Started at exactly 0: every unit is inactive, so nothing moves at all. Only the output bias learns.
Glorot for sigmoid and tanh, He for ReLU. For layers of equal width, Glorot is \(1/n_{\text{in}}\).
shape = (n_in, n_out)
W = rng.standard_normal(shape)
W *= np.sqrt(2 / n_in) # He
The bearings: vibration about 1.8 mm/s, temperature about 55 °C.
A sensible weight for one is a hopeless weight for the other.
| after 50 epochs | training loss | test |
|---|---|---|
| raw | 0.325 | 90.0% |
| standardised | 0.133 | 94.6% |
90.0% is the constant healthy model of lecture 3. On raw features the network learned nothing in 50 epochs.
A minibatch of \(B\) examples sits between: a gradient averaged over \(B\), and \(n / B\) steps per epoch.
1,000 training bearings, one pass:
| \(B\) | steps |
|---|---|
| 1 | 1,000 |
| 32 | 32 |
| 1,000 | 1 |
Each row costs the same: 1,000 examples through the network, forward and backward.
Same cost, a thousandfold difference in how far the weights travel.
for each epoch:
shuffle the n examples
for each batch of B:
g ← gradient on the batch
W ← W − η g
for epoch in range(epochs):
order = rng.permutation(n)
for s in range(0, n, B):
rows = order[s:s + B]
g = grad(X[rows], y[rows])
W = W - lr * g
2-16-1 on the bearings, 100 epochs, counting units that no training point switches on.
| \(\eta\) | dead | loss |
|---|---|---|
| 0.3 | 0 of 16 | 0.135 |
| 1.0 | 0 of 16 | 0.145 |
| 3.0 | 0 of 16 | 0.224 |
| 10.0 | 14 of 16 | 0.471 |
Oversized steps drive a unit below zero for every bearing, and the mask of block 2 keeps it there.
Enough to train the networks of this course. Lecture 6 makes step 4 easier with Adam, which still has a rate to choose.
Checkpoint
| Logistic regression | Network | |
|---|---|---|
| Features | given, or built by hand | learned |
| Model | \(\sigma(\vw\T\vx + b)\) | \(\sigma(\mW_2\T g(\mW_1\T\vx + \vb_1) + b_2)\) |
| Parameters | \(d + 1\) | \(dm + m + m + 1\) |
| Gradient | one formula | backpropagation |
| Loss, optimiser | unchanged | |
The bearings: the line ties the network at 94.7%.
Lecture 7 onwards.
composed nonlinear layers
the change from lecture 3: only the model
cross-entropy, squared error
unchanged since lectures 1 and 3
gradient descent
on minibatches, with gradients from backpropagation
All three books are free to read online.
Optimisers and schedules, regularisation of networks, and PyTorch with its training loop.
Two moons and Fashion-MNIST: the forward pass and backpropagation of today in numpy, checked against a numerical gradient, then PyTorch.
It follows lecture 6 and uses lectures 5 and 6.