Train a perceptron to 100% accuracy on AND (linearly separable), then show it cannot reach 100% on XOR (not linearly separable).
Train a perceptron to 100% accuracy on AND (linearly separable), then show it cannot reach 100% on XOR (not linearly separable).
Answer
def sign(x): return 1 if x >= 0 else -1 def train(data, epochs, c=0.1): w = [0.0, 0.0, 0.0] # [bias, w1, w2] for _ in range(epochs): for x1, x2, d in data: x = [1, x1, x2] o = sign(sum(wi * xi for wi, xi in zip(w, x))) if o != d: for i in range(3): w[i] += c * (d - o) * x[i] return w def accuracy(w, data): ok = sum(1 for x1, x2, d in data if sign(w[0] + w[1] * x1 + w[2] * x2) == d) return ok / len(data) # bipolar targets: -1 = false, +1 = true AND = [(-1, -1, -1), (-1, 1, -1), (1, -1, -1), (1, 1, 1)] XOR = [(-1, -1, -1), (-1, 1, 1), (1, -1, 1), (1, 1, -1)] w_and = train(AND, epochs=20) print(f'AND learned, weights={[round(v, 1) for v in w_and]} acc={accuracy(w_and, AND)}') w_xor = train(XOR, epochs=20) print(f'XOR did not converge (acc={accuracy(w_xor, XOR)})')