← 返回卷宗
數據預測

使用AForge.NET中的BP神經網絡庫實現異或邏輯

神經網絡

  調試神經網絡時,感覺無論怎麼測試數據,收斂都非常慢,總懷疑是不是AForge.Net的BP算法有問題,所以測試一下異或邏輯,代碼如下:

   private static void testXOR(){
 var func = new AForge.Neuro.SigmoidFunction();
 var network = new AForge.Neuro.ActivationNetwork(func,2,2,1);
 AForge.Neuro.Learning.BackPropagationLearning bp = new AForge.Neuro.Learning.BackPropagationLearning (network);

 double[][] input = new double[4][];
 input [0] = new double[]{ 0, 0 };
 input [1] = new double[]{ 0, 1 };
 input [2] = new double[]{ 1, 0 };
 input [3] = new double[]{ 1, 1 };
 double[][] output = new double[4][];
 output[0] = new double[]{0};
 output[1] = new double[]{1};
 output[2] = new double[]{1};
 output[3] = new double[]{0};

 double error=1;
 while(error>0.01){
 error = bp.RunEpoch(input,output);
 Console.WriteLine("error:"+error);
 }

 for (int i = 0; i < 4; i++) {
 var result =  network.Compute (input [i]);
 Console.WriteLine(string.Format("output:{0},realresult:{1}",result[0],output[i][0]));
 }
 }

  輸出結果如下:

xor

 

 測試了一下,只要有個隱藏層神經元為2,就足夠學習到異或邏輯,而為1,很難收斂,隱藏神經元為20以上時,數值變化已經在小數點後的十幾位了,也很難收斂,這說明並不是神經元越多就越好,同時增加層數時也會發現這種情況。

 看來不收斂的問題還是出在自己的參數上,並不是庫有問題。

  

  

本文由 三符道長 撰於 2015年5月12日。轉載請註明出處。