Deep Learning · Multilayer perceptron
Adding a hidden layer with a non-linear activation turns the perceptron into an MLP that can solve XOR; a forward pass written in NumPy.
A multilayer perceptron (MLP) stacks layers:
The activation φ (ReLU, tanh, sigmoid) is essential. Without it, is just another linear map, and stacking layers buys nothing.
To show the hidden layer solves XOR, I set the weights by hand: one hidden unit acts as OR, another as AND, and the output computes “OR and not AND”. Training them automatically comes next, with backpropagation.
import numpy as np
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
step = lambda z: (z > 0).astype(int)
W1 = np.array([[1, 1], # hidden unit 1 ~ OR
[1, 1]]) # hidden unit 2 ~ AND
b1 = np.array([-0.5, -1.5])
W2 = np.array([1, -2]) # OR minus 2*AND
b2 = -0.5
H = step(X @ W1.T + b1)
out = step(H @ W2 + b2)
print("hidden activations:\n", H)
print("XOR output:", out)
# without the non-linearity, two layers are one linear map
A, B = np.random.default_rng(0).normal(size=(3, 2)), np.random.default_rng(1).normal(size=(1, 3))
x = np.array([0.3, -1.2])
print("B(Ax) == (BA)x:", np.allclose(B @ (A @ x), (B @ A) @ x))
Output:
hidden activations:
[[0 0]
[1 0]
[1 0]
[1 1]]
XOR output: [0 1 1 0]
B(Ax) == (BA)x: True#deep-learning#neural-networks
Sources
- Dive into Deep Learning: Multilayer Perceptrons — d2l.ai
- CS231n: Neural Networks Part 1 — Stanford CS231n
- Deep Learning (Goodfellow, Bengio, Courville), ch. 6 — MIT Press