Teach a neuron AND
One artificial neuron learns a rule from examples, one mistake at a time.
A perceptron is the smallest possible neural network: it multiplies its inputs by weights, adds a bias, and answers 1 if the total is above 0, otherwise 0. When it gets an example wrong, it nudges its weights toward the right answer.
Write trainPerceptron(examples, rate, epochs): examples is an array of [x1, x2, target] with target 0 or 1. Start with w1 = 0, w2 = 0, b = 0. For each epoch, go through the examples in order. Predict 1 if w1 * x1 + w2 * x2 + b > 0, otherwise 0. Let err = target - prediction, then update w1 += rate * err * x1, w2 += rate * err * x2, b += rate * err. Return [w1, w2, b] after all epochs.
Count the mistakes in each epoch and call show(points), where points is the [epoch, mistakes] pairs so far, starting at epoch 0. The stage draws the mistakes dropping toward zero.
Example: trainPerceptron([[1, 1, 1]], 1, 1) returns [1, 1, 1].
Tests you can see
trainPerceptron([[1,1,1]], 1, 1)[1,1,1]trainPerceptron([[0,0,0],[0,1,0],[1,0,0],[1,1,1]], 0.1, 10)[0.2,0.1,-0.2]trainPerceptron([[0,0,0],[0,1,1],[1,0,1],[1,1,1]], 0.5, 5)[0.5,0.5,0]
More tests are hidden. The server runs them all before a time counts.
Call show() while your code works and the run plays back here.
Tab indents · Cmd/Ctrl+Enter runs