/// <summary> /// 测试 /// </summary> void FormulaTest()// { List <Matrix <double> > C0 = new List <Matrix <double> >(), C1 = new List <Matrix <double> >(); List <Matrix <double> > Test0 = new List <Matrix <double> >(), Test1 = new List <Matrix <double> >(); var m2 = new DenseMatrix(new[, ] { { 1.0 }, { 0.5 } }); var k2 = new DenseMatrix(new[, ] { { 1.0 } }); var v2 = new DenseMatrix(new[, ] { { 3.0, 1.0 }, { 1.0, 2.0 } }); var m1 = new DenseMatrix(new[, ] { { 0.0 }, { 0.0 } }); var k1 = new DenseMatrix(new[, ] { { 1.0 } }); var v1 = new DenseMatrix(new[, ] { { 1.0, 0.0 }, { 0.0, 1.0 } }); ClassifiedData.Clear(); for (int i = 0; i < 50; i++) { C0.Add(MatrixNormal.Sample(new Random(), m2, v2, k2)); ClassifiedData.Add(new data(C0[0][0, 0], C0[0][1, 0], 0)); C1.Add(MatrixNormal.Sample(new Random(), m1, v1, k1)); ClassifiedData.Add(new data(C1[0][0, 0], C1[0][1, 0], 1)); } for (int i = 0; i < 20; i++) { Test0.Add(MatrixNormal.Sample(new Random(), m2, v2, k2)); Test1.Add(MatrixNormal.Sample(new Random(), m1, v1, k1)); TestData.Add(new data(Test0[0][0, 0], Test0[0][1, 0], 0)); C1.Add(MatrixNormal.Sample(new Random(), m1, v1, k1)); TestData.Add(new data(Test1[0][0, 0], Test1[0][1, 0], 1)); } int indicator = 0; int endNetworks = 50; int startNetworks = 1; //int seed = 0; do { indicator = 0; for (int ii = startNetworks; ii <= endNetworks; ii++) { Jobs ajob = new Jobs(ii, 10); /// 新建一个神经网络 NeuronNetworks nn = new NeuronNetworks(ajob.m);//, new Random(DateTime.Now.Millisecond)); /// 当前的迭代次数 nn.currentLearnRepeats = 0; DateTime _start = DateTime.Now; while (true) //(++nn.currentLearnRepeats < nn.maxLearnRepeats) { ++nn.currentLearnRepeats; foreach (data d in ClassifiedData) { nn.Train(d.input1, d.input2, d.output); } //if (nn.currentLearnRepeats % ajob.testpoint == 0) // recheck whether it is converge. { double errorsum = 0; for (int i = 0; i < ClassifiedData.Count; i++) { double estimateOut = nn.Display(ClassifiedData[i].input1, ClassifiedData[i].input2) - ClassifiedData[i].output; errorsum += (estimateOut * estimateOut) / 2; } if (errorsum < nn.ε) { break; } if ((DateTime.Now - _start).Seconds > ajob.timeout)//timeout { Console.WriteLine(DateTime.Now - _start); Console.WriteLine("Request timeout."); break; } } } double sum = 0; foreach (data d in TestData) { double t = (nn.Display(d.input1, d.input2) - d.output); //Console.WriteLine("{0},{1}->{2}", d.input1, d.input2, nn.Display(d.input1, d.input2)); sum += t * t; } //Console.WriteLine(DateTime.Now - _start);未知样本的总差值= if (sum > 10) { indicator++; } //Console.WriteLine("Task: m={0},testpoint={1};", ajob.m, ajob.testpoint); //Console.WriteLine("开始计算差值"); //Console.WriteLine(nn.currentLearnRepeats); sum = 0; foreach (data d in ClassifiedData) { double t = (nn.Display(d.input1, d.input2) - d.output); //Console.WriteLine("{0},{1}->{2}", d.input1, d.input2, nn.Display(d.input1, d.input2)); sum += t * t; } //Console.WriteLine(DateTime.Now - _start);样本训练后的总差值= Console.Write("{0}\t", sum); sum = 0; foreach (data d in TestData) { double t = (nn.Display(d.input1, d.input2) - d.output); //Console.WriteLine("{0},{1}->{2}", d.input1, d.input2, nn.Display(d.input1, d.input2)); //if (t > 0.7) t = 1; //else t = 0; sum += t * t; } //Console.WriteLine(DateTime.Now - _start);未知样本的总差值= Console.WriteLine("{0}", sum); //if (sum < 10) Console.ReadKey(); if (indicator >= (endNetworks - startNetworks) * 0.9) { Console.Clear(); } } } while (indicator > 5); Console.WriteLine("Press any key to exit."); if (indicator < 5) { Console.ReadKey(); } }
/// <summary> /// 线程执行的任务 /// </summary> void dojob() { Jobs ajob = new Jobs(0, 0); /// 当前已经没有待执行的任务,退出 while (AllJobs.Count > 0) { /// 否则选取一个任务 ajob = AllJobs.Pop(); /// 输出当前任务信息 Console.WriteLine("Running for m={0},testpoint={1};", ajob.m, ajob.testpoint); /// 新建一个神经网络 NeuronNetworks nn = new NeuronNetworks(ajob.m); /// 设定学习率 nn.ε = 0.01; /// 当前的迭代次数 nn.currentLearnRepeats = 0; DateTime _start = DateTime.Now; while (true) //(++nn.currentLearnRepeats < nn.maxLearnRepeats) { ++nn.currentLearnRepeats; foreach (data d in testdata) { nn.Train(d.input1, d.input2, d.output); } if (nn.currentLearnRepeats % ajob.testpoint == 0) // recheck whether it is converge. { double errorsum = 0; for (int i = 0; i < testdata.Count; i++) { double estimateOut = nn.Display(testdata[i].input1, testdata[i].input2) - testdata[i].output; errorsum += estimateOut * estimateOut; } if (errorsum < 2 * nn.ε) { break; } if ((DateTime.Now - _start).Seconds > ajob.timeout)//timeout { Console.WriteLine(DateTime.Now - _start); Console.WriteLine("Request timeout."); break; } } } Console.WriteLine("Task: m={0},testpoint={1};", ajob.m, ajob.testpoint); Console.WriteLine("开始计算差值"); Console.WriteLine(nn.currentLearnRepeats); double sum = 0; foreach (data d in testdata) { double t = (nn.Display(d.input1, d.input2) - d.output); //Console.WriteLine("{0},{1}->{2}", d.input1, d.input2, nn.Display(d.input1, d.input2)); sum += t * t; } Console.WriteLine(DateTime.Now - _start); Console.WriteLine("样本训练后的总差值={0}", sum); sum = 0; for (int i = 0; i < generated.Length; i++) { if (!generated[i]) { double a = i - i % 100; a = a / 10000; double b = i % 100; b = b / 100; double t = (nn.Display(a, b) - 1 / Math.Sqrt(1 + a * a + b * b)); sum += t * t; } } Console.WriteLine(DateTime.Now - _start); Console.WriteLine("未知样本的总差值={0}", sum); } }