我刚刚开始研究神经网络。在解决XOR问题时,我遇到了一个总是给我0.5的问题
import random
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def dot(l1, l2):
s = 0
for i in range(len(l1)):
s += l1[i] * l2[i]
return s
INPUTS = [[0, 0], [1, 1], [0, 1], [1, 0]]
OUTPUT = [0, 0, 1, 1]
weights = [random.random(), random.random()]
for _ in range(10_000):
for i in range(len(INPUTS)):
layer_input = INPUTS[i]
output = sigmoid(dot(layer_input, weights))
err = OUTPUT[i] - output
delta = err * (output * (1 - output))
grad1 = layer_input[0] * delta
grad2 = layer_input[1] * delta
weights[0] += grad1
weights[1] += grad2
print(sigmoid(dot([0, 0], weights))) # 0.5
尝试使用 numpy,同样的事情
有必要增加神经元的层数