Backprop on XOR (optional reinforcement of Q9). Train a 2-2-1 sigmoid net; print the falling sum-squared error and confirm it learns XOR. Seed is fixed, so output is deterministic.
Backprop on XOR (optional reinforcement of Q9). Train a 2-2-1 sigmoid net; print the falling sum-squared error and confirm it learns XOR. Seed is fixed, so output is deterministic.
Answer
import math import random random.seed(1) def sigmoid(x): return 1 / (1 + math.exp(-x)) inputs = [[0, 0], [0, 1], [1, 0], [1, 1]] targets = [0, 1, 1, 0] def rand_weights(): return [random.uniform(-1, 1) for _ in range(3)] # 2 inputs + bias w_hidden = [rand_weights(), rand_weights()] # two hidden neurons w_out = rand_weights() # output neuron (2 hidden + bias) lr = 0.5 for epoch in range(20001): sse = 0.0 for x, t in zip(inputs, targets): h = [sigmoid(x[0]*w_hidden[i][0] + x[1]*w_hidden[i][1] + w_hidden[i][2]) for i in range(2)] o = sigmoid(h[0]*w_out[0] + h[1]*w_out[1] + w_out[2]) sse += (t - o) ** 2 delta_o = (t - o) * o * (1 - o) delta_h = [h[i] * (1 - h[i]) * delta_o * w_out[i] for i in range(2)] w_out[0] += lr * delta_o * h[0] w_out[1] += lr * delta_o * h[1] w_out[2] += lr * delta_o for i in range(2): w_hidden[i][0] += lr * delta_h[i] * x[0] w_hidden[i][1] += lr * delta_h[i] * x[1] w_hidden[i][2] += lr * delta_h[i] if epoch in (0, 2000, 8000, 20000): print(f"epoch {epoch:5d} SSE={sse:.3f}") predictions = [] for x in inputs: h = [sigmoid(x[0]*w_hidden[i][0] + x[1]*w_hidden[i][1] + w_hidden[i][2]) for i in range(2)] o = sigmoid(h[0]*w_out[0] + h[1]*w_out[1] + w_out[2]) predictions.append(1 if o > 0.5 else 0) print(f"XOR rounded: {predictions}") print("learned XOR:", "YES" if predictions == targets else "NO")