Perceptron Learning Simulator Back
Machine Learning Simulator

Perceptron Learning Simulator — Linear Classifier Convergence

Visualize the learning rule of a 2-input single-layer perceptron y = sign(w1 x1 + w2 x2 + b). Watch how the decision boundary converges on linearly separable data while you tune the learning rate and initial weights.

Parameters
Learning rate η
Epochs
ep
w_1 initial value
b initial value

The initial value of w_2 is fixed at 0.5. Data are generated by a fixed-seed LCG (seed = 42), so the same 20 points appear every time you redraw.

Results
Final accuracy
Learned w_1
Learned w_2
Convergence epoch
Final decision boundary

Blue dots = class +1 / red crosses = class -1 / green solid = learned boundary / gray dashed = true boundary x1 + x2 = 0

Theory & Key Formulas

A single-layer perceptron is a 2-input linear threshold unit whose output is the sign of a weighted sum:

$$y = \operatorname{sign}(w_1 x_1 + w_2 x_2 + b)$$

Update rule (Perceptron Learning Algorithm, PLA). $t \in \{+1,-1\}$ is the target and $y$ is the model output:

$$w_i \leftarrow w_i + \eta\,(t - y)\,x_i,\quad b \leftarrow b + \eta\,(t - y)$$

The decision boundary is $w_1 x_1 + w_2 x_2 + b = 0$, or for plotting:

$$x_2 = -\frac{w_1 x_1 + b}{w_2}$$

The margin, the signed distance from a point to the boundary, is normalized by the weight norm:

$$\gamma = \frac{w \cdot x_{\text{target}} + b}{\|w\|}$$

If the data are linearly separable, the perceptron convergence theorem guarantees that a separating hyperplane is reached in a finite number of updates.

What is the Perceptron Learning Simulator

🙋
"Perceptron" is one of the first words you meet in any neural network course. Is it still worth studying today?
🎓
Yes — and skipping it makes everything that comes later in deep learning look like a magic spell. The single-layer perceptron is the smallest possible model: just "take the sign of a weighted sum of two inputs to classify them into two classes". The update rule fits on one line, $w \leftarrow w + \eta(t-y)x$. The "final accuracy" card above shows 100% because the data here are linearly separable.
🙋
When I crank the learning rate η up close to 1.0, the slope of the boundary jumps around quite a bit.
🎓
That is because each update step is too large, and a single misclassified point swings the weights a lot. On linearly separable data it does still converge, but you may need more epochs, or the boundary may settle far from the true boundary x1 + x2 = 0. In practice the standard recipe is to start with a small η (0.01–0.1) and lower it further if the oscillations are too violent.
🙋
The "convergence epoch" card shows a pretty small number with the defaults. What does that count exactly?
🎓
It is the first epoch in which a complete pass over the data produces zero misclassifications. The defaults w = (0.5, 0.5), b = 0 already separate the data cleanly, so you typically hit zero errors on the first pass. If you set w_1 initial = -2, the model starts by classifying the red crosses and blue dots the wrong way, so you can watch the boundary need a few epochs to flip.
🙋
So "learning" is essentially nudging a wild guess closer and closer to the right answer?
🎓
Exactly, and it does so very frugally — it only updates when there is a misclassification. This is the prototype of online learning, and it is strong on streaming data. The 1958 Rosenblatt algorithm is in direct lineage with modern SGD (stochastic gradient descent). Sliding the sliders here and watching the boundary move gives you a small taste of what a deep learning optimizer actually feels like.

Frequently Asked Questions

No. XOR is not linearly separable, so the weights oscillate forever and the decision boundary never converges. Minsky and Papert pointed out this limitation in their 1969 book Perceptrons, which triggered the first AI winter for neural networks. A multi-layer perceptron (MLP) with one hidden layer can represent XOR by nonlinearly transforming the feature space.
A single-layer perceptron can only solve linearly separable problems, while adding one or more hidden layers allows it to approximate any continuous function to arbitrary precision (the universal approximation theorem). Training is done by backpropagation, and the step activation is replaced by sigmoid or ReLU. Today's deep learning is essentially this multi-layer structure made even deeper.
Both seek a linear separating hyperplane, but with different optimization targets. The perceptron just finds some separating hyperplane, and the solution depends on the order in which data are presented and on the initialization. An SVM finds the hyperplane that maximizes the margin (distance from the boundary to the nearest data points), so its solution is unique and tends to generalize better. SVMs also extend to nonlinear problems via the kernel trick.
The main reason is that the data are not linearly separable. The perceptron convergence theorem then does not apply and the weights keep oscillating. Remedies are (1) checking on a scatter plot whether the data really are linearly separable, (2) using the pocket algorithm, which keeps the best weights seen so far, or (3) switching to an MLP or kernel SVM if separability fails. This simulator uses a fixed, linearly separable dataset, so it always reaches 100% accuracy.

Real-World Applications

Historical significance (1958–): The single-layer perceptron proposed by Frank Rosenblatt is the origin point of modern neural networks and deep learning. It was even built in hardware at the time and attracted attention as a "learning machine". The first AI winter that followed Minsky and Papert's critique was eventually broken by the move to multi-layer models and the backpropagation algorithm, leading directly to today's deep learning.

Theoretical basis for linear SVMs and logistic regression: The basic structure of "learning a hyperplane that linearly separates two classes" is shared by linear SVMs, logistic regression, linear discriminant analysis (LDA) and other linear classifiers still widely used today. Understanding the perceptron lets you place these methods on a single axis, where they differ mainly in their loss function and optimizer.

Online learning and streaming data: The perceptron is the prototype of online learning, in which weights are updated one sample at a time. It does not need to hold the whole batch in memory, so it still plays an active role in situations where data arrive sequentially — click-through prediction in ads, fraud detection, IoT sensor analytics and so on.

Education and proof of concept: The perceptron is one of the most frequently used examples in machine learning education. Within a few lines of code you can experience the same core concepts — weights, bias, learning rate, convergence, overfitting, linear separability — that still apply to today's deep models.

Common Misconceptions and Cautions

The most common misconception is to assume that "if I just train long enough, any dataset will eventually be separated". The perceptron convergence theorem only states that "if the data are linearly separable, the algorithm converges in finite time". For data that are not linearly separable, it will not converge in 100 or 10000 epochs. This simulator deliberately uses a linearly separable, fixed dataset so that it always reaches 100% accuracy, but real-world data usually require feature engineering before they can be linearly separated.

The next pitfall is to think that "a larger learning rate always converges faster" and push η close to 1.0. Larger steps are taken, yes, but a single misclassified point now swings the weights so much that the boundary needs extra epochs to stabilize, or it may settle far from the true boundary (x1 + x2 = 0). Slowly sweep η from 0.01 to 1.0 in the simulator above and compare the "learned w_1, w_2" cards and the decision boundary. The standard practice is to start small and lower further when oscillations are violent, or raise when convergence is too slow.

Finally, do not assume that "the perceptron's solution is unique". Linearly separable data admit infinitely many separating hyperplanes — as long as there is any margin, you can translate and rotate the boundary inside it. The perceptron simply returns the first weight vector that reaches zero errors, so a different presentation order or initialization can produce a different boundary. To obtain a unique "best" boundary, the standard choice is an SVM, which builds margin maximization directly into the objective.