---
title: "Zone Classifier"
year: "2019"
motto: "Zone Classifier Neural Network"
description: "Classifies a point in a blue or pink zone using a vanilla neural network."
stack: ["javascript"]
website: "https://lab.rolandoandrade.me/zone-classifier/"
url: "https://rolandoandrade.me/projects/zone-classifier/"
author: "Rolando Andrade"
---

# Zone Classifier

> Zone Classifier Neural Network

Classifies a point in a blue or pink zone using a vanilla neural network.

Year: 2019. Stack: javascript. Website: https://lab.rolandoandrade.me/zone-classifier/

Repositories:

- [github](https://github.com/RolandoAndrade/zone-classifier)
- [gitlab](https://gitlab.com/RolandoAndrade/zone-classifier)

Drawing a line between two groups sounds simple. If points on one side are blue and points on the other side are pink, any rule based program can do it. But what if the border is not a straight line? What if the blue and pink regions mix in a complex pattern?

Plain if else logic breaks down fast. You would have to hardcode every curve and corner. Change the pattern, and you start over. Neural networks work differently. They learn the border from examples instead of following written rules.

This project was my first neural network built from scratch. No TensorFlow. No libraries. Only JavaScript, math and a canvas to show the results.

## Click a point, get a color

The app shows a 500x500 pixel canvas split into colored zones. Blue covers some regions and pink covers the rest. The border between them follows a diagonal pattern made by two crossing lines.

A hundred training points appear on the canvas. Blue dots mark blue zones. Pink dots mark pink zones. The neural network studies these examples and learns to classify any point you click.

Click anywhere, and the network predicts the zone. A new dot appears in blue or pink, based on what the network thinks. Early predictions are random guesses. After training, they match the real zones with high accuracy.

The error rate shows below the canvas. You can watch it drop from near 50% to single digits as the network learns.

[https://lab.rolandoandrade.me/zone-classifier/](https://lab.rolandoandrade.me/zone-classifier/)

## Four, two, one

The network has three layers in a simple shape: 4 neurons, then 2 neurons, then 1 output neuron.

The input is the X and Y coordinates of a point. These two values go into the first hidden layer of 4 neurons. That layer feeds a second hidden layer of 2 neurons. The output neuron gives a value between 0 and 1. A value closer to 1 means blue. A value closer to 0 means pink.

![Two coordinates go in, one number between 0 and 1 comes out.](https://storage.rolandoandrade.me/blog/zone-classifier-network.webp)

Each neuron multiplies its inputs by weights, adds a bias, and then applies a sigmoid activation function. The sigmoid squeezes any value into the range from 0 to 1. Its smooth curve works well for classification.

## Learning from mistakes

The network adjusts its weights with backpropagation. The process has four steps:

1. **Forward pass.** The input coordinates flow through each layer and produce a prediction.
2. **Calculate loss.** The difference between the predicted class and the real class becomes the error.
3. **Backward pass.** The error flows back through each layer.
4. **Update weights.** Each weight changes in proportion to how much it caused the error.

The learning rate sets the size of each change. Too high, and the network overshoots. Too low, and learning takes forever. This network uses 0.5, which learns this problem quickly.

Every 300 milliseconds, the network goes through all the training points. Each pass is one training iteration. The weights get a little better every time.

## The math behind it

Forward propagation calculates the output of each layer. The layer multiplies the input by its weight matrix, adds the bias vector and applies the sigmoid to each element.

The sigmoid function turns any real number into a value between 0 and 1:

```
f(x) = 1 / (1 + e^(-x))
```

Its derivative has a handy form that makes backpropagation efficient:

```
f'(x) = f(x) * (1 - f(x))
```

Backpropagation works backward from the output. At each layer, it measures how much each weight added to the error. The gradient points in the direction where the error grows fastest. Subtracting it moves the network toward a lower error.

The delta at each layer depends on two things. One is the derivative of the activation function. The other is the error signal from the layer above. Matrix multiplication applies the weight updates efficiently.

## Two invisible lines

The training data comes from a simple rule. Two invisible lines divide the plane. If a point is on the same side of both lines (above both or below both), it belongs to one class. If the lines disagree, it belongs to the other.

The data generator creates 100 random points inside a 20x20 unit square. For each point, it checks which side of each line the point is on. That combination decides blue or pink.

No single straight line can separate this pattern. The network has to learn a curved, nonlinear border. That is why hidden layers matter. Each layer lets the network represent more complex shapes.

## Watching it learn

The canvas is 500x500 pixels with the origin in the center. Each unit maps to 25 pixels. A point at (2, 3) appears at pixel (300, 175).

Blue dots use the hex color #616aff. Pink dots use #ff566f. The axes are drawn in blue as a reference. Training points are small circles. Clicked points are the same size.

The error display updates after each training iteration. It shows the average absolute error across all training points. Watching that number fall tells you right away that the network is learning.

## Stuck at 50 percent

Backpropagation took several attempts. Matrix sizes must line up perfectly. One transpose in the wrong place breaks everything without an error. The output looks fine but never improves.

The first version updated the weights wrong. The error dropped at first, then stopped at 50%. That is random chance. The network was not learning at all. I worked through the math by hand and found a missing derivative term.

The starting weights mattered too. Weights that were too large made the neurons saturate. The sigmoid outputs were all 0s or all 1s, the gradients vanished, and learning stopped. Starting the weights between -1 and 1 avoided this.

## Backpropagation is just calculus

Building a neural network from scratch made me understand every piece. Matrix multiplication is not magic. Backpropagation is just calculus. The learning comes from simple arithmetic repeated thousands of times.

Three layers with fewer than 20 neurons can learn surprisingly complex borders. You do not always need a deep network. Many problems work fine with a shallow one.

The biggest lesson was how gradients flow backward. Each layer's error depends on the layers after it. That chain is why it is called backpropagation. Understanding it at the math level made modern frameworks much clearer to me later.

Seeing the results also made debugging easier. Dots on a canvas show right away if the predictions make sense. The error plot shows if learning is happening. Without the canvas, I would have spent much longer hunting bugs.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
