SECTION 04 / 4

XOR, Boolean Functions and the Power of a Network of Perceptrons

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

Build XOR from two hidden decisions

Let one hidden neuron compute OR and the other compute AND. The output is 1 when OR − 2·AND − 0.5 is at least zero.

  1. For inputs (1, 0), OR is 1 and AND is 0.

    z=1−2(0)−0.5=0.5⇒y=1z=1-2(0)-0.5=0.5\quad\Rightarrow\quad y=1
  2. For inputs (1, 1), both hidden outputs are 1.

    z=1−2(1)−0.5=−1.5⇒y=0z=1-2(1)-0.5=-1.5\quad\Rightarrow\quad y=0
  3. The same rule handles the other two inputs.

    (0,0)↦0,(0,1)↦1(0,0)\mapsto0,\qquad(0,1)\mapsto1

What changed? The hidden layer creates two useful features. Their combination separates the XOR cases.

Quick check: What is the final output for (0, 1)?

1: OR is 1, AND is 0, and the output score is 0.5.

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

Start here One perceptron draws one straight line. Some problems — even a tiny one called XOR — cannot be solved with one line. The fix is surprisingly simple: use many perceptrons in layers. This page proves that such a network can represent any Boolean function.

1. XOR: the function one perceptron cannot learn

XOR (“exclusive OR”) is 1 when the two inputs are different.

x1x_{1} x2x_{2} XOR Condition for a perceptron Simplifies to
0 0 0 w0+0+0<0w_{0} + 0 + 0 < 0 w0<0w_{0} < 0
0 1 1 w0+w2≥0w_{0} + w_{2} \ge 0 w2≥−w0w_{2} \ge -w_{0}
1 0 1 w0+w1≥0w_{0} + w_{1} \ge 0 w1≥−w0w_{1} \ge -w_{0}
1 1 0 w0+w1+w2<0w_{0} + w_{1} + w_{2} < 0 w1+w2<−w0w_{1} + w_{2} < -w_{0}

The contradiction, step by step:

  1. From rows 2 and 3:w1+w2≥−w0+(−w0)=−2w03: w_{1} + w_{2} \ge -w_{0} + (-w_{0}) = -2w_{0}.
  2. From row1,w0<0row 1, w_{0} < 0, so −w0>0-w_{0} > 0, so −2w0>−w0-2w_{0} > -w_{0}.
  3. Sow1+w2≥−2w0>−w0,iSo w_{1} + w_{2} \ge -2w_{0} > -w_{0}, i.e. w1+w2>−w0w_{1} + w_{2} > -w_{0}.
  4. Row 4 needs w1+w2<−w0w_{1} + w_{2} < -w_{0}. Impossible.
The two 1-points sit on one diagonal and the two 0-points on the other. Every line makes at least one mistake.
The two 1-points sit on one diagonal and the two 0-points on the other. Every line makes at least one mistake.
Open image ↗
XOR points: no straight line separates the 1s from the 0s

Real data is like this too. Most real-world data is not linearly separable. It has outliers, and sometimes even without outliers no line can separate it. We need models that can handle this. A single perceptron cannot — but a network of perceptrons can.

2. How many Boolean functions of 2 inputs?

Each function is one way of filling the output column for the 4 input rows. Each row can be 0 or 1, so there are 2×2×2×2=2 \times 2 \times 2 \times 2 = 24=162^{4} = 16 functions.

x1x_{1} x2x_{2} f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

Of these 16, 14 are linearly separable. Only f7=XORf7 = XOR and f10=!XOR(XNOR)f10 = !XOR (XNOR) are not.

For n inputs: there are 2n2^{n} input rows, and each can output 0 or 1, so there are 22n2^{2^{n}} Boolean functions. (n=2→24=16;n=3→28=256n = 2 \to 2^{4} = 16; n = 3 \to 2^{8} = 256.) Some of these are not linearly separable. (The exact count is not needed for this course.)

3. A network of perceptrons for any 2-input function

For this part, use True =+1= +1 and False =−1= -1.

The network:

  • Input layer:x1,x2. x_{1}, x_{2}.
  • Hidden layer: 4 perceptrons h1,h2,h3,h4h_{1}, h_{2}, h_{3}, h_{4}. Each gets both inputs with weights +1 (blue edge) or −1 (red edge). Each has bias −2 — it fires only if its weighted sum is ≥2\ge 2.
  • Output layer: one perceptron y, with weights w1,w2,w3,w4w_{1}, w_{2}, w_{3}, w_{4} from the hidden units (these are what we choose/learn).

Terminology: input layer, hidden layer (the middle one), output layer. Red/blue edges == layer 1 weights.w1…w4=. w_{1}\ldots w_{4} = layer 2 weights. Outputs of the hidden perceptrons =h1…h4= h_{1}\ldots h_{4}.

Key trick: each hidden perceptron fires for exactly one input.

Hidden unit Weights (x1,x2)(x_{1}, x_{2}) Fires only for Check: sum for that input
h1h_{1} (−1, −1) (−1, −1) (−1)(−1)+(−1)(−1)=2≥2✔(-1)(-1) + (-1)(-1) = 2 \ge 2 ✔
h2h_{2} (−1, +1) (−1, +1) (−1)(−1)+(+1)(+1)=2✔(-1)(-1) + (+1)(+1) = 2 ✔
h3h_{3} (+1, −1) (+1, −1) 2 ✔
h4h_{4} (+1, +1) (+1, +1) 2 ✔

For any other input, the sum is 0 or −2, which is <2< 2, so the unit stays off.

Each hidden unit “owns” one row of the truth table, and its output weight decides that row’s answer.
Each hidden unit “owns” one row of the truth table, and its output weight decides that row’s answer.
Open image ↗
2-4-1 network of perceptrons with red and blue edges computing XOR

Lecture example — XOR with this network Let the output fire if ∑wihi≥w0\sum w_{i}h_{i} \ge w_{0} (output threshold). For each input only one hi=1h_{i} = 1, so the sum is just that wiw_{i}:

x1x_{1} x2x_{2} XOR h1h2h3h4h_{1} h_{2} h_{3} h_{4} ∑wihi\sum w_{i}h_{i}
0 (−1) 0 (−1) 0 1 0 0 0 w1w_{1}
0 (−1) 1 (+1) 1 0 1 0 0 w2w_{2}
1 (+1) 0 (−1) 1 0 0 1 0 w3w_{3}
1 (+1) 1 (+1) 0 0 0 0 1 w4w_{4}

Conditions: w1<w0,w2≥w0,w3≥w0,w4<w0.w_{1} < w_{0}, w_{2} \ge w_{0}, w_{3} \ge w_{0}, w_{4} < w_{0}. No contradiction now! One answer: w0=0,w1=−1,w2=+1,w3=+1,w4=−1w_{0} = 0, w_{1} = -1, w_{2} = +1, w_{3} = +1, w_{4} = -1.

Why it works: each wiw_{i} controls one row alone, so we can set every row’s answer freely. The same network can do all 16 functions — just change w1…w4w_{1}\ldots w_{4}. Each function gives its own set of non-contradicting inequalities.

4. More inputs: 2ⁿ hidden perceptrons

  • 3 inputs: 8 input combinations → 8 hidden perceptrons (bias −3, each fires for one combination) → 8 output weights.
  • n inputs: 2n2^{n} combinations →2n\to 2^{n} hidden perceptrons.

Theorem. Any Boolean function of n inputs can be represented exactly by a network of perceptrons with 1 hidden layer of 2n2^{n} perceptrons and 1 output perceptron. Proof (informal): we just built it.

Note: 2n+12^{n} + 1 perceptrons is sufficient, not necessary. AND needs just 1 perceptron.

The catch: the hidden layer grows exponentially.

5. Back to the movie problem

We have past data: for each movie, features x1…xnx_{1} \ldots x_{n} and the label y (like =1= 1 / dislike =0= 0). Points with y=1y = 1 are p1,p2,…p_{1}, p_{2}, \ldots ; points with y=0y = 0 are n1,n2,…n_{1}, n_{2}, \ldots . The data may or may not be linearly separable.

The theorem says: there exists a network of perceptrons whose weights can be learned so that its output matches y for every pip_{i} and njn_{j}. So a network can separate positive and negative points even when one line cannot.

6. Multilayer Perceptron (MLP)

Networks with an input layer, one or more hidden layers, and an output layer are called Multilayer Perceptrons (MLPs). (A more accurate name would be “multilayered network of perceptrons”, but MLP is the common name.)

Representation power of an MLP with one hidden layer: it can represent any Boolean function exactly.

In one line: One perceptron fails on XOR, but one hidden layer of 2n2^{n} perceptrons can represent any Boolean function — at the cost of exponentially many units.

Exam words: XOR, XNOR, not linearly separable, 22n2^{2^{n}} Boolean functions, hidden layer, input layer, output layer, layer 1/layer 2 weights, multilayer perceptron (MLP), representation power, 2n2^{n} hidden perceptrons, sufficient vs necessary.

Quick check

  1. How many Boolean functions of 3 inputs exist?
223=28=256. 2^{2^{3}} = 2^{8} = 256.
  1. In the ±1 XOR network, which hidden unit fires for input (+1, −1), and what is its sum?
h3h_{3} (weights +1,−1+1, -1): (+1)(+1)+(−1)(−1)=2≥2(+1)(+1) + (-1)(-1) = 2 \ge 2, so it fires. All others give 0 or −2.
  1. Using the same network, give w1…w4w_{1}\ldots w_{4} (with w0=0w_{0} = 0) for AND (only (+1,+1) → 1).
w1=−1,w2=−1,w3=−1,w4=+1w_{1} = -1, w_{2} = -1, w_{3} = -1, w_{4} = +1. Only h4h_{4}’s row reaches the threshold.
  1. How many hidden perceptrons does the theorem use for 5 inputs?
25=322^{5} = 32 hidden perceptrons (plus 1 output perceptron).
  1. Is a hidden layer of 2n2^{n} units needed for every function?
No. It is enough (sufficient) for any function, but simple functions like AND or OR need only one perceptron.
YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑