Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Perceptrons and the XOR Problem

Welcome back to your journey into modern AI. In our last lesson, we deconstructed the fundamental "neuron" and saw how familiar models like logistic regression can be viewed as a single-layer neural network. We established that its power, and its limitation, is its reliance on a linear decision boundary.

Today, we confront that limitation head-on. We'll explore the classic "XOR problem," a simple logical puzzle that a single neuron is provably incapable of solving. This isn't just a historical curiosity; its discovery in the 1960s had profound consequences for the field of AI. More importantly for us, understanding why the single neuron fails and how we fix it provides the core intuition for why deep, multi-layer networks are so powerful. This lesson will explain why stacking layers is not just adding more of the same, but enabling the network to learn progressively more complex representations of data.

The Wall of Linearity: The XOR Problem

As a quick recap, a single perceptron or logistic regression unit defines a decision boundary given by , which is the equation of a line (in 2D) or a hyperplane (in higher dimensions). This means it can only classify data that is linearly separable—that is, you can draw a single straight line to separate the classes.

Many simple logical functions are linearly separable. For example, AND and OR can be easily solved by a single perceptron. However, the XOR (exclusive-or) function is different. XOR is true if and only if exactly one of its inputs is true.

Here is the truth table for XOR:

(XOR)
0 0 0
0 1 1
1 0 1
1 1 0

If we plot these four points on a 2D plane, with one class being the points where the output is 0 (blue) and the other where the output is 1 (red), the challenge becomes immediately apparent.

This figure illustrates the inputs for NAND, OR, and XOR problems. For NAND and OR, a single line (the decision boundary) can separate the two classes. For XOR, no single line can successfully separate the Class 1 points from the Class 0 points.

As you can see, you simply cannot draw one straight line to separate the red points from the blue points. This visual intuition suggests that XOR is not linearly separable. Now, let's prove it formally.

Proof of Non-Separability

Your preference for mathematical rigor will be well-served here. We can prove the non-separability of XOR in several ways. We'll start with a straightforward algebraic proof by contradiction.

The following reading provides historical context, defines the XOR problem, and then walks through a clean algebraic proof.

Chapter 8: The XOR Problem — Classical Foundations of Artificial Neural Networks

This text introduces the XOR problem's historical significance and provides a formal proof that a single-layer perceptron cannot solve it.

Please read from the beginning of the chapter, starting with "The Most Famous Impossibility Result..." through the end of the algebraic proof, concluding at the contradiction. Focus on how the four conditions derived from the truth table lead to an inescapable contradiction.

The algebraic proof is conclusive. For a different and perhaps more generalizable perspective, we can use a geometric argument based on convex sets. A linear classifier works by separating two sets of points with a hyperplane. A key theorem states this is only possible if the convex hulls of the two sets are disjoint. As the video below explains, the convex hulls of the XOR classes intersect, making linear separation impossible.

Deep Learning - Lecture 3.2 (Deep Neural Networks: The XOR Problem)

This video provides a formal proof of XOR's non-separability using the concept of convex sets, which elegantly generalizes the problem.

Please watch from this segment. The lecturer formally defines a convex set and then demonstrates that because the convex hulls of the positive and negative XOR classes overlap, no linear separator can exist.

The Solution: Stacking Layers

If a single layer is insufficient, the natural next step is to add another one. This is the genesis of the Multi-Layer Perceptron (MLP).

A simple Multi-Layer Perceptron (MLP). It consists of an input layer, one or more hidden layers, and an output layer. The "hidden" layers are so-named because their values are not specified in the training data; the network must learn them.

The critical insight is that the hidden layer is not just a passive conduit. It actively transforms the input data. The goal is to take the original data, which is not linearly separable, and map it into a new space (the "hidden representation") where it is linearly separable. The final output layer then has the simple task of drawing a line in this new, transformed space.

How does this work for XOR? One of the most intuitive ways to understand this is to construct a network that solves XOR by combining simpler, linearly separable functions. We know a single perceptron can implement AND, OR, and NAND. It turns out that:

This decomposition suggests an architecture:

  1. A hidden layer with two neurons: one that computes OR, another that computes NAND.
  2. An output layer with one neuron that takes the outputs of the hidden neurons and computes AND.

The following readings and videos demonstrate this construction and the resulting space transformation.

Chapter 8: The XOR Problem — Classical Foundations of Artificial Neural Networks

This part of the article explicitly builds an MLP to solve XOR and, crucially, visualizes the transformation from the original input space to the hidden feature space.

Please read the sections "The Solution: Adding a Hidden Layer" and "Hidden Space Transformation". Pay close attention to the table and the plots that show how the original four input points are mapped to a new set of coordinates in the hidden layer, where they become easy to separate.

The video below offers a complementary perspective, showing how to build this network from the ground up by combining individual logistic regression units.

Deep Learning - Lecture 3.2 (Deep Neural Networks: The XOR Problem)

This segment shows how to construct a multi-layer network for XOR by composing the logical gates we've discussed. It frames the solution as a differentiable computation graph, setting the stage for learning.

Please watch the final segment of the video, from this point. Focus on how the video composes the OR, NAND, and AND classifiers into a single, multi-layer network, which he identifies as our "first multi-layer perceptron."

Finally, to solidify the core concept of space transformation, this short video provides an excellent animated visualization.

Neural Networks 2 XOR

This video provides a concise explanation of the problem and shows a clear animation of how the hidden layer remaps the input points to solve the problem.

Please watch from this section. The key part is the visualization starting around 06:12, which shows the original x space and the transformed h space side-by-side.

The Power of Representation Learning

The XOR example reveals the foundational principle of deep learning: learning representations.

In classical machine learning, you might have tackled a non-linear problem like this through feature engineering. For example, you could manually add a new feature, , to your input. In this new 3D space, the XOR problem becomes linearly separable. The video from Tübingen Machine Learning discusses this exact point (watch this optional segment if you're interested).

What an MLP does is learn the optimal feature engineering automatically. The weights of the hidden layers are trained to create transformations of the data—new representations—that make the classification task progressively simpler for the layers that follow. We didn't have to tell the network to discover the OR and NAND features; with a learning algorithm like backpropagation (which we will cover soon), it can discover these or other, even more effective, features on its own.

Conclusion

This lesson addressed a critical limitation of the single-neuron model and introduced the architectural solution that defines modern neural networks.

Key Takeaways:

  • Single-layer perceptrons can only solve linearly separable problems.
  • The XOR problem is the canonical example of a simple but non-linearly separable function. We proved its non-separability both algebraically and geometrically.
  • Multi-Layer Perceptrons (MLPs) solve this by using hidden layers to perform non-linear transformations on the input data.
  • The hidden layer's role is to map the input data into a new representation space where the classes become linearly separable.
  • This mechanism is a form of automatic representation learning, which is the core idea behind the power of deep learning.

We have now seen what an MLP is and why its layered structure is necessary. In the next lesson, we will formalize its mathematical structure. We will express a feedforward neural network as a composition of affine transformations and nonlinear activation functions, which will provide the precise mathematical language needed to analyze and train these powerful models.

Can't find a good explanation? Sign up and we'll make it for you

Sign up