我知道感知器只能在线性可分集合上正常工作,比如 NAND、AND、OR 函数的输出。我一直在阅读Wikipedia 关于感知器的条目,并开始使用它的代码。
XOR 是单层感知器失败的情况,因为它不是线性可分集。
#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 是正确的。怎么会?
xor
input: (1, 1), output: 0
input: (0, 1), output: 1
input: (1, 0), output: 1
input: (1, 1), output: 0