why does Wikipedia's perceptron correctly separate XOR?
我知道感知器只能在线性可分集上正确工作,例如 NAND、AND、OR 函数的输出。我一直在阅读维基百科关于感知器的条目,并开始使用它的代码。
XOR 是一种单层感知器应该失败的情况,因为它不是一个线性可分集。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #xor print ("xor") t_s = [((1, 1, 1), 0), ((1, 0, 1), 1), ((1, 1, 0), 1), ((1, 1, 1), 0)] threshold = 0.5 learning_rate = 0.1 w = [0, 0, 0] def dot_product(values, weights): return sum(value * weight for value, weight in zip(values, weights)) def train_perceptron(threshold, learning_rate, weights, training_set): while True: #print('-' * 60) error_count = 0 for input_vector, desired_output in training_set: #print(weights) result = dot_product(input_vector, weights) > threshold error = desired_output - result if error != 0: error_count += 1 for index, value in enumerate(input_vector): weights[index] += learning_rate * error * value if error_count == 0: #iterate till there's no error break return training_set t_s = train_perceptron(threshold, learning_rate, w, t_s) t_s = [(a[1:], b) for a, b in t_s] for a, b in t_s: print"input:" + str(a) +", output:" + str(b) |
此 Ideone 运行的输出对于 XOR 是正确的。怎么来的?
1 2 3 4 5 | xor input: (1, 1), output: 0 input: (0, 1), output: 1 input: (1, 0), output: 1 input: (1, 1), output: 0 |
你把
1 | t_s = train_perceptron(threshold, learning_rate, w, t_s) |
这根本不会改变
然后在这里输出:
1 2 3 4 | t_s = [(a[1:], b) for a, b in t_s] for a, b in t_s: print"input:" + str(a) +", output:" + str(b) |
尝试更改您的训练集:
1 | t_s = [((1, 1, 1), 0), ((1, 0, 1), 1), ((1, 1, 0), 1), ((0, 0, 0), 0)] |
如果我的记忆是正确的,可以用感知器解决非线性问题,那么您至少需要一个隐藏层,该层中的神经元具有非线性激活功能。