为什么Wikipedia的感知器正确地分离了XOR?

2024-06-16 09:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道感知器只能在线性可分离集上正确工作,比如NAND、AND、OR函数的输出。我一直在读Wikipedia's entry on the perceptron,并开始研究它的代码。在

当它是一个可分离的层时,它不应该是线性的。在

#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)

The output for this Ideone run is correct for XOR。怎么会?在

^{pr2}$

Tags: inforinputoutputthresholdratevaluecount
3条回答

如果我的记忆是正确的打破非线性问题的感知器你需要至少一个隐藏层的非线性激活的神经元在这一层。在

您将t_s输入到train_perceptron中并返回它而不进行修改。然后输出。当然,那是完美的。。。。在

t_s = train_perceptron(threshold, learning_rate, w, t_s)

这根本不会改变t_strain_perceptron根本没有修改training_set,。但返回它:return training_set

然后在这里输出:

^{pr2}$

试着改变你的训练计划:

t_s = [((1, 1, 1), 0), ((1, 0, 1), 1), ((1, 1, 0), 1), ((0, 0, 0), 0)]

相关问题 更多 >