Multi-Layer Perceptrons (MLPs), Hidden Representations & Solving XOR
Why stacking linear layers without activations collapses to 1 layer, how a 2-neuron hidden layer folds 2D space to conquer XOR, and why Deep beats Wide.
The XOR crisis literally triggered the first AI Winter in 1969 — and understanding how hidden layers fold space unlocks ALL of Deep Learning!
Every Transformer block spends ~66% of its parameters inside a 2-layer MLP (Feed-Forward Network)! Knowing how hidden representations warp feature space explains why networks memorize or generalize.
When linear models plateau on complex interactions (e.g., fraud rings or non-linear pricing curves), adding hidden layers lets the network learn its own nonlinear features automatically.
Guaranteed fundamentals interview question: 'What happens if you remove all activation functions from a 10-layer neural network, and why does a 2-layer MLP solve XOR when a single Perceptron cannot?'
You build a 5-layer network x → Linear → Linear → Linear → Linear → Linear → y with NO activation functions (no ReLU/Sigmoid/Tanh) between layers. Can it learn to separate a circular ring or XOR?
- Prove algebraically why stacking 100 Linear layers without a non-linearity collapses into a single Linear layer
- Solve the classic XOR problem by hand with a 2-hidden-neuron ReLU MLP and visualize how it folds 2D space
- Explain the Universal Approximation Theorem — and why Depth is exponentially more parameter-efficient than shallow Width
- Build and trace a clean multi-layer
nn.Sequential/nn.ModuleMLP in PyTorch
Imagine drawing blue dots at opposite corners (0,0) and (1,1) of a piece of paper, and gold dots at (0,1) and (1,0). Try separating blue from gold with one straight ruler — impossible! Now fold the paper diagonally so (0,1) and (1,0) land right on top of each other, away from (0,0) and (1,1). Now ONE straight snip of scissors separates them! That fold IS what a hidden layer + ReLU does to space!
1. The Linear Collapse Proof: Why Activations Are Non-Negotiable
Before we fold space, let's prove why raw stacked linear layers are a total trap. Toggle ReLU OFF vs. ON in the mini-lab below and watch two stacked layers snap from a boring straight line into a bent mountain!
Exact 2-Line Proof of Linear Collapse
Suppose Layer 1 computes and Layer 2 computes with no activation in between: Even with 100 layers and 100 million parameters, the model can STILL only draw flat straight hyperplanes! Inserting a non-linear activation breaks associativity and unlocks curved, folded decision boundaries.
Toggle ReLU Between Two Layers: Straight Line vs. Tent Curve!
With ReLU OFF, 1.2(x + 1) − 2.2(x − 0.5) simplifies algebraically to −1.0x + 2.3 — just ONE boring straight line!
2. Solving XOR by Hand: How Hidden Neurons Invent New Coordinates
In 1969, Minsky & Papert proved a single Perceptron cannot solve XOR. The cure? Give the network 1 hidden layer with 2 ReLU neurons! Each hidden neuron creates a folded feature (h₁ = ReLU(x₁ + x₂) and h₂ = ReLU(x₁ + x₂ − 1)), mapping the 4 tangled corners into a new space where 1 straight line separates them cleanly!
The Exact 2-Neuron ReLU XOR Solution
Let , , , :
Hand-Verify Every Single XOR Corner Through Our 2-Layer MLP
Let's plug all 4 XOR inputs (x₁, x₂) into h₁ = ReLU(x₁+x₂), h₂ = ReLU(x₁+x₂−1), and ŷ = h₁ − 2h₂!
h₁ = ReLU(0+0) = 0, h₂ = ReLU(0+0−1) = ReLU(−1) = 0. Output: ŷ = 0 − 2(0) = 0 ✓!
In our XOR MLP above, what is the role of the second hidden neuron h₂ = ReLU(x₁ + x₂ − 1)?
3. Universal Approximation Theorem: Why Depth Crushes Shallow Width
Cybenko (1989) and Hornik (1991) proved the Universal Approximation Theorem (UAT): a neural network with just ONE single hidden layer (and a non-linear activation) can approximate ANY continuous function on a bounded domain to arbitrary precision — if you make it wide enough. So why do we build Deep networks instead of 1 giant wide layer?
The Exponential Power of Depth (Compositional Hierarchy)
- A shallow 1-hidden-layer ReLU net with neurons creates at most linear fold regions.
- A deep ReLU net of depth and width composes folds on top of folds — creating up to linear regions!
- Everyday intuition: A deep vision network learns edges (Layer 1) → eyes/ears (Layer 2) → cat faces (Layer 3), reusing simple parts across thousands of objects instead of memorizing every pixel combination separately!
Why does the Universal Approximation Theorem NOT guarantee that a 1-hidden-layer MLP will achieve 99% test accuracy on ImageNet?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Simply because we directly observe the input layer x (our data) and the output layer ŷ (our prediction compared against label y), whereas the intermediate layers live inside the network and invent their own internal features automatically!
How a 2-Neuron Hidden Layer Folds Space to Solve XOR
Watch the entire 2D grid stretch, shift, and fold under ReLU until 1 green line separates Blue from Gold!
Transformer FFN Sub-Layers
Every GPT/Llama block contains a massive 2-layer MLP (d → 4d → d) that stores factual knowledge.
Tabular & RecSys Ranking Towers
Two-Tower recommendation networks pass user & item embeddings through 3-layer MLPs to predict click affinity.
Neural Radiance Fields (NeRF)
A pure 8-layer MLP maps 3D coordinates (x, y, z) to photorealistic RGB color & density!
Policy & Value Networks in RL
PPO and DQN agents use compact MLPs to map game/robot state vectors to optimal actions.
import torch
import torch.nn as nn
class SimpleMLP(nn.Module):
def __init__(self, d_in: int, d_hidden: int, d_out: int):
super().__init__()
self.net = nn.Sequential(
nn.Linear(d_in, d_hidden), # (B, d_in) -> (B, d_hidden)
nn.ReLU(), # bends & folds hidden space!
nn.Linear(d_hidden, d_hidden),
nn.ReLU(),
nn.Linear(d_hidden, d_out), # (B, d_hidden) -> (B, d_out) raw logits
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.net(x)
# Test our 2-hidden-neuron XOR MLP!
model = SimpleMLP(d_in=2, d_hidden=8, d_out=1)
X_xor = torch.tensor([[0., 0.], [0., 1.], [1., 0.], [1., 1.]]) # (4, 2)
logits = model(X_xor) # (4, 1)
print("Output shape:", logits.shape)
nn.CrossEntropyLoss or nn.MSELoss.nn.Linear layers back-to-back by accident.ReLU/GELU/SiLU) separates consecutive Linear layers (except intentional low-rank bottlenecks like LoRA!).nn.ReLU() on the final output of a house-price residual or raw-logit classifier?