我正在嘗試XOR用 Java構建一個基本的神經網絡來計算邏輯函數。該網絡有兩個輸入神經元,一個包含三個神經元的隱藏層和一個輸出神經元。但是經過幾次迭代后,輸出中的誤差變為NaN。我已經瀏覽了其他實現神經網絡的實現和教程,但我找不到錯誤。我覺得問題在于我的落后功能。請幫助我理解我哪里出錯了。我的代碼:import org.ejml.simple.SimpleMatrix;import java.util.ArrayList;import java.util.List;import java.util.Random;// SimpleMatrix constructor format: SimpleMatrix(rows, cols)//The layers are represented as a matrix with 1 row and multiple columns (row vector)public class Network { private SimpleMatrix inputs, outputs, hidden, W1, W2, predicted; static final double LEARNING_RATE = 0.3; Network(List<double[]> ips, List<double[]> ops){ hidden = new SimpleMatrix(1, 3); W1 = new SimpleMatrix(ips.get(0).length, hidden.numCols()); W2 = new SimpleMatrix(hidden.numCols(), ops.get(0).length); initWeights(W1,W2); for(int i=0;i<5000;i++){ for(int j=0;j<ips.size();j++){ train(ips.get(j), ops.get(j)); } } System.out.println("Trained"); } //Prints output matrix SimpleMatrix predict(double[] ip){ SimpleMatrix bkpInputs = inputs.copy(); SimpleMatrix bkpOutputs = outputs.copy(); inputs = new SimpleMatrix(1, ip.length); inputs.setRow(0, 0, ip); forward(); inputs = bkpInputs; outputs = bkpOutputs; predicted.print(); return predicted; } void train(double[] inputs, double[] outputs){ this.inputs = new SimpleMatrix(1, inputs.length); this.inputs.setRow(0, 0, inputs); this.outputs = new SimpleMatrix(1, outputs.length); this.outputs.setRow(0,0,outputs); this.predicted = new SimpleMatrix(1,outputs.length); forward(); backward(); }
2 回答

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
因此,這可能不是導致您出現問題的原因,但我注意到:
W1.get(i,j) + LEARNING_RATE*W1_delta.get(i, 0));
當您更新權重時。我認為正確的公式是:
所以你的代碼應該是:
W1(i,j) += LEARNING_RATE * W1_delta.get(i, 0) * <output from the connected node>;
它可能無法解決它,但值得一試!
添加回答
舉報
0/150
提交
取消