§6.1–6.2Example: Learning XOR · Gradient-Based Learning

Part II DL pp. 171–177 · ~4 min read

§6.1 The smallest network that beats a linear model

To make feedforward networks concrete, the book takes the simplest task a linear model cannot do: XOR. The exclusive-or returns 1 when exactly one of x1,x2x_1, x_2 is 1, else 0 — the target y=f(x)y = f^*(\boldsymbol{x}) over the four points {[0,0],[0,1],[1,0],[1,1]}\{[0,0], [0,1], [1,0], [1,1]\}. We train on all four (no generalization here — the only challenge is fitting), with an MSE loss (chosen purely to keep the algebra simple; MSE is a poor choice for binary data in practice — see §6.2.1).

Try the linear model f(x;w,b)=xw+bf(\boldsymbol{x}; \boldsymbol{w}, b) = \boldsymbol{x}^\top\boldsymbol{w} + b — solving the normal equations gives w=0\boldsymbol{w} = \boldsymbol{0}, b=12b = \tfrac12, i.e. 0.5 everywhere:

A linear model on XOR (fig 6.1 left) — no weights reach 100%
(0,0)→0(1,0)→1(0,1)→1(1,1)→0x₁x₂

predict class 1 iff w₁x₁ + w₂x₂ + b > 0

accuracy: 75%

red-ringed points are misclassified; the shaded side of the line is "predict 1"

Stuck at 75%? The output must INCREASE with x₂ when x₁=0 but DECREASE with x₂ when x₁=1 — a single fixed coefficient w₂ on x₂ cannot do both. The linear model cannot use x₁ to change the coefficient on x₂. (You met this exact wall in §1.2.1; now we break it.)

The fix: learn a new feature space

Add one hidden layer with two units. The network chains two functions: h=f(1)(x;W,c)\boldsymbol{h} = f^{(1)}(\boldsymbol{x}; \boldsymbol{W}, \boldsymbol{c}) then y=f(2)(h;w,b)y = f^{(2)}(\boldsymbol{h}; \boldsymbol{w}, b) — the output layer is still linear regression, but applied to h\boldsymbol{h} instead of x\boldsymbol{x}. f(1)f^{(1)} must be nonlinear: if it were linear, the whole composition would collapse to a linear function (wWx=xw\boldsymbol{w}^\top\boldsymbol{W}^\top\boldsymbol{x} = \boldsymbol{x}^\top\boldsymbol{w}'). So we use an affine map followed by a fixed activation function — the default rectified linear unit (ReLU) , g(z)=max{0,z}g(z) = \max\{0, z\}:

Fig 6.3 — the ReLU activation g(z) = max{0, z}: piecewise-linear, two pieces
0.03.06.0-606z
g(z) = max{0, z}= 2.000
z2.00

Why a nearly-linear nonlinearity?

ReLU is barely nonlinear — two straight pieces with a kink at 0. That is the point: it preserves the properties that make linear models easy to optimize (large, consistent gradients) and generalize well, while adding just enough nonlinearity to matter. As a Turing machine builds everything from storing 0/1, a universal function approximator can be built from rectified linear pieces.

The complete network is one clean expression:

Term by term

f(x;W,c,w,b)=wmax{0,Wx+c}+bf(\boldsymbol{x}; \boldsymbol{W}, \boldsymbol{c}, \boldsymbol{w}, b) = \boldsymbol{w}^\top \max\{0,\, \boldsymbol{W}^\top\boldsymbol{x} + \boldsymbol{c}\} + b
Wx+c\boldsymbol{W}^\top\boldsymbol{x} + \boldsymbol{c}the hidden layer’s AFFINE map — a whole vector of biases c now, one per hidden unitpre-activation, 2×1
max{0,}\max\{0, \cdot\}the ReLU activation, applied element-wise → the hidden features hh, 2×1
w()+b\boldsymbol{w}^\top(\cdot) + bthe linear output layer, now reading h instead of xscalar

Watch the feature space do the work

The book supplies an exact solution: W=[1111]\boldsymbol{W} = \begin{bmatrix}1&1\\1&1\end{bmatrix}, c=[0,1]\boldsymbol{c} = [0, -1]^\top, w=[1,2]\boldsymbol{w} = [1, -2]^\top, b=0b = 0. Step through the four inputs and see why it works — the ReLU layer folds the two “1” points onto the same point in h\boldsymbol{h}-space, where a single line finally separates the classes (fig 6.1 right):

Fig 6.1 (interactive) — XOR solved by learning a representation
original x-space (no line works)x₁→ ReLU(Wᵀx+c) →learned h-space (a line works)collapsed!h₁
Wᵀx + c = [1, 0]
h = max(0, ·) = [1, 0]
wᵀh + b = 1·1 + (−2)·0 = 1 (target XOR = 1)

blue = target 0, green = target 1. The ReLU hidden layer folds (0,1) and (1,0) onto the SAME h-point, so the output line separates the classes.

In a real problem there might be billions of parameters and examples — you cannot guess the solution. But this one sits at a global minimum of the loss, so gradient descent could converge to it (or to one of the other equivalent solutions, depending on initialization — and it would rarely be this clean and integer-valued).

§6.2 Gradient-based learning

Training a neural network is mostly like training any other model with gradient descent: pick an optimizer, a cost function, and a model family (§5.10). The one big difference:

Aside — the price of nonlinearity: non-convex loss

A neural network’s nonlinearity makes most interesting loss functions non-convex. So instead of the linear-equation solvers (linear regression) or globally-convergent convex optimizers (logistic regression, SVMs), neural nets are trained by iterative, gradient-based optimizers that merely drive the cost low — with NO convergence guarantee and real sensitivity to where they start. Practical consequence: initialize weights to small random values, and biases to zero or small positives. (The algorithms live in chapter 8; initialization in §8.4.)

Everything else — computing the gradient efficiently and exactly — is what back-propagation handles. The two design choices that remain, the cost function and the output units, get special attention next, because for neural networks their interaction with saturation makes or breaks learning: cost functions and output units.

Check yourself — XOR and gradient-based learning

1.The linear model on XOR solves to w = 0, b = ½. What is it doing, and why can't a better linear fit exist?

2.Why must the hidden function f⁽¹⁾ be nonlinear?

3.In the XORNetLab, you select input (0,1) and then (1,0). What do you observe in the h-space pane?

4.Why is the ReLU's 'barely nonlinear' shape considered a feature, not a bug?

5.What is the ONE big difference between training a neural network and a linear/logistic model with gradient descent?

5 questions