我正在嘗試XOR用 Java構(gòu)建一個(gè)基本的神經(jīng)網(wǎng)絡(luò)來計(jì)算邏輯函數(shù)。該網(wǎng)絡(luò)有兩個(gè)輸入神經(jīng)元,一個(gè)包含三個(gè)神經(jīng)元的隱藏層和一個(gè)輸出神經(jīng)元。但是經(jīng)過幾次迭代后,輸出中的誤差變?yōu)镹aN。我已經(jīng)瀏覽了其他實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)的實(shí)現(xiàn)和教程,但我找不到錯(cuò)誤。我覺得問題在于我的落后功能。請(qǐng)幫助我理解我哪里出錯(cuò)了。我的代碼: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貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
因此,這可能不是導(dǎo)致您出現(xiàn)問題的原因,但我注意到:
W1.get(i,j) + LEARNING_RATE*W1_delta.get(i, 0));
當(dāng)您更新權(quán)重時(shí)。我認(rèn)為正確的公式是:
所以你的代碼應(yīng)該是:
W1(i,j) += LEARNING_RATE * W1_delta.get(i, 0) * <output from the connected node>;
它可能無法解決它,但值得一試!

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
嘗試使用較低的學(xué)習(xí)率。當(dāng)錯(cuò)誤出現(xiàn)時(shí)NaN
,通常意味著您的成本/錯(cuò)誤函數(shù)已經(jīng)爆炸。嘗試范圍內(nèi)的東西[10^-3, 10^-5]
。
添加回答
舉報(bào)
0/150
提交
取消