1

我尝试使用具有 2 个输入、2 个隐藏神经元、1 个输出神经元(使用随机梯度下降)的 NN 编写经典 XOR 问题。但是无论我做什么,我的神经网络都不能正常工作,仍然得到错误的输出,我真的不知道问题出在哪里,我认为在反向传播中,也许我以错误的顺序乘以它,我真的不知道。我用于更新输出权重的公式是 LEARING_RATE * ERROR * (OUTPUT * (1 - OUTPUT) * HIDDEN_OUTPUT.T,隐藏权重的公式是 LEARING_RATE * HIDDEN_ERROR * (HIDDEN_OUTPUT * (1 - HIDDEN_OUTPUT) * INPUT。我会想知道我的代码中的问题出在哪里,如果您能提供任何帮助,我将不胜感激:)

def sigmoid(x):
    return 1 / (1 + np.exp(-x))


def sigmoid_derivative(y):
    return np.exp(y) * (1 - np.exp(y))


inp = np.array([ [[0, 0]],
                 [[0, 1]],
                 [[1, 0]],
                 [[1, 1]] ])

w_ih = np.random.uniform(size=(2, len(inp[0][0])))  # weights from input to hidden

bias_hidden = np.random.uniform(size=(1, len(w_ih)))

w_ho = np.random.uniform(size=(1, len(w_ih)))       # weights from hidden to output

bias_output = np.random.uniform(size=(1, len(w_ho)))

des_output = [[0, 1, 1, 0]]

r = 0.1 # learning rate

for i in range(10000):
    index_sample = random.randint(0, len(inp) - 1)

    # Forward propagation
    hidden = sigmoid(np.dot(w_ih, inp[index_sample].T) + bias_hidden.T)
    output = sigmoid(np.dot(w_ho, hidden) + bias_output)
    # Calculating error
    output_error = output - np.array(des_output).T[index_sample]
    hidden_error = np.dot(w_ho.T, output_error)

    # Back propagation
    output_gradient = output_error * sigmoid_derivative(output)
    w_ho += np.dot(output_gradient, hidden.T) * r

    bias_output += np.sum(output_gradient, axis=0, keepdims=True) * r


    hidden_gradient = hidden_error * sigmoid_derivative(hidden)
    w_ih += np.dot(hidden_gradient, inp[index_sample]) * r

    bias_hidden += np.sum(np.array(hidden_gradient), axis=0, keepdims=True) * r


test = np.array([ [[0, 0]],
                  [[0, 1]],
                  [[1, 0]],
                  [[1, 1]] ])
for i in test:
    h1_test = sigmoid(np.dot(w_ih, np.array(i).T) + bias_hidden.T)      # testing neural newtwork
    output_test = sigmoid(np.dot(w_ho, h1_test) + bias_output)

    print(output_test, i)
4

0 回答 0