Visualizations
017 / 500

017 · Machine learning

ReLU Stained Glass

A ReLU network's exact linear regions, drawn as a stained glass window.

A small ReLU network learns to separate two classes of points, and the plane is drawn as what the network really is: a mosaic of convex polygons, inside each of which it computes one exact affine function. The mosaic is not sampled on a grid. Each neuron's pre-activation is affine inside every polygon, so the polygon is clipped in two along that neuron's line, layer by layer, and the affine maps are composed through the units that stay on. Panes are tinted by the predicted class and grow pale near the decision boundary, which is drawn as a gold wire with exactly one straight segment per pane, and the lead is heavier for cuts made by earlier layers.

Try it. Click to add a point of one class and Shift-click (or right-click) for the other. Use the chips or the arrow keys to change depth and width, which shatters the window and grows a new one; press 1 to 4 for datasets and R to reinitialize. Hover any pane to see its activation pattern and its affine logit.

  • Exact linear region enumeration
  • Convex polygon clipping
  • Backpropagation with Adam

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a "ReLU stained glass" visualization with JavaScript and the HTML canvas element: a tiny ReLU neural network that classifies 2D points, drawn as the mosaic of linear regions it really is. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window.
- Generate a two-class dataset (two interleaved spirals) in the square from -1 to 1, mapped to the middle of the screen.
- Write a small network from scratch: 2 inputs, two hidden layers of 8 ReLU units, one output logit. Train it with full-batch Adam and binary cross-entropy, a few steps per frame.
- Compute the linear regions exactly. Start with one polygon, the visible rectangle, carrying the identity affine map from (x, y) to the inputs. For each hidden layer, every neuron's pre-activation is an affine function a x + b y + c inside each polygon, so split each polygon along that line. Then zero the units that are off in each piece and compose the affine maps for the next layer.
- Fill each final polygon with a color from the predicted class at its centroid, and stroke the edges dark. Show the region count.

Once that works, make it beautiful:
- Treat it as a stained glass window: vary each pane's hue slightly using a hash of its activation pattern, make panes near the decision boundary pale, add a faint mottled texture, and draw thicker lead for edges cut by the first layer.
- The output is affine in every pane, so draw the decision boundary as one straight gold segment per pane.
- Let clicks add points and buttons change depth and width.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as a hover card showing a pane's activation pattern, shattering the window into falling shards when the architecture changes, or plotting region count against depth.
PreviousMandelbulbA 3D fractal, ray marched pixel by pixel on the CPU. NextProcedural PinballGenerated pinball tables in perspective, played by an AI that simulates its flips.

Related visualizations

  • Neural NetworkMachine learning A tiny neural network learns to classify points, live, in your browser.
  • Normalizing FlowMachine learning An invertible network trains live and bends a Gaussian and its grid like taffy.
  • Lottery TicketMachine learning Prune a network to a sparse winning ticket that still learns, round by round.
  • Voronoi MosaicMath Drifting seeds divide the plane into a living stained-glass mosaic.
  • Deep Q SnakeMachine learning A deep Q-network learns snake from scratch, live, on a retro dot-matrix LCD.
  • Fourier FeaturesMachine learning A plain MLP, random Fourier features and a SIREN race to memorize one picture.
  • Self-Organizing QuiltMachine learning Kohonen maps sew a quilt from fabric dyes and drape a sheet over 3D point clouds.
  • GrokkingMachine learning A tiny network memorizes modular addition, then suddenly understands it.
  • Growing Neural CellsMachine learning Cells running one tiny learned rule grow a gecko from a single seed and heal its wounds.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs