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:
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.
