The perceptron and linear separability

The perceptron, introduced by Frank Rosenblatt in 1958, is the simplest artificial neuron. It computes a weighted sum of its inputs plus a bias and outputs 1 if the sum is above zero, and 0 otherwise. Geometrically it draws a line, or in higher dimensions a hyperplane, and classifies points by which side they fall on.

Python
def train_perceptron(data, epochs=20):
    w = [0, 0]
    b = 0
    for _ in range(epochs):
        mistakes = 0
        for (x1, x2), target in data:
            output = 1 if w[0] * x1 + w[1] * x2 + b > 0 else 0
            error = target - output
            if error:
                w[0] += error * x1
                w[1] += error * x2
                b += error
                mistakes += 1
        if mistakes == 0:
            return w, b, True
    return w, b, False


AND = [((0, 0), 0), ((0, 1), 0), ((1, 0), 0), ((1, 1), 1)]
XOR = [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 0)]
print(train_perceptron(AND))
print(train_perceptron(XOR)[2])
Read the full lesson