Chapter 3

Layers of Thinking

One neuron can only draw a straight line. Stack them in layers, and they can learn anything.

🧠 Why One Neuron Isn't Enough

In the previous chapters, you met a single neuron — a tiny math machine that takes in numbers, multiplies them by weights, adds a bias, and squishes the result through an activation function. That single neuron learned to do some cool things, like telling apart two groups of dots.

But here's the thing: a single neuron has a fundamental limitation. It can only draw one straight line (or, in higher dimensions, a flat surface) to separate things. Imagine you have a piece of paper with red dots and blue dots on it. A single neuron is like placing one straight ruler on the paper — everything on one side is "red" and everything on the other side is "blue." That works great when the dots are neatly separated!

But what happens when the dots are arranged in a pattern that can't be split with a single straight line? That's where things get interesting — and that's exactly what the XOR problem is all about.

❌ The XOR Problem

What Does "XOR" Even Mean?

XOR stands for "exclusive or." In plain English, it means: "one or the other, but not both."

Here's a real-world example that makes it crystal clear. Imagine a room with a light and two switches — one by each door. The light works like this:

See the pattern? The light turns on when exactly one switch is flipped. If both are the same (both on or both off), the light stays off. That's XOR!

This might sound simple — and to your brain, it is. You can look at those four cases and instantly see the pattern. But for a single neuron, this is impossible to solve. Let's understand why.

Why a Single Neuron Fails at XOR

Remember: a single neuron can only draw one straight line to separate things. Let's visualize the four XOR cases as dots on a grid:

The "ON" dots are at the top-left and bottom-right. The "OFF" dots are at the bottom-left and top-right. They're arranged diagonally — like opposite corners of a checkerboard.

Now try to draw a single straight line that puts the two "ON" dots on one side and the two "OFF" dots on the other. Go ahead, imagine it. You can't do it! No matter how you angle the line, you'll always have one "wrong" dot on the wrong side. It's like trying to separate the black squares from the white squares on a checkerboard with one straight cut — geometrically impossible.

This was actually a huge deal in the history of artificial intelligence. In the 1960s, researchers realized that single-layer networks (called perceptrons) couldn't solve XOR, and many people thought neural networks were a dead end. It took decades before someone figured out the solution: add more layers.

What the Visualization Shows

Below you'll see two colored grids side by side. Here's how to read them:

The left grid uses a single neuron. Watch how it can only create a straight color boundary — it'll never make that checkerboard pattern. The right grid uses a network with a hidden layer (more on that in a moment). Watch how it curves and bends the boundary until it gets the pattern right!

Single Neuron (fails!)

0
Epoch
Loss

Two Layers (succeeds!)

0
Epoch
Loss
Press "Train Both" to watch the single neuron struggle while the 2-layer network learns XOR!

What Just Happened?

If you hit "Train Both," you saw something remarkable. The single neuron on the left tried its best — its loss went down a bit, and the color boundary shifted around — but it could never create the checkerboard pattern XOR requires. It's stuck forever drawing a straight line.

The two-layer network on the right, however, gradually bent its decision boundary into a shape that correctly separates all four dots. The loss (how wrong the network is) dropped close to zero, meaning it nailed it.

This is the power of layers. A single neuron draws lines. Multiple layers draw curves, corners, and any shape you need.

🫣 What is a Hidden Layer?

You've been hearing "hidden layer" — let's define it properly.

A neural network is organized in layers. Think of it like an assembly line in a factory:

  1. Input layer — This is where raw data comes in. For our XOR problem, the input is two numbers (the two switch positions). You can see this layer; it's your data.
  2. Hidden layer(s) — These are the layers in between. They take the input, transform it, and pass results forward. They're called "hidden" because you don't directly see their values in the final answer — they're like the intermediate thoughts in your head before you reach a conclusion. When you solve a math problem, you don't just jump from the question to the answer; you have intermediate steps. Hidden layers are those intermediate steps.
  3. Output layer — The final answer. For XOR, it's a single number: close to 1 for "ON" or close to 0 for "OFF."

The magic happens in the hidden layers. Each neuron in a hidden layer learns to detect a different feature or pattern in the data. In the XOR example, the hidden neurons might learn things like "is switch A on?" and "are both switches the same?" — and then the output neuron combines those intermediate answers to produce the final result.

The more hidden layers you add, the more abstract the thinking becomes. The first layer might detect simple patterns. The second layer combines those into more complex patterns. The third layer combines those into even more complex ones. This is why deep networks (networks with many layers) can recognize faces, understand language, and do other amazing things.

⚡ Meet ReLU — The Most Popular Activation Function

In Chapter 1, you learned about the sigmoid activation function — the S-shaped curve that squishes any number into a value between 0 and 1. Sigmoid works, but it has some problems when networks get deep (many layers). The signal can get weaker and weaker as it passes through layers — a problem called the vanishing gradient.

Enter ReLU, which stands for Rectified Linear Unit. Don't let the fancy name scare you. Here's what ReLU does:

That's it. Seriously. For example:

It's like a gate that only lets positive signals through. Why is this so popular? Because it's fast (just a simple comparison), it doesn't squish large values (so signals stay strong through many layers), and it works really well in practice. ReLU is the default choice for most modern neural networks.

You'll also see Tanh in the demos below. Tanh is like sigmoid's cousin — it squishes values between -1 and +1 instead of 0 and 1. Each activation function has its strengths, but ReLU is the workhorse of modern deep learning.

🔬 Build Your Own Network

Now it's your turn to experiment! The demo below lets you build a custom neural network and watch it learn in real time. Before you dive in, let's explain what everything means.

The Controls

Hidden Layers — This dropdown lets you choose the topology (shape) of your network. Here's what the options mean:

Rule of thumb: more layers = more abstract thinking. More neurons per layer = more nuance in each level of thinking.

Dataset — The shape of data the network needs to separate. XOR is the diagonal pattern you already know. Circle means one class is inside a ring and the other is outside. Spiral is two classes wound around each other — the hardest pattern here!

Activation — The squishing function used by each hidden neuron. Sigmoid, ReLU, and Tanh — you know these from earlier in this chapter and from Chapter 1.

The Display

On the left, you see the decision boundary canvas. This is the same kind of colored grid from the XOR demo — purple means the network says "1," light means "0." The dots are the training data points. As the network learns, the colors shift to correctly surround each group of dots.

On the right, you see two things:

The Parameters stat shows the total number of weights + biases the network needs to learn. A network with topology [2, 4, 4, 1] has 2×4 + 4 + 4×4 + 4 + 4×1 + 1 = 37 parameters. More parameters = more power, but also more to learn.

0
Epoch
Loss
Parameters
💡 Things to Try
  • 🌀 Select the Spiral dataset with just 1 layer (4 neurons). Hit Train. Watch it struggle — one layer can't draw spiral-shaped boundaries!
  • Now switch to 2 layers (4, 4) and reset. Train again. Better, right?
  • Try 3 layers (8, 6, 4) with Spiral. Watch how more layers let the network carve increasingly complex boundaries.
  • Switch between ReLU and Sigmoid on the same dataset. Notice how ReLU often learns faster and creates sharper boundaries.
  • Try the Circle dataset with just 1 layer. Even a simple network can handle this — why? Because a circle boundary can be approximated with a few straight cuts combined together.

📝 What You Just Learned

In the next chapter, you'll get a full playground where you can experiment with even more datasets, adjust the learning rate, and really see how all these pieces work together. Let's go! 🚀