我理解感知器只能在线性可分离的集合上正常工作,例如NAND,AND,OR函数的输出。我一直在阅读Wikipedia's entry on the perceptron,并开始使用它的代码。
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)
The output for this Ideone run is correct for XOR。怎么会?
xor
input: (1, 1), output: 0
input: (0, 1), output: 1
input: (1, 0), output: 1
input: (1, 1), output: 0
答案 0 :(得分:6)
您将t_s
输入train_perceptron
并返回,而不进行修改。然后输出它。当然这很有效......
t_s = train_perceptron(threshold, learning_rate, w, t_s)
这根本不会改变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)
答案 1 :(得分:1)
尝试更改训练集:
t_s = [((1, 1, 1), 0), ((1, 0, 1), 1), ((1, 1, 0), 1), ((0, 0, 0), 0)]
答案 2 :(得分:0)
如果我的记忆是正确的,以破坏感知器的非线性问题,你需要至少一个隐藏层,对该层中的神经元进行非线性激活。