CS 460G Midterm Hub / Lectures / Week 8 Lecture 3
Week 8 · Lecture 3

Neural Networks

Perceptron, MLP, activation functions, forward and backward propagation, and network size.

mathguaranteed Q

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.

A linear classifier failing on the two-class problem
A linear classifier failing on the two-class problem · slide 5

Perceptron

> First introduced by Frank Rosenblatt, 1957.

The perceptron — inputs, weights, output between 0 and 1
The perceptron — inputs, weights, output between 0 and 1 · slide 6

How it works, in the deck's four steps:

  1. Randomly initialise the weights
  2. Take one sample $x_i$ and predict $y_i$
  3. Update the weights for mistaken predictions
  4. 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──┘
Adding a smooth activation to the linear unit
Adding a smooth activation to the linear unit · slide 7

Each neuron performs three operations:

  1. a dot product of the input with its weights ($w x$)
  2. adds the bias ($wx + b$)
  3. 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.

The three operations of a neuron
The three operations of a neuron · slide 11

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.

Four desired properties of an activation function
Four desired properties of an activation function · slide 12
FunctionFormulaRangeNotes
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 1output layer for multiclass
Sigmoid
Sigmoid · slide 13
Tanh
Tanh · slide 14
ReLU
ReLU · slide 15
Leaky ReLU
Leaky ReLU · slide 16

"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:

What if we build this network without an activation function?
What if we build this network without an activation function? · slide 28

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.

MLP — input, intermediate, output layers
MLP — input, intermediate, output layers · slide 23

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

A 2-layer network written out
A 2-layer network written out · slide 24

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.
Topology facts
Topology facts · slide 29

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 counting rule with a worked example
The counting rule with a worked example · slide 32

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$$

A 2-layer network — 1 hidden layer of 4, output of 2
A 2-layer network — 1 hidden layer of 4, output of 2 · slide 31

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.

5, 10 and 50 hidden neurons approximating a function
5, 10 and 50 hidden neurons approximating a function · slide 34

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 pass for inference, backward pass for learning
Forward pass for inference, backward pass for learning · slide 35

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.

Forward pass through a 3-3-2-1 network
Forward pass through a 3-3-2-1 network · slide 39

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.

The credit-assignment problem for hidden layers
The credit-assignment problem for hidden layers · slide 42

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$:

  1. Forward: given $x^{(n)}$, propagate activity $x^{(n)}\to h^{(n)}\to g^{(n)}\to o^{(n)}$
  2. Backward: propagate gradients backward through the network
  3. Update each weight by gradient descent
The backpropagation training loop
The backpropagation training loop · slide 44

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.