Folding space: what a neural network does to the paper 🗞️
Have you ever looked at a neural network's decision boundary, a wiggly line curling neatly around the red dots, and wondered how a pile of multiplications and additions drew that? In part 1 we built networks one neuron at a time and watched the formulas. This time, let's watch something else: what those formulas do to space.
Here's the whole trick in one sentence. A neural network can't draw a curved line. All it can do is pick up the paper the data is printed on, then stretch it, bend it and fold it, until one straight cut separates red from teal. Every figure below is that paper, live: you can drag it, train it, turn it around in 3D, and even fold a real one.
Under each figure, ▦ Numbers shows the actual matrices, ∑ Math the formulas, and 🐍 Python runnable NumPy code that talks to the figure. ✨
The whole idea in one picture
Think of the two classes as two earphone cables tangled in your pocket: a red one and a teal one, spiralling around each other. Lying flat like that, no straight cut can separate them. But you're allowed to move the paper they're printed on.
Every layer of a network does two things to the paper. A linear part (a matrix) stretches, rotates and tilts it, and a bend (the activation function) curves or folds it. At the very end, the output neuron makes one straight cut. Let's take those three moves one at a time.
A matrix is what it does to the grid
A layer with two neurons computes, for every point \(\mathbf{x}\) on the paper, two new numbers: \(\mathbf{x}' = W\mathbf{x} + \mathbf{b}\). Usually we read that as "each neuron takes a weighted sum". Here's another way to read it: W moves every point of the paper at once, so it moves the paper itself.
And you only need to know where two arrows go. The first column of \(W\) is where the arrow \(\hat\imath\) (one step right) lands; the second column is where \(\hat\jmath\) (one step up) lands. Every other point follows, because \(W\mathbf{x} = x_1\hat\imath' + x_2\hat\jmath'\). The bias \(\mathbf{b}\) just slides the whole sheet.
Notice what never happens: straight lines stay straight, parallel lines stay parallel, and every grid square becomes the same parallelogram.
▦ Numbers
One point through the layer, then the first five points of the data at once. Shading: red positive, teal negative.
∑ Math
The layer and its current numbers:
The determinant \(\det W = w_{11}w_{22} - w_{12}w_{21}\) is how much the area of any patch of paper gets multiplied by. Negative means the paper flipped over; zero means it was flattened onto a line (or a point), and that's the one move nothing can undo: two different inputs now have the same numbers.
Two linear layers in a row are still one: \(W_2(W_1\mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2 = (W_2W_1)\mathbf{x} + (W_2\mathbf{b}_1 + \mathbf{b}_2)\).
🐍 Python
Drag as much as you like: the red and teal cables stay tangled. A matrix can tilt and stretch the paper, but a straight cut after it is still a straight cut before it. And stacking more matrices doesn't help either, because two in a row are just one bigger move of the same kind. We need something that bends.
The bend: where straight lines stop being straight
After the matrix, each neuron passes its number through an activation function, one coordinate at a time. In part 1 we drew those as little curves. On the paper, they look like this.
▦ Numbers
The first five points, before and after each step.
∑ Math
$$\mathbf{z} = W\mathbf{x} + \mathbf{b}, \qquad \mathbf{h} = f(\mathbf{z}) = \begin{bmatrix} f(z_1) \\ f(z_2) \end{bmatrix}$$For ReLU, \(f(z) = \max(0, z)\): the crease lines are exactly where \(z_1 = 0\) or \(z_2 = 0\), one straight crease per neuron. A fold is two ReLUs: \(|z| = \operatorname{relu}(z) + \operatorname{relu}(-z)\).
🐍 Python
Now straight lines can become curves (tanh), and the paper can be creased (ReLU) or folded right on top of itself (|z|). But with two neurons, the paper is stuck in a flat plane, like a tablecloth you can only push around on the table. What if a layer had three neurons?
Lifting the paper: three neurons, three dimensions
Three neurons give every point three numbers, so the paper now lives in 3D. The matrix lifts it off the table, tilted but still perfectly flat (a linear map can't bend anything). Then the bend curves it. And the output neuron, \(w_1h_1 + w_2h_2 + w_3h_3 + c\), is positive on one side of a flat plane through that space: that's the straight cut.
Below is a real network, trained on the dots in this page. Press ▶ play to follow the paper through each step, and drag to turn it around.
▦ Numbers
Every weight of the network above, live while it trains. Each row of a W is one neuron.
∑ Math
A network with one hidden layer of three neurons, and the current first layer:
$$\mathbf{h} = f(W_1\mathbf{x} + \mathbf{b}_1) \in \mathbb{R}^3, \qquad \text{output} = \mathbf{w}_2\cdot\mathbf{h} + c \;\begin{cases} > 0 & \text{red} \\ < 0 & \text{teal}\end{cases}$$The points where the output is exactly 0 form the plane \(\mathbf{w}_2\cdot\mathbf{h} + c = 0\) in the 3D space of \(\mathbf{h}\). Pulled back to the paper, \(\{\mathbf{x} : \mathbf{w}_2\cdot f(W_1\mathbf{x} + \mathbf{b}_1) + c = 0\}\) is the curved boundary on the flat view.
🐍 Python
Look at the flat view next to the 3D one. The curvy boundary around the red middle isn't something the network drew. It's the flat cut, seen from the paper's point of view. Curved boundaries are straight cuts through bent paper.
Which networks can untangle it?
So which shapes of network can do which jobs? Rather than argue, let's try them all. Each tile below is a separate small network, trained from the same random start on the ring data until it separates the dots (at most 1500 steps). Click a tile to load it into the sheet above.
A few things to look for:
- Two tanh neurons per layer, on the ring. With two neurons the paper never leaves the plane, and the matrix and tanh can stretch and bend it but never tear it or pass it through itself. So the teal ring stays wrapped around the red middle, however many layers you add, and no straight line can cut the middle out. The network is stuck squeezing the paper and hoping, and the tiles show it: high accuracy, but not 100%.
- Three neurons. Now the middle can be lifted out of the plane, and one layer is enough.
- ReLU with two neurons plays by different rules: flattening the paper onto a crease can put two points in the same place, so the "no tearing" argument doesn't apply. On the ring it doesn't help, though: it does even worse than tanh, because once paper is flattened, that information is gone for every later layer.
This is a big reason hidden layers in real networks are usually wider than their input: room to lift the paper and pull things apart.
Paper snowflakes: depth is folding
Remember the |z| bend from Demo 2: it laid one half of the paper exactly on top of the other. That's a fold, and a fold is just one ReLU neuron plus a skip connection that adds the input back:
$$\mathbf{x} \;\leftarrow\; \mathbf{x} + 2\,\operatorname{relu}(c - \mathbf{n}\cdot\mathbf{x})\,\mathbf{n}$$Points on the kept side of the line \(\mathbf{n}\cdot\mathbf{x} = c\) don't move; points on the other side are mirrored over it. Now do what every kid does in December: fold the paper a few times, make one straight cut, and unfold. Each fold is a layer, and the cut is the output neuron.
▦ Numbers
The network you just built by folding: one neuron per fold, plus the cut.
∑ Math
A fold maps the plane onto one side of a line and never stretches it: distances along the paper are kept. After \(k\) folds, up to \(2^k\) layers of paper lie under the scissors, so one straight cut becomes up to \(2^k\) edges once unfolded. The fold-and-cut theorem (Demaine, Demaine and Lubiw, 1998, and Bern et al.) says this is enough for any shape with straight sides: some sequence of folds lines up all its edges for one single cut.
🐍 Python
Four folds, four neurons, one cut, and you get an eight-pointed star with sixteen straight edges. Getting those sixteen edges with a single layer would take sixteen neurons, one per edge. Each layer of folding doubles what the next layer can do. That's the deepest reason depth matters, and the next demo counts it. (Want to write the folds yourself? It's step 3 of the Fold the paper challenge.)
Counting the pieces: wide vs deep
Here's another way to see a ReLU network: it chops the paper into flat pieces, and on each piece it's just a plain matrix. A crease starts wherever any neuron switches on or off. So a good measure of what a network can express is how many pieces it can make. Let's give two networks the same budget of neurons: one spends it all in one wide layer, the other stacks it in layers.
∑ Math
One layer of \(B\) ReLU neurons on a 2-D input draws \(B\) straight lines, and \(B\) lines cut the plane into at most
$$1 + B + \binom{B}{2} = 1 + B + \frac{B(B-1)}{2}$$pieces, which grows like \(B^2\). Folding by hand with 4 neurons per layer, \(k\) layers give \((2^k)^2 = 4^k\) pieces, which grows exponentially: at \(B = 16\) that's \(256\) against at most \(137\). (Montúfar, Pascanu, Cho and Bengio, 2014, proved deep ReLU networks can have exponentially more pieces than shallow ones of the same size.)
🐍 Python
Two honest surprises. With few neurons, wide wins: folding needs a few layers before it pays off. And with random weights, the deep network makes even fewer pieces than the wide one: most of its folds land in places that don't multiply anything. Depth gives the potential for exponentially many pieces; whether a network uses it depends on its weights, and that's training's job.
Shadows of higher dimensions
Real layers don't have three neurons, they have hundreds or thousands, so the paper lives in a space we can't picture. What we can do is look at its shadow: project it down to 2-D, the way the sun projects you onto the pavement. Below, a network with two layers of 8 neurons was trained on the earphones. Its paper lives in 8 dimensions, and the shadow slowly turns.
From most angles the cables still look tangled. That's what makes high-dimensional data hard to inspect. But from the one direction the output neuron looks along, red and teal fall on opposite sides of a line. The network didn't untangle the cables in every view, only in the one view that matters.
From here to the big ones
- Skip connections are everywhere. Our folds added the input back (\(\mathbf{x} + \ldots\)). So does every block of a ResNet and every layer of a Transformer: each layer nudges the paper rather than replacing it, which keeps deep stacks trainable.
- Language models move paper too. Inside an LLM, every word is a point in a space of thousands of dimensions, and each layer moves it. Words that need different next words get pulled apart, just like our cables.
- The manifold idea. Real data (photos, sounds) is believed to lie on thin, crumpled sheets inside huge spaces. A network's job is to uncrumple the sheet until the classes are easy to cut apart.
Cheat sheet
| move | formula | what it does to the paper |
|---|---|---|
| linear | \(W\mathbf{x} + \mathbf{b}\) | stretch, rotate, shear, slide; straight lines stay straight; \(\det W\) scales area, \(0\) flattens |
| lift | \(W \in \mathbb{R}^{3\times 2}\) | places the flat paper into 3D, tilted |
| tanh | \(\tanh(z_i)\) | bends, squeezes everything into a cube |
| ReLU | \(\max(0, z_i)\) | presses paper flat onto a wall: one straight crease per neuron (on its input) |
| fold | \(\mathbf{x} + 2\,\operatorname{relu}(c - \mathbf{n}\cdot\mathbf{x})\,\mathbf{n}\) | mirrors one side of a line onto the other; \(k\) folds stack \(2^k\) layers |
| cut | \(\mathbf{w}\cdot\mathbf{h} + c > 0\) | one flat plane; looks curved when pulled back onto the paper |
| pieces | \(1 + B + \binom{B}{2}\) vs \(4^k\) | one layer of \(B\) vs \(k\) folding layers |
Conclusion
A neural network never draws a curve. Each layer stretches the paper with a matrix and bends or folds it with an activation, and the output neuron makes one straight cut. Every curvy boundary you have ever seen around a cluster of dots is that cut, seen from the paper's point of view. More neurons per layer give the paper room to move around; more layers let it fold onto itself, and every fold doubles what the next cut can do.
Ready to fold one yourself? Print a snowflake from Demo 5 and cut it, then build it yourself in the workshop's Fold the paper challenge: write the fold, the cut and the piece counter in Python, solve the star puzzle, and measure how wide a network must be to untangle the ring.
If you want to go further: Chris Olah's essay "Neural Networks, Manifolds, and Topology" is the classic on this way of seeing networks, and Erik Demaine's fold-and-cut page has many more shapes to cut from one fold. Happy folding! 🗞️✂️✨
Everything on this page runs in your browser: the 3D sheet with WebGL2, the training in plain JavaScript, and the 🐍 drawers with Pyodide.
Let's stay connected!
Author
I'm Oliver Nguyen. A software maker working mostly in Go and JavaScript. I enjoy learning and seeing a better version of myself each day. Occasionally spin off new open source projects. Share knowledge and thoughts during my journey.