← Back to archive
Data Forecasting

Implementing XOR Logic Using the BP Neural Network Library in AForge.NET

神经网络

  When debugging the neural network, I felt that no matter how I tested the data, the convergence was very slow. I always suspected that there was a problem with AForge.Net's BP algorithm, so I tested the XOR logic with the following code:

   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]));
 }
 }

  The output result is as follows:

xor

 

 After testing, as long as there are 2 neurons in the hidden layer, it is enough to learn the XOR logic. When it is 1, it is difficult to converge. When there are more than 20 hidden neurons, the numerical changes are already in the dozen or so digits after the decimal point, and it is also difficult to converge. This shows that more neurons are not necessarily better, and this situation also occurs when increasing the number of layers.

 It seems that the problem of non-convergence lies in my own parameters, not that there is a problem with the library.

  

  

Written by Master Sanfu on May 12, 2015. Please credit the source if you share.

Translation Notice: This English version was translated with AI assistance. Specialized, historical, religious, or culturally sensitive terms may contain nuances, inaccuracies, or debatable wording. In case of ambiguity or discrepancy, the original Chinese text shall prevail.