Perceptron Coding Challenge


Perceptron Coding Challenge

Corey
Alright, Hope, today we're tackling the perceptron—the simplest artificial neural network. It's basically a model of a single neuron, invented by Frank Rosenblatt back in 1957.

Hope
So it's like a building block for all the fancy neural networks we hear about? What does it actually do?

Corey
Exactly—it's the simplest unit that can learn to classify things. Imagine you have a bunch of points on a 2D grid split by a line. The perceptron looks at a point's coordinates and decides whether it's above or below that line. It outputs either +1 for one class or -1 for the other.

Hope
Okay, so how does it make that decision? What's the math inside?

Corey
It's pretty straightforward. Each input—like the x and y coordinates—gets multiplied by a weight. Then you add up all those weighted inputs. That sum goes through an activation function, which in this case is just the sign function: if the sum is positive, output +1; if it's negative, output -1. That's the feed-forward step.

Hope
So the weights determine the decision boundary. But how do we find the right weights?

Corey
That's where supervised learning comes in. We give the perceptron a bunch of points with known correct answers. It makes a guess, we compare guess to answer, and that gives us an error. Then we tweak the weights to reduce that error.

Hope
How exactly do we tweak them? Is there a formula?

Corey
Yes—it's a simple gradient descent update. For each weight, the change is: error times the corresponding input, times a small learning rate. So if the perceptron guessed +1 but the answer was -1, the error is -2, and we adjust weights to push future outputs toward -1.

Hope
And the learning rate stops it from overcorrecting, right? Like steering a car—you don't crank the wheel full lock.

Corey
Exactly! In the demo I coded in Processing, I started with random weights. Without training, nearly all points were classified wrong. But after a few cycles of training on the whole dataset, it converged to 100% accuracy. You could even click to step through training and see the errors shrink.

Hope
But I remember there was a big problem: what if the point is at (0,0)? No matter the weights, the weighted sum is 0, so the sign function always outputs +1. That means it can't learn certain decision boundaries.

Corey
Right—that's the missing piece: a bias input. We add a third input that's always 1, with its own weight. That lets the perceptron learn an offset, like the y-intercept in the line equation y = mx + b. Without bias, the decision line always passes through the origin, which is way too restrictive.

Hope
So the bias weight acts like the y-intercept, and the other weights act like the slope. The perceptron is essentially doing linear regression with gradient descent.

Corey
Exactly. And that's why it's such a limited model—it can only learn linear boundaries. But it's the perfect starting point for building more complex networks. In the next video, I'll add the bias and visualize the learned line.

Ask anything
We've read this whole study set, so questions about it get the best answers.