我想知道為什么我的神經(jīng)網(wǎng)絡(luò)不起作用。我想說(shuō)我對(duì)此提出了類似的問(wèn)題,但我仍然有一些我不明白的事情......代碼:import numpy as npinputs = np.array([ [[0],[0]], [[1],[0]], [[0],[1]], [[1],[1]]])expected_output = np.array([ [0], [1], [1], [0]])epochs = 100lr = 0.2hidden_weights = np.array([ [0.2, 0.3], [0.4, 0.5]])hidden_bias = np.array([[0.3], [0.6]])output_weights = np.array([[0.6, 0.7]])output_bias = np.array([[0.5]])def sigmoid(z): return 1/(1+np.exp(-z))def sigmoid_derivative(z): return z * (1.0-z)for _ in range(epochs): for index, input in enumerate(inputs): hidden_layer_activation = np.dot(hidden_weights, input) hidden_layer_activation += hidden_bias hidden_layer_output = sigmoid(hidden_layer_activation) output_layer_activation = np.dot(output_weights, hidden_layer_output) output_layer_activation += output_bias predicted_output = sigmoid(output_layer_activation) #Backpropagation output_errors = expected_output[index] - predicted_output hidden_errors = output_weights.T.dot(output_errors) d_predicted_output = output_errors * sigmoid_derivative(predicted_output) d_hidden_layer = hidden_errors * sigmoid_derivative(hidden_layer_output) output_weights += np.dot(d_predicted_output, hidden_layer_output.T) * lr hidden_weights += np.dot(d_hidden_layer, input.T) * lr output_bias += np.sum(d_predicted_output) * lr hidden_bias += np.sum(d_hidden_layer) * lr# NOW THE TESTING,I pass 2 input neurons. One with value 1 and value 1test = np.array([ [[1], [1]]])我已經(jīng)測(cè)試了前饋傳播,它工作正常。錯(cuò)誤似乎很好。我認(rèn)為更新權(quán)重是問(wèn)題,但更新權(quán)重有正確的公式。這段代碼來(lái)自《制作你自己的神經(jīng)網(wǎng)絡(luò)》一書(shū),它與我使用的幾乎相同:self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))目前我當(dāng)時(shí)只轉(zhuǎn)發(fā) 2 個(gè)神經(jīng)元的 1 個(gè)輸入并計(jì)算錯(cuò)誤。我非常希望它保持這種狀態(tài),而不是一遍又一遍地轉(zhuǎn)發(fā)整個(gè)測(cè)試數(shù)據(jù)。有什么辦法可以做到嗎?先感謝您 :)
1 回答

紅糖糍粑
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
你有一個(gè)小的實(shí)現(xiàn)錯(cuò)誤:
在反向傳播中,您評(píng)估:
hidden_errors = output_weights.T.dot(output_errors)
但是您的隱藏錯(cuò)誤必須根據(jù) d_predicted_output 進(jìn)行評(píng)估,如下所示:
hidden_errors = output_weights.T.dot(d_predicted_output)
此外,您應(yīng)該降低學(xué)習(xí)率并增加 epoch 數(shù)。10000 epochs 和 lr = 0.1 對(duì)我有用,但你可以微調(diào)它。
添加回答
舉報(bào)
0/150
提交
取消