Neural Networks
Perceptron, MLP, activation functions, forward and backward propagation, and network size.
The last deck in scope, and the source of the "describe this network and calculate its size" question — slide 32 gives the counting convention explicitly.
Why non-linearity
A linear classifier predicts $y = wx + b$, a linear combination of the inputs. Many predictions are better made with non-linear functions of the input, and the canonical demonstration is that a linear classifier cannot separate XOR.

Perceptron
> First introduced by Frank Rosenblatt, 1957.

How it works, in the deck's four steps:
- Randomly initialise the weights
- Take one sample $x_i$ and predict $y_i$
- Update the weights for mistaken predictions
- Repeat until convergence (no errors)
That is the perceptron learning rule, and it converges only if the data is linearly separable — the limitation Minsky and Papert highlighted in 1969.
From linear model to neuron
The improvement is the activation function: something smoother than a hard threshold, applied to the linear score.
x0 ──w0──┐
x1 ──w1──┤
x2 ──w2──┼──► f ──► g(f) ──► y [0,1]
x3 ──w3──┘

Each neuron performs three operations:
- a dot product of the input with its weights ($w x$)
- adds the bias ($wx + b$)
- applies the non-linearity, i.e. the activation function
The activation function is also called the squashing function — that name is a hint about what it does to an unbounded score.

Biological motivation: the brain has ~$10^{11}$ neurons, each connected to ~$10^4$ others, and the neuron is the basic computational unit.
Activation functions — the properties and the formulas
Four desired properties: non-linear, easy to compute, differentiable, bounded output.

| Function | Formula | Range | Notes |
|---|---|---|---|
| Sigmoid | $\sigma(z)=\dfrac{1}{1+e^{-z}}$ | $(0,1)$ | smooth step; saturates → vanishing gradient |
| Tanh | $\tanh(z)=\dfrac{e^{z}-e^{-z}}{e^{z}+e^{-z}}$ | $(-1,1)$ | zero-centred version of sigmoid |
| ReLU | $\max(0,z)$ | $[0,\infty)$ | cheap, no saturation for $z>0$; dies for $z<0$ |
| Leaky ReLU | $\max(\alpha z, z)$, small $\alpha$ | $(-\infty,\infty)$ | fixes the dying-ReLU problem |
| Softmax | $\dfrac{e^{z_i}}{\sum_j e^{z_j}}$ | $(0,1)$, sums to 1 | output layer for multiclass |




"What is an activation function? Explain its role." It is a non-linear, differentiable, bounded function applied to the weighted sum inside each neuron, $a = g(w^Tx+b)$. Its role is to give the network non-linear representational power: without it, a stack of linear layers collapses to a single linear map, $W_2(W_1x) = (W_2W_1)x$, so no depth would buy anything. Beyond that it bounds and shapes the output (sigmoid/softmax turn scores into probabilities), and its derivative is what backpropagation multiplies through the chain rule — which is why saturating functions such as sigmoid produce vanishing gradients and ReLU became the default in hidden layers.
The deck asks the "without activation function" question explicitly:

Answer: $W_2W_1$ is just another matrix, so the 64→20→10 network becomes a single linear map 64→10 and loses all benefit of the hidden layer.
MLP
> A single perceptron makes one decision. For multiple decisions you stack multiple outputs to match the possible outcomes. An MLP feeds one layer as input to the next.

$$f(x) = f_2\big(f_1(x)\big) = W_2\max(0,\,W_1x)$$

Three facts that get examined:
- Neurons are connected in an acyclic graph (feed-forward).
- Logistic regression and SVMs are special cases of single-layer networks.
- MLPs are also called Artificial Neural Networks.

Counting — the convention, and the worked example
The input layer does not count. "A 2-layer neural network" means 1 hidden layer + 1 output layer. Write that down before you count anything.

The deck's example: a 2-layer network with 4 hidden units and 2 output units, fed by 3 inputs.
Neurons: $4 + 2 = 6$
Parameters: weights $+$ biases, layer by layer:
$$[3\times 4] + [4\times 2] + \underbrace{[4 + 2]}_{\text{biases}} = 12 + 8 + 6 = 26$$

General rule for layers $n_0 \to n_1 \to \dots \to n_L$:
$$\#\text{params} = \sum_{\ell=1}^{L}\big(n_{\ell-1}n_\ell + n_\ell\big) = \sum_{\ell=1}^{L} n_\ell\,(n_{\ell-1}+1)$$
"Describe the following neural network. Calculate the size of the network." The practice sheet's diagram is 3 inputs → 4 hidden → 4 hidden → 1 output. Description: a fully-connected feed-forward MLP with one input layer of 3 units, two hidden layers of 4 units each, and a single output unit (suitable for binary classification with a sigmoid, or regression with a linear output). Every neuron in a layer connects to every neuron in the next; information flows left to right with no cycles. Depth: 3 layers by the course convention (input layer not counted) — or "2 hidden layers". Neurons: $4+4+1 = 9$. Parameters: $$(3\times4+4) + (4\times4+4) + (4\times1+1) = 16 + 20 + 5 = \mathbf{41}$$ Always show the three terms separately; that is where the marks are. The bias term $+n_\ell$ is the one everybody forgets.
Representational power
> Can represent any function with at least one hidden layer — the universal approximator property.

Depth is what makes this practical: the slide sequence shows 5 hidden neurons giving a crude fit, 10 a good one, 50 an essentially exact one. Universal approximation is an existence statement, not a statement about how much data or how many neurons you need — which is the caveat to add if asked to critique it.
The two passes
- Forward pass — compute the output prediction (inference).
- Backward pass — adjust the parameters (learning).

Forward
For the 3→h→g→o network on slide 37: inputs propagate $x \to h \to g \to o$, each edge multiplying by its weight and each node applying its activation. That is all a forward pass is — repeated matrix multiply plus non-linearity.

Backward
> We know the target $y$, so we can calculate the error/loss. The goal is to find the parameters where the error is minimised — but we do not have targets for the hidden layers. That is the difficulty.

Backpropagation, introduced by Rumelhart, Hinton and Williams (1986), solves it by propagating the gradient of the loss backward through the chain rule:
Training loop Repeat until convergence — for each example $n$:
- Forward: given $x^{(n)}$, propagate activity $x^{(n)}\to h^{(n)}\to g^{(n)}\to o^{(n)}$
- Backward: propagate gradients backward through the network
- Update each weight by gradient descent

Why it matters: it is efficient for large numbers of examples and complex architectures, and the deck calls it "the backbone of modern neural networks".
"Explain forward and backward propagation." Forward: compute $z^{(\ell)} = W^{(\ell)}a^{(\ell-1)}+b^{(\ell)}$ and $a^{(\ell)}=g(z^{(\ell)})$ layer by layer, ending in the loss $L(a^{(L)},y)$. Backward: compute the error at the output, $\delta^{(L)} = \nabla_a L \odot g'(z^{(L)})$, then propagate it backwards with $\delta^{(\ell)} = \big(W^{(\ell+1)T}\delta^{(\ell+1)}\big)\odot g'(z^{(\ell)})$, and read off the gradients $\partial L/\partial W^{(\ell)} = \delta^{(\ell)}a^{(\ell-1)T}$ and $\partial L/\partial b^{(\ell)}=\delta^{(\ell)}$. Then update $W \leftarrow W - \alpha\,\partial L/\partial W$. The crucial insight is that the chain rule lets the same backward sweep reuse intermediate quantities, so the cost of computing all gradients is the same order as one forward pass — which is what makes training deep networks feasible at all.





























