SECTION 01 / 4

Feedforward Neural Networks — Layers, Notation and Parameters

5 min read
On this page 7 headings
BUILD INTUITION WITH NUMBERS

Track shapes through one layer

Input has two features. This layer has two sigmoid neurons. We use column vectors.

  1. Check the dimensions before multiplying.

    x=[12]W=[1002]b=[−11]\begin{aligned}x&=\begin{bmatrix}1\\2\end{bmatrix}\\W&=\begin{bmatrix}1&0\\0&2\end{bmatrix}\\b&=\begin{bmatrix}-1\\1\end{bmatrix}\end{aligned}
  2. Multiply and add the bias. A 2×2 matrix times a 2×1 vector gives a 2×1 vector.

    a=Wx+b=[14]+[−11]=[05]\begin{aligned}a&=Wx+b\\&=\begin{bmatrix}1\\4\end{bmatrix}+\begin{bmatrix}-1\\1\end{bmatrix}\\&=\begin{bmatrix}0\\5\end{bmatrix}\end{aligned}
  3. Apply sigmoid separately to each entry.

    h=[σ(0)σ(5)]≈[0.50.9933]h=\begin{bmatrix}\sigma(0)\\\sigma(5)\end{bmatrix}\approx\begin{bmatrix}0.5\\0.9933\end{bmatrix}

What changed? Each row of W belongs to one neuron. The activation keeps the same vector shape as the pre-activation.

Quick check: If this layer had three neurons and two input features, what shape would W have?

3×2: three rows for neurons, two columns for input features.

Swipe long formulas, tables or code sideways to see the rest.

Start here A feedforward neural network is just many sigmoid-type neurons arranged in layers, where information flows one way: from the input, through hidden layers, to the output. This page fixes the notation (ai,hi,Wi,bi)(a_{i}, h_{i}, W_{i}, b_{i}) that the whole of backpropagation uses — learn it well and the rest becomes much easier.

1. The structure

Information flows upward: x → h₁ → h₂ → ŷ. Each layer has its own weights W and biases b.
Information flows upward: x → h₁ → h₂ → ŷ. Each layer has its own weights W and biases b.
Open image ↗
Input layer, two hidden layers and an output layer; each neuron split into pre-activation and activation
  • Input: an n-dimensional vector x=(x1,…,xn)x = (x_{1}, \ldots , x_{n}). Call it layer 0(h0=x). (h_{0} = x).
  • Hidden layers: L−1L - 1 of them (2 in the picture), each with n neurons.
  • Output layer: k neurons (for example, one per class). Call it layer L (here L=3L = 3).
  • Between layer i−1i - 1 and layer i: a weight matrix WiW_{i} and a bias vector bib_{i}.
Parameter Shape Between
Wi(0<i<L)W_{i} (0 < i < L) n×nn \times n layer i−1i - 1 and hidden layer i
bi(0<i<L)b_{i} (0 < i < L) n
WLW_L k×nk \times n last hidden layer and output
bLb_L k

(One later slide writes ∇WL∈Rn×k\nabla W_L \in \mathbb{R}^{n\times k}; the correct shape, matching WLW_L, is k×nk \times n.)

Everyday example A factory line: raw material (input) passes through station 1, station 2 (hidden layers), and comes out as the finished product (output). Nothing goes backwards while making the product.

2. Each neuron has two parts

a is the “raw score”; h is what the neuron passes on after squashing it.
a is the “raw score”; h is what the neuron passes on after squashing it.
Open image ↗
One neuron: first the weighted sum a, then the activation h = g(a)

Pre-activation (the weighted sum):

ai=bi+Wihi−1a_{i} = b_{i} + W_{i} h_{i-1}

Activation (apply the activation function g):

hi=g(ai)h_{i} = g(a_{i}) g=g = logistic (sigmoid), tanh, linear, …

Output layer uses an output activation function O:

y^=f(x)=hL=O(aL)\hat{y} = f(x) = h_L = O(a_L) O=softmax⁡O = \operatorname{softmax}, linear, …

aia_{i} and hih_{i} are vectors (one number per neuron in the layer). We write aia_{i} instead of ai(x)a_{i}(x) to keep things short.

Lecture example — sizes with numbers Say x has n=3n = 3 values, each hidden layer has 3 neurons, and there are k=2k = 2 outputs. W1:3×3,b1:3→a1=b1+W1xW_{1}: 3 \times 3, b_{1}: 3 \to a_{1} = b_{1} + W_{1}x is a 3-vector, h1=g(a1)h_{1} = g(a_{1}) is a 3-vector. W2:3×3,b2:3→a2,h2W_{2}: 3 \times 3, b_{2}: 3 \to a_{2}, h_{2} are 3-vectors. W3:2×3,b3:2→a3W_{3}: 2 \times 3, b_{3}: 2 \to a_{3} is a 2-vector, y^=O(a3)\hat{y} = O(a_{3}) is a 2-vector. Total parameters =(9+3)+(9+3)+(6+2)== (9 + 3) + (9 + 3) + (6 + 2) = 32.

3. The whole network as one formula

ForL=3For L = 3:

y^i=f(xi)=O(W3g(W2g(W1xi+b1)+b2)+b3)\hat{y}_{i} = f(x_{i}) = O(W_{3} g(W_{2} g(W_{1}x_{i} + b_{1}) + b_{2}) + b_{3})

Read it inside-out: multiply by W1,addb1W_{1}, add b_{1}, apply g → multiply by W2,addb2W_{2}, add b_{2}, apply g → multiply by W3,addb3W_{3}, add b_{3}, apply O.

4. The same five parts as before

Part For a feedforward network
Data {(xi,yi)},i=1…N\{(x_{i}, y_{i})\}, i = 1 \ldots N
Model y^i=O(W3g(W2g(W1x+b1)+b2)+b3)\hat{y}_{i} = O(W_{3} g(W_{2} g(W_{1}x + b_{1}) + b_{2}) + b_{3})
Parameters θ=W1,…,WL,b1,…,bL\theta = W_{1}, \ldots , W_L, b_{1}, \ldots , b_L
Algorithm gradient descent with backpropagation
Loss e.g. (1/N)∑i∑j(y^ij−yij)2(1/N) \sum _{i} \sum _{j} (\hat{y}_{ij} - y_{ij})^{2}, in general L(θ)L(\theta )

5. Learning the parameters — same gradient descent

Gradient descent is unchanged; only θ\theta is bigger:

t ← 0;  max_iterations ← 1000
initialise θ₀ = [W₁, …, W_L, b₁, …, b_L]
while t++ < max_iterations:
    θ_{t+1} ← θ_t − η ∇θ_t

Except now ∇θ\nabla \theta is huge: it contains ∂L/∂\partial L/\partial (every single weight and bias) — ∂L/∂W111,∂L/∂W112,…,∂L/∂WL,nk,∂L/∂b11,…\partial L/\partial W_{111}, \partial L/\partial W_{112}, \ldots , \partial L/\partial W_L,nk, \partial L/\partial b_{11}, \ldots . It is made of:

  • ∇W1,…,∇WL−1∈Rn×n,∇WL∈Rk×n\nabla W_{1}, \ldots , \nabla W_{L-1} \in \mathbb{R}^{n\times n}, \nabla W_L \in \mathbb{R}^{k\times n}
  • ∇b1,…,∇bL−1∈Rn,∇bL∈Rk\nabla b_{1}, \ldots , \nabla b_{L-1} \in \mathbb{R}^{n}, \nabla b_L \in \mathbb{R}^{k}

So we must answer two questions:

  1. How do we choose the loss function L(θ)L(\theta )? → next page.
  2. How do we compute ∇θ\nabla \theta efficiently? → backpropagation (pages 3 and 4).

In one line: A feedforward network computes ai=bi+Wihi−1a_{i} = b_{i} + W_{i}h_{i-1} and hi=g(ai)h_{i} = g(a_{i}) layer by layer, ending with y^=O(aL)\hat{y} = O(a_L); its parameters are all the WiW_{i} and bib_{i}, learned by gradient descent.

Exam words: feedforward neural network, input layer (layer 0), hidden layer, output layer (layer L), pre-activation aia_{i}, activation hih_{i}, activation function g, output function O, weight matrix WiW_{i}, bias bi,θb_{i}, \theta .

Quick check

  1. Input dimension 4, two hidden layers of 5 neurons, 3 outputs. Shapes of W1,W2,W3W_{1}, W_{2}, W_{3}?
W1:5×4,W2:5×5,W3:3×5W_{1}: 5 \times 4, W_{2}: 5 \times 5, W_{3}: 3 \times 5. (The lecture assumes n=n = input size == hidden size for simplicity; in general W1W_{1} is (hidden size) × (input size).)
  1. How many parameters does the network in Q1 have?
W120+b15+W225+b25+W315+b33=73. W_{1} 20 + b_{1} 5 + W_{2} 25 + b_{2} 5 + W_{3} 15 + b_{3} 3 = 73.
  1. W1=[[1,0],[2,−1]],b1=[0,1],x=[1,1]W_{1} = [[1, 0], [2, -1]], b_{1} = [0, 1], x = [1, 1]. Find a1a_{1}.
W1x=[1×1+0×1,2×1+(−1)×1]=[1,1].a1=[1,1]+[0,1]=[1,2]. W_{1}x = [1\times 1 + 0\times 1, 2\times 1 + (-1)\times 1] = [1, 1]. a_{1} = [1, 1] + [0, 1] = [1, 2].
  1. What is the difference between g and O?
g is the activation used in hidden layers (sigmoid, tanh, …). O is the output activation used at the last layer (softmax, linear, …).
  1. What are the two questions we must answer to train the network?
How to choose the loss function L(θ)L(\theta ), and how to compute the gradient ∇θ\nabla \theta (backpropagation).
YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑