ML Notes Module3 4
ML Notes Module3 4
Modules at a Glance
Module 3 [9L]: The Linear Model II (Logistic Regression, Likelihood, Gradient
Descent) & Neural Networks (Model, Backpropagation, RBF, RNN, CNN, DNN)
Module 4 [9L]: Support Vector Machines (Margin, Kernel Methods, Soft-margin)
& Overfitting & Regularisation (Weight Decay, Regularizer choice)
2 Neural Networks 4
2.1 Neural Network Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.2 The Artificial Neuron . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.3 Activation Functions . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.4 Network Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Backpropagation Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 The Chain Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.2 Backpropagation Steps . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.3 Practical Considerations in Training . . . . . . . . . . . . . . . . 7
2.3 Introduction to Radial Basis Function (RBF) Networks . . . . . . . . . . 7
2.4 Recurrent Neural Networks (RNN) . . . . . . . . . . . . . . . . . . . . . 8
2.4.1 RNN Computation . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4.2 LSTM and GRU . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 Convolutional Neural Networks (CNN) . . . . . . . . . . . . . . . . . . . 9
2.5.1 Key CNN Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5.2 Typical CNN Architecture . . . . . . . . . . . . . . . . . . . . . . 9
2.6 Deep Neural Networks (DNN) . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6.1 Why Depth Helps . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6.2 Challenges of Deep Learning . . . . . . . . . . . . . . . . . . . . . 10
2.6.3 Key Deep Learning Components Summary . . . . . . . . . . . . . 10
MODULE 4 11
1
Machine Learning — Modules 3 & 4 3rd Year [Link]
4 Overfitting 14
4.1 What is Overfitting? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 Causes of Overfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3 Dealing with Overfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3.1 Early Stopping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3.2 Cross-Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Regularisation 16
5.1 Informal View of Regularisation . . . . . . . . . . . . . . . . . . . . . . . 16
5.2 Formal Regularisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.2.1 Bias-Variance View . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.3 Weight Decay (L2 Regularisation / Ridge) . . . . . . . . . . . . . . . . . 16
5.4 L1 Regularisation (Lasso) . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.5 Choosing a Regulariser . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.5.1 L1 vs. L2 Comparison . . . . . . . . . . . . . . . . . . . . . . . . 18
5.5.2 Elastic Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.5.3 Choosing λ: Cross-Validation . . . . . . . . . . . . . . . . . . . . 18
5.5.4 Other Regularisation Strategies . . . . . . . . . . . . . . . . . . . 18
2
Machine Learning — Modules 3 & 4 3rd Year [Link]
0 MODULE 3 [9 Lectures]
1 The Linear Model II
1.1 Logistic Regression
1.1.1 Motivation: Beyond Hard Classification
The Perceptron outputs a hard {+1, −1} decision. But in many real problems we want
a probability: “What is the probability that this customer will default?”
P (y = +1 | x) = h(x) = θ(wT x)
P (y = −1 | x) = 1 − h(x) = θ(−wT x)
P (y | x) = θ(y · wT x)
3
Machine Learning — Modules 3 & 4 3rd Year [Link]
MLE: Find w that maximises L(w), i.e., makes the observed data most probable.
N N
1 X −yn wT xn
1 X
Ein (w) = ln 1 + e = ℓ(yn , h(xn ))
N n=1 N n=1
Why cross-entropy?
It is a convex function of w — no local minima, gradient descent is guaranteed to
find the global minimum.
Penalises confident wrong predictions very heavily.
Preferred over MSE for classification because it has better gradient behaviour.
Linear Regression Logistic Regression
Output Real number wT x Probability θ(wT x) ∈ (0, 1)
Loss function Mean Squared Error Cross-entropy (log-loss)
Task Regression Binary classification
Solution Closed-form (X† y) Iterative (gradient descent)
Decision boundary N/A wT x = 0
4
Machine Learning — Modules 3 & 4 3rd Year [Link]
Stochastic Gradient Descent (SGD): Update using one random sample (xn , yn ):
w ← w + η · yn xn · θ(−yn wT xn )
2 Neural Networks
2.1 Neural Network Model
2.1.1 Motivation
Linear models (perceptron, logistic regression) can only learn linear decision boundaries.
Real-world data is often highly nonlinear. Neural Networks stack multiple layers of
linear + nonlinear transformations to learn arbitrarily complex functions.
5
Machine Learning — Modules 3 & 4 3rd Year [Link]
d
!
X
z=ϕ wi xi = ϕ(wT x)
i=0
where ϕ is the activation function and w0 is the bias term (with x0 = 1).
Notation: An L-layer network has L − 1 hidden layers and 1 output layer. Let n[l]
denote the number of neurons in layer l.
6
Machine Learning — Modules 3 & 4 3rd Year [Link]
∂E ∂E ∂z[l]
= ·
∂W[l] ∂z[l] ∂W[l]
Backprop propagates the error signal (δ) backward through the network, layer by layer.
4. Compute Gradients:
∂E ∂E
= δ [l] (a[l−1] )T , = δ [l]
∂W[l] ∂b[l]
7
Machine Learning — Modules 3 & 4 3rd Year [Link]
Architecture:
1. Input layer: Receives input x.
2. Hidden layer: Each neuron k computes ϕk (x) — a Gaussian centred at µk with
spread σk . The neuron activates strongly when x is close to its centre.
3. Output layer: A linear combination of the RBF activations:
K
X
ŷ = wk ϕk (x) + w0
k=1
Training:
Centres µk : Chosen by K-means clustering on the training data, or randomly from
training examples.
Widths σk : Typically set heuristically (e.g., mean distance between centres).
8
Machine Learning — Modules 3 & 4 3rd Year [Link]
ht = ϕ(Wh ht−1 + Wx xt + b)
ŷt = Wy ht + by
Weights Wh , Wx , Wy are shared across all time steps.
Training: Backpropagation Through Time (BPTT) — unroll the RNN across T time
steps and apply standard backprop on the unrolled graph.
Problem: Vanilla RNNs suffer from the vanishing/exploding gradient problem
over long sequences.
Applications of RNNs:
Language modelling and text generation
Machine translation (seq2seq)
Speech recognition
Time series prediction (stock prices, weather)
Video captioning
9
Machine Learning — Modules 3 & 4 3rd Year [Link]
Properties of convolution:
Local connectivity: Each neuron connects to only a small local region (receptive
field).
Parameter sharing: The same filter weights are used across all positions — dra-
matically reduces parameters.
Translation equivariance: If the input shifts, the feature map shifts correspond-
ingly.
(b) Pooling Layer: Reduces spatial dimensions (downsampling), providing spatial in-
variance:
Max pooling: Takes the maximum value in each pooling window. Most common.
Average pooling: Takes the average value.
(c) Fully Connected (FC) Layer: After several conv+pool layers, the feature maps
are flattened and fed into one or more FC layers for final classification.
Famous CNNs:
LeNet-5 (1998): 5 layers; pioneered CNNs for digit recognition.
AlexNet (2012): Deep CNN that won ImageNet; introduced ReLU, dropout.
VGG (2014): Very deep (16–19 layers) with small 3 × 3 filters.
10
Machine Learning — Modules 3 & 4 3rd Year [Link]
11
Machine Learning — Modules 3 & 4 3rd Year [Link]
2 MODULE 4 [9 Lectures]
3 Support Vector Machines (SVM)
3.1 The Margin
For a linearly separable dataset, there are infinitely many hyperplanes that cor-
rectly classify all training data. An SVM finds the optimal separating hyper-
plane — the one that maximises the margin: the perpendicular distance between
the hyperplane and the nearest data points of each class.
|wT xn + b| yn (wT xn + b)
distance = =
∥w∥ ∥w∥
(The second equality uses the fact that yn and wT xn + b have the same sign for
correctly classified points.)
Margin definition: The margin ρ is twice the distance from the hyperplane to the
nearest point (the support vectors):
2
ρ=
∥w∥
(after normalising so that yn (wT xn + b) ≥ 1 for all training points, with equality for
support vectors.)
Intuition: A larger margin means the classifier is more confident and less sensitive to
noise in the training data. The SVM maximises ρ = maximises generalisation.
subject to yn (wT xn + b) ≥ 1, n = 1, . . . , N
Maximising ρ = 2/∥w∥ is equivalent to minimising 12 ∥w∥2 .
12
Machine Learning — Modules 3 & 4 3rd Year [Link]
Lagrangian:
N
1 X
L(w, b, α) = ∥w∥2 − αn yn (wT xn + b) − 1
2 n=1
N
X
subject to αn ≥ 0, αn y n = 0
n=1
N
X
∗
w = αn yn xn
n=1
Key insight: The optimal hyperplane is completely determined by the support vectors
alone. All other training points (with αn = 0) can be removed without changing the
solution. This is why SVMs are called sparse classifiers.
Why SVMs generalise well: The VC bound says Eout ≤ Ein + O(dV C /N ). SVMs
don’t just minimise Ein ; they simultaneously minimise model complexity by
maximising the margin. Larger margin ⇒ fewer support vectors ⇒ lower effective
dV C ⇒ better generalisation. This is Structural Risk Minimisation (SRM).
13
Machine Learning — Modules 3 & 4 3rd Year [Link]
Note that xTn xm has been replaced by Φ(xn )T Φ(xm ) — the inner product in Z-space.
N
X 1X
max αn − αn αm yn ym K(xn , xm )
α
n=1
2 n,m
N
!
X
h(x) = sign αn yn K(xn , x) + b
n=1
14
Machine Learning — Modules 3 & 4 3rd Year [Link]
Soft-Margin Primal:
N
1 2
X
min ∥w∥ + C ξn
w,b,ξ 2
n=1
subject to yn (wT xn + b) ≥ 1 − ξn , ξn ≥ 0
Meaning of ξn :
ξn = 0: point correctly classified and outside the margin.
0 < ξn < 1: point inside the margin but correctly classified.
ξn = 1: point exactly on the decision boundary.
ξn > 1: point misclassified.
The hyperparameter C:
Large C: Small tolerance for violations; near hard-margin; may overfit.
Small C: Large tolerance; wider margin; more regularisation; may underfit.
C is chosen by cross-validation.
Hard-Margin SVM Soft-Margin SVM
Data requirement Linearly separable Any (allows violations)
Slack variables No Yes (ξn )
Hyperparameter None C (controls trade-off)
Robustness to noise Low (outliers ruin solution) High (tolerates outliers)
4 Overfitting
4.1 What is Overfitting?
Overfitting occurs when a model learns the training data too well, including its
noise and random fluctuations, and performs worse on new (unseen) data than
a simpler model would.
Overfitting ⇐⇒ Ein (g) < Ein (h) but Eout (g) > Eout (h)
Signs of overfitting:
Very low training error but high test/validation error.
Large gap between Ein and Eout .
Validation error increases while training error continues to decrease.
15
Machine Learning — Modules 3 & 4 3rd Year [Link]
4.3.2 Cross-Validation
k-Fold Cross-Validation: Split the training data into k equal folds. Train on k − 1
folds and validate on the remaining fold. Repeat k times. The final performance
estimate is the average validation error across all k folds.
16
Machine Learning — Modules 3 & 4 3rd Year [Link]
5 Regularisation
5.1 Informal View of Regularisation
Regularisation is any technique that reduces overfitting by constraining, penal-
ising, or adding information to the model, at the cost of slightly increased bias. It
discourages the model from memorising the training data.
Informal principle: Among all hypotheses that fit the data roughly equally well, prefer
the simpler one (Occam’s Razor). Regularisation implements this preference mathemat-
ically by adding a complexity penalty to the loss.
λ
Ereg (w) = Ein (w) + Ω(w)
N
where Ω(w) is the regulariser (penalty term) and λ > 0 is the regularisation
parameter controlling the trade-off.
Effect: The penalty Ω(w) discourages large or complex weight vectors. The model is
constrained to a simpler region of hypothesis space.
λ
Ereg (w) = Ein (w) + ∥w∥2
N
17
Machine Learning — Modules 3 & 4 3rd Year [Link]
2ηλ 2ηλ
w ← w − η∇Ein (w) − w =w 1− − η∇Ein (w)
N N
Each weight is “decayed” (shrunk) at every step — hence the name weight decay.
−1 T
w∗ = XT X + λI X y
The λI term makes the matrix invertible even when XT X is singular.
Properties of L2 regularisation:
Shrinks all weights toward zero, but never exactly to zero.
Differentiable everywhere; easy to optimise.
Prefers many small weights over few large ones.
Equivalent to placing a Gaussian prior on weights in a Bayesian framework.
λ
Ereg (w) = Ein (w) + ∥w∥1
N
Properties of L1 regularisation:
Produces sparse weight vectors: many weights become exactly zero.
Acts as automatic feature selection: irrelevant features have weight set to 0.
Not differentiable at zero; requires subgradient or coordinate descent.
Equivalent to placing a Laplace prior on weights.
18
Machine Learning — Modules 3 & 4 3rd Year [Link]
19
Machine Learning — Modules 3 & 4 3rd Year [Link]
End of Machine Learning Notes (Modules 3 & 4) — Good luck in your examinations!
20