Built in 2019
Linear Classifier
Perceptron Draws a Line
A perceptron that finds the line between two groups of random points, drawn live on an HTML canvas.
Made with
A hundred dots appear on the screen. Some are blue, some are red. Somewhere between them there is a straight line that splits the two colors perfectly. The computer knows where it is. The perceptron does not. Every 300 milliseconds, it draws its best guess as a black line, and you watch that line turn and slide until it finds the gap.
I built this in April 2019. A few weeks earlier, I had written a single perceptron that learned the AND logic gate. It worked, but the results only lived in the browser console as a list of numbers. I wanted to see the learning happen.
Perceptrons A single perceptron in vanilla JavaScript that learns the AND logic gate with gradient descent. A secret line
The demo needs data with a known right answer. A small DataGenerator class makes it.
First, it picks a secret line with the formula y = mx + b. The slope m and the offset b are random numbers between -5 and 5. Then it creates 100 random points with whole number coordinates between -10 and 9.
Each point gets a label. If the point is above the secret line, it is blue. If it is below, it is red. That is the whole rule, one comparison: y > mx + b.
Because the labels come from a straight line, a perfect straight answer always exists. Machine learning people call this linearly separable data. It is the one kind of problem a single perceptron can solve completely. My last commit message, in Spanish, says exactly that: “Datos separables aleatorios”, random separable data.
What the perceptron sees
The perceptron never sees the secret line. It only gets the 100 points and their colors.
It has two inputs, the x and y of a point. It has two weights, one for each input, and a bias. A weight says how much an input matters. The bias shifts the answer up or down. All three start as random numbers between -1 and 1, so the first guess is usually far from right.
For each point, the perceptron computes w1 * x + w2 * y + bias. It passes the result through a sigmoid function, which turns any number into a value between 0 and 1. Close to 1 means “blue”. Close to 0 means “red”.
Weights become a line
Here is the key idea. Those three numbers are not only a calculator. They describe a line.
The perceptron changes its mind exactly where w1 * x + w2 * y + bias = 0. On one side of that place the sum is positive, and on the other side it is negative. Solve for y and you get y = -(w1 * x + bias) / w2. That is a line on the canvas. It is called the decision boundary.
The draw method uses that formula. It computes the y value at x = -10 and at x = 10, and draws a black line between the two points. When the weights change, the line moves. Learning becomes something you can watch.
Learning, one dot at a time
A timer runs every 300 milliseconds. Each tick does three things.
- Clears the canvas and draws the 100 dots and the axes again.
- Shows every point to the perceptron once, so it can learn from each one.
- Draws the black line for the new weights.
The learning step is gradient descent, the same rule I used for the AND gate. For each point, the perceptron compares its answer to the right color. The difference is the error. It multiplies the error by the sigmoid’s derivative (how steep the curve is at that point) to get a value called delta. Then it moves each weight a small step in the direction that makes the error smaller. The bias moves the same way.
The learning rate is 0.1. It controls how big each step is. Small steps make the line move smoothly instead of jumping around.
Updating after every single point, instead of after the whole set, is called stochastic gradient descent. One pass over the data is an epoch. So the demo runs one epoch every 300 milliseconds, and you see the line after each one.
Drawing the plane
The canvas is 500 by 500 pixels. I put the origin in the center and made each unit 25 pixels wide. That maps the range from -10 to 10 onto the whole canvas.
Canvas coordinates grow downward, but math coordinates grow upward. So every y value is multiplied by -1 before drawing. Without that flip, the dots and the line would show up upside down.
The background is a light blue (#dfedff). Blue dots use #616aff and red dots use #ff566f. Thin blue axes cross at the center. The shapes come from small Rectangle, Circle and Line classes, a pattern I also used in my canvas games.
A new problem on demand
Below the canvas there is one button, “Generar”, which means “generate”. It creates a new secret line and 100 new points.
The perceptron does not start over. It keeps the weights it already learned. So the black line begins at the old answer and you see it travel to the new one. That shows something that is easy to miss in a static chart: learning is not a single event. It is a correction that never stops while there is error left.
A quiet bug
The code has a small mistake. In learnByOneInput, the step for the weights is multiplied by the learning rate twice. The bias step uses it once. So the weights learn with an effective rate of 0.01, while the bias learns with 0.1.
The demo still works. The line still finds the gap, only more slowly. That is also a lesson about machine learning. A bug in the math often does not crash anything. It only makes learning a bit worse, and you may never notice.
Where a line stops working
One perceptron can only draw one straight line. That is perfect for this demo, because I made the data that way. Real data is rarely so kind. When two groups mix in curves and islands, no single line can split them.
That limit pushed me to the next experiment. A few weeks later I built a network with hidden layers and backpropagation, so it could learn borders that bend.
Zone Classifier Classifies a point in a blue or pink zone using a vanilla neural network. The same tiny perceptrons also show up in a game. In Dino AI, 300 of them learn to play the Chrome dino game at the same time.
Dino AI An AI that learns to play the Chrome dino game. 300 tiny neural networks (perceptrons) run at once, and a genetic algorithm keeps the best players each round. Three numbers and a black line. That is all a perceptron is, and watching it move makes the math easy to follow.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build