为什么维基百科的感知器正确地将XOR分开?

and*_*and 2 python perceptron neural-network

我理解感知器只能在线性可分离的集合上正常工作,比如NAND,AND和OR函数的输出.我一直在阅读维基百科关于感知器的条目,并开始使用它的代码.

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)
Run Code Online (Sandbox Code Playgroud)

此Ideone运行的输出对于XOR是正确的.怎么会?

xor
input: (1, 1), output: 0
input: (0, 1), output: 1
input: (1, 0), output: 1
input: (1, 1), output: 0
Run Code Online (Sandbox Code Playgroud)

Nab*_*bla 6

您输入t_strain_perceptron并返回它,而无需修改.然后输出它.当然这很完美......

t_s = train_perceptron(threshold, learning_rate, w, t_s)
Run Code Online (Sandbox Code Playgroud)

这根本不会改变t_s.train_perceptron没有任何修改training_set,.但返回它:return training_set

然后在这里输出它:

t_s = [(a[1:], b) for a, b in t_s]

for a, b in t_s:
    print "input: " + str(a) + ", output: " + str(b)
Run Code Online (Sandbox Code Playgroud)