调试神经网络时,感觉无论怎么测试数据,收敛都非常慢,总怀疑是不是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]));
}
}
输出结果如下:
测试了一下,只要有个隐藏层神经元为2,就足够学习到异或逻辑,而为1,很难收敛,隐藏神经元为20以上时,数值变化已经在小数点后的十几位了,也很难收敛,这说明并不是神经元越多就越好,同时增加层数时也会发现这种情况。
看来不收敛的问题还是出在自己的参数上,并不是库有问题。
Leave a Reply