了解到运用AForge对在c#中实现神经网络已经比较成熟了,最近做分类要用到,看了下代码,基本会用了。流程是先训练网络然后对数据进行识别。现在出现一个情况,我的样本数比较多的时候,训练网络要花比较长的时间,所以不想每次都训练结束后才能进行识别。AForge能不能把训练后的网络保存下来,然后直接调用?
谁知道AForge有没有保存网络再调用的相关用法。
难道真的要自己用C#写的神经网络才能保存嘛。。。
运用的例子在下面:
//整理输入输出数据
double[][] input = new double[4][]; double[][] output = new double[4][];
input[0] = new double[] { 0, 0 }; output[0] = new double[] { 0 };
input[1] = new double[] { 0, 1 }; output[1] = new double[] { 0 };
input[2] = new double[] { 1, 0 }; output[2] = new double[] { 0 };
input[3] = new double[] { 1, 1 }; output[3] = new double[] { 1 };
for (int i = 0; i < 4; i++)
{
Console.WriteLine("input{0}: ===> {1},{2} output{0}: ===> {3}",i,input[i][0],input[i][1],output[i][0]);
}
//建立网络,层数1,输入2,输出1,激励函数阈函数
ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, 1);
//学习方法为感知器学习算法
PerceptronLearning teacher = new PerceptronLearning(network);
//定义绝对误差
double error = 1.0;
Console.WriteLine();
Console.WriteLine("learning error ===> {0}", error);
//输出学习速率
Console.WriteLine();
Console.WriteLine("learning rate ===> {0}",teacher.LearningRate);
//迭代次数
int iterations = 0;
Console.WriteLine();
while (error > 0.001)
{
error = teacher.RunEpoch(input, output);
Console.WriteLine("learning error ===> {0}", error);
iterations++;
}
Console.WriteLine("iterations ===> {0}", iterations);
Console.WriteLine();
Console.WriteLine("sim:");
//模拟
for (int i = 0; i < 4; i++)
{
Console.WriteLine("input{0}: ===> {1},{2} sim{0}: ===> {3}", i, input[i][0], input[i][1], network.Compute(input[i])[0]);
}
参考资料:
http://www.cnblogs.com/htynkn/archive/2012/02/07/AForge_5.html
下面直接用c#写的BP网络 能实现保存,加载:
http://blog.csdn.net/jiutao_tang/article/details/6598488