Lecture 7
Introduction to Machine Learning
KU Leuven, campuses Diepenbeek and Geel
Academic year 2026–2027
The MLP of exercise 3 read Fashion-MNIST as 784 numbers:
convolutional layers
the same filter at every position
cross-entropy
over ten classes, the softmax of lecture 3
Adam
unchanged since lecture 6
Exercise 4 writes a convolution in numpy and trains a CNN with blocks 1 and 2.
Block 1
One dense layer of 128 units:
Compute the 3 × 3 output.
| −2 | 0 | 1 |
| 1 | −3 | 3 |
| 2 | 0 | −1 |
k ← width of the filter K
n ← width of X
m ← n − k + 1
for i = 0 … m − 1:
for j = 0 … m − 1:
P ← k × k patch at (i, j)
Y[i, j] ← Σ P ⊙ K
return Y, m × m
def conv2d(X, K):
k = K.shape[0]
m = X.shape[0] - k + 1
Y = np.zeros((m, m))
for i in range(m):
for j in range(m):
P = X[i:i + k, j:j + k]
Y[i, j] = np.sum(P * K)
return Y
A filter spans all \(C_{\text{in}}\) input channels, and each of the \(C_{\text{out}}\) filters gives one output channel:
Independent of the size of the image: every position shares the same filters.
Today's CNN: \(3^2 \cdot 1 \cdot 8 + 8 = 80\) and \(3^2 \cdot 8 \cdot 16 + 16 = 1{,}168\) parameters in its two convolutional layers.
layer = nn.Conv2d(1, 8, 5,
padding=2)
X = torch.zeros(64, 1, 28, 28)
Y = layer(X)
Y.shape # 64 × 8 × 28 × 28
Images enter as N × C × H × W: batch, channels, height, width.
The arguments are \(C_{\text{in}}\), \(C_{\text{out}}\) and \(k\), then stride and padding.
Checkpoint
Block 2
image, 1 × 28 × 28
conv 3×3, 8 filters → 8×28×28
ReLU
max pool → 8×14×14
conv 3×3, 16 filters → 16×14×14
ReLU
max pool → 16×7×7
flatten → 784
linear → 10 scores
model = nn.Sequential(
nn.Conv2d(1, 8, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(8, 16, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(16 * 7 * 7, 10),
)
loss_fn = nn.CrossEntropyLoss()
opt = torch.optim.Adam(
model.parameters(), lr=2e-3)
history = train(model, loader,
loss_fn, opt, epochs=8,
val_loader=val_loader)
train from mlcourse.training runs the loop of lecture 6 and returns the history per epoch.
8,000 training and 2,000 validation images of the subset of exercise 3, split with the lecture's own seed.
The layers learn a correction to their input. The gradient reaches earlier layers through the identity path.
ResNet, 2015: residual blocks stacked to 152 layers.
Checkpoint
Break
Block 3
model = lecture_cnn()
model.load_state_dict(pretrained)
for p in model.parameters():
p.requires_grad = False
model[-1] = nn.Linear(784, 3)
Frozen weights get no gradient. The new layer does, since it is created after the loop.
The optimiser receives only the head: model[-1].parameters().
signal, 1 × 1024
conv 16, 4 filters, stride 2
→ 4 × 505
ReLU, max pool 4 → 4 × 126
conv 8, 8 filters → 8 × 119
ReLU
max over time → 8
linear → 2 scores
model = nn.Sequential(
nn.Conv1d(1, 4, 16, stride=2),
nn.ReLU(),
nn.MaxPool1d(4),
nn.Conv1d(4, 8, 8),
nn.ReLU(),
nn.AdaptiveMaxPool1d(1),
nn.Flatten(),
nn.Linear(8, 2),
)
Checkpoint
convolutional layers
the same filter at every position
cross-entropy
over ten classes
Adam
in PyTorch, through train
All three books are free to read online.
In week 8: a convolution in numpy, a CNN on Fashion-MNIST with train, then a recurrent network on electricity load.
It uses lectures 7 and 8.
Recurrent neural networks: structure for time.