Math Foundations — Lecture 3
Affine transforms, activations, chain rule, SGD, regularisation, Elman RNN, gradient problems.
Affine transformation
\(\mathbf{x}\in\mathbb{R}^{d_\text{in}}\), \(W\in\mathbb{R}^{d_\text{in}\times d_\text{out}}\), \(\mathbf{b}\in\mathbb{R}^{d_\text{out}}\). A layer = affine + activation.
Activation functions
Sigmoid
Output in \((0,1)\). Derivative max at \(z=0\) (\(0.25\)); saturates → vanishing grad.
Tanh
Output in \((-1,1)\). Zero-centred (helps optimisation vs sigmoid). Still saturates at extremes.
ReLU
No saturation for positive inputs; sparse activation. "Dead neuron" if always \(z\le 0\).
MLP (multi-layer perceptron)
Two-layer example:
Without \(g\), composition collapses: \(\mathbf{x}W_1 W_2=\mathbf{x}W'\) — still linear. Non-linearity \(g\) is essential (XOR example).
Gradient descent & SGD
\(\eta\) = learning rate. SGD: compute gradient on a random minibatch instead of full dataset; introduces noise that can help escape local minima.
Chain rule & backpropagation
For a composition \(f = f_3 \circ f_2 \circ f_1\), the chain rule gives:
Forward pass: compute and cache intermediate values. Backward pass: propagate gradients from loss to each parameter via cached Jacobians.
Logistic example
Regularisation
\(\ell_2\) (weight decay)
Penalises large weights; equivalent to Gaussian prior. Gradient contribution: \(+2\lambda w_i\).
\(\ell_1\) (sparsity)
Encourages exact zeros → feature selection.
Dropout
At training time, zero each hidden unit independently with probability \(p\). At test time, multiply weights by \((1-p)\) (or scale during training). Does not change the loss function — modifies forward pass only.
Elman RNN
Initialise \(\mathbf{h}_0\) to zeros or learn it as a parameter.
BPTT (backpropagation through time)
Unroll the RNN for \(T\) steps → apply chain rule across all steps. Total gradient for a parameter sums contributions from each time step.
Vanishing & exploding gradients
Gradient of loss w.r.t. \(\mathbf{h}_k\) involves the product:
If \(\|V\|\) and \(\tanh'\) are <1 repeatedly → product shrinks exponentially (vanishing).
If \(\|V\|\) is large → product grows exponentially (exploding).
Fixes
Gradient clipping: cap \(\|\nabla\|\) to a max norm. Gated architectures (LSTM, GRU): additive paths that preserve gradients over long distances — covered in later lectures.