Multilayer Perceptron (MLP)
Prerequisites
- Perceptron — Single neuron model, XOR limitation
🎯 Learning Objectives
- Describe the architecture of an MLP: input, hidden, and output layers
- Derive the forward propagation equations using weight matrices
- State the Universal Approximation Theorem and its implications
- Solve XOR using a 2-layer network with specific weights
- Count total parameters for any fully-connected architecture
1. MLP Architecture
A Multilayer Perceptron (MLP) is a feedforward neural network with one or more hidden layers between the input and output layers. Every neuron in one layer is connected to every neuron in the next layer — hence the name fully connected (or dense) network.
Architecture Notation
An MLP with architecture [n0, n1, n2, …, nL] has:
- n0 = number of input features (input layer)
- n1, n2, …, nL−1 = number of neurons in each hidden layer
- nL = number of output neurons
- L = total number of layers with learnable weights (hidden + output)
Example: [3, 4, 2, 1] means 3 inputs, two hidden layers (4 and 2 neurons), 1 output.
Why “Hidden”?
Hidden layers are called “hidden” because their activations are not directly observed in the training data. Only the input features and the target output are known; the network must learn what intermediate representations to compute.
2. Forward Propagation Mathematics
During forward propagation, data flows from input to output through successive matrix multiplications and non-linear activations.
Forward Pass Equations
For layer l (l = 1, 2, …, L):
z[l] = W[l] a[l−1] + b[l]
a[l] = fl(z[l])
where:
- a[0] = x (the input vector)
- W[l] is the weight matrix of shape (nl × nl−1)
- b[l] is the bias vector of shape (nl × 1)
- fl is the activation function for layer l
- z[l] is the pre-activation (linear combination)
- a[l] is the post-activation output
Example: Forward Pass for [2, 3, 1]
Input: x = (x1, x2)T
Hidden layer (3 neurons, sigmoid activation):
z[1] = W[1]x + b[1] (3×1 vector)
a[1] = σ(z[1]) (element-wise sigmoid)
Output layer (1 neuron, sigmoid):
z[2] = W[2]a[1] + b[2] (scalar)
a[2] = σ(z[2]) (output probability)
Matrix dimensions: W[1] is 3×2, b[1] is 3×1, W[2] is 1×3, b[2] is 1×1.
3. Solving XOR with an MLP
The XOR function requires at least one hidden layer. Here is an explicit solution using a [2, 2, 1] network.
XOR Network: Weights & Architecture
Architecture: 2 inputs → 2 hidden neurons (step activation) → 1 output (step activation).
Hidden layer weights:
W[1] = [[1, 1], b[1] = [−0.5]
[1, 1]] [−1.5]
Output layer weights:
W[2] = [1, −1] b[2] = [−0.5]
Verification (using step function: output 1 if z ≥ 0, else 0):
| x1 | x2 | h1=step(x1+x2−0.5) | h2=step(x1+x2−1.5) | y=step(h1−h2−0.5) |
|---|---|---|---|---|
| 0 | 0 | step(−0.5)=0 | step(−1.5)=0 | step(−0.5)=0 |
| 0 | 1 | step(0.5)=1 | step(−0.5)=0 | step(0.5)=1 |
| 1 | 0 | step(0.5)=1 | step(−0.5)=0 | step(0.5)=1 |
| 1 | 1 | step(1.5)=1 | step(0.5)=1 | step(−0.5)=0 |
Output matches XOR truth table perfectly. The hidden layer decomposes XOR into two linearly separable sub-problems: h1 computes OR, h2 computes AND, and the output computes (OR) AND (NOT AND) = XOR.
4. Parameter Counting
Parameter counting is a very common GATE question type for neural networks.
Parameter Count Formula
For a fully-connected layer connecting nin inputs to nout outputs:
Parameters = nin × nout + nout (weights + biases)
For the entire network [n0, n1, …, nL]:
Total = ∑l=1L (nl−1 × nl + nl)
Example 1: Architecture [784, 256, 128, 10]
This is a typical digit classification network (MNIST).
- Layer 1: 784 × 256 + 256 = 200,704 + 256 = 200,960
- Layer 2: 256 × 128 + 128 = 32,768 + 128 = 32,896
- Layer 3: 128 × 10 + 10 = 1,280 + 10 = 1,290
- Total: 200,960 + 32,896 + 1,290 = 235,146 parameters
Example 2: Architecture [5, 10, 10, 3]
- Layer 1: 5 × 10 + 10 = 60
- Layer 2: 10 × 10 + 10 = 110
- Layer 3: 10 × 3 + 3 = 33
- Total: 60 + 110 + 33 = 203 parameters
GATE NAT Example
Q: A fully connected neural network has 3 input neurons, two hidden layers with 4 and 5 neurons respectively, and 2 output neurons. All layers include bias. How many trainable parameters does the network have?
A: Architecture is [3, 4, 5, 2].
- Layer 1: 3×4 + 4 = 16
- Layer 2: 4×5 + 5 = 25
- Layer 3: 5×2 + 2 = 12
- Total: 16 + 25 + 12 = 53
5. Network Capacity & Design Choices
The capacity (or expressiveness) of a neural network refers to the set of functions it can represent. Key design decisions:
| Design Choice | Effect |
|---|---|
| More neurons per layer (wider) | Can represent more complex functions within a single layer; risk of overfitting |
| More hidden layers (deeper) | Hierarchical feature learning; exponentially more efficient than wide shallow networks |
| Fewer parameters | Simpler model; may underfit |
| Activation function choice | Determines what non-linearities the network can express |
Practical Guidelines
- Start with 1–2 hidden layers for most problems.
- Common hidden layer sizes: powers of 2 (32, 64, 128, 256, 512).
- The output layer size matches the task: 1 for regression/binary classification, K for K-class classification.
- For classification: use softmax activation in the output layer.
- For regression: use linear (identity) activation in the output layer.
6. Complete Forward Pass Numerical Example
Problem
Architecture [2, 2, 1] with sigmoid activation. Given:
W[1] = [[0.5, 0.3], b[1] = [[0.1],
[0.2, 0.7]] [0.2]]
W[2] = [[0.6, 0.4]] b[2] = [[0.3]]
Input: x = [[1.0], [0.5]]
Compute the network output.
Solution
- Step 1 — Hidden layer pre-activation:
z[1] = W[1]x + b[1] z1 = 0.5(1.0) + 0.3(0.5) + 0.1 = 0.5 + 0.15 + 0.1 = 0.75 z2 = 0.2(1.0) + 0.7(0.5) + 0.2 = 0.2 + 0.35 + 0.2 = 0.75 - Step 2 — Hidden layer activation (sigmoid):
a1 = σ(0.75) = 1/(1+e−0.75) ≈ 0.679 a2 = σ(0.75) ≈ 0.679 - Step 3 — Output pre-activation:
z[2] = 0.6(0.679) + 0.4(0.679) + 0.3 = 0.407 + 0.272 + 0.3 = 0.979 - Step 4 — Output activation:
a[2] = σ(0.979) ≈ 0.727
Final output: ≈ 0.727 (can be interpreted as probability of class 1 in binary classification).
Summary
- An MLP has input, hidden, and output layers connected by weight matrices and biases.
- Forward propagation: z[l] = W[l]a[l−1] + b[l], then a[l] = f(z[l]).
- XOR can be solved with a [2, 2, 1] MLP using step activations.
- Parameter counting: per layer = nin × nout + nout; sum over all layers for total.
- The Universal Approximation Theorem says one hidden layer suffices in theory, but deep networks are more efficient in practice.
- For GATE: master parameter counting and forward pass computation.