public static void train(DataSet X) { if (Global.mode == "test") { Console.WriteLine("begain testing......"); testAccuracy1 = runtestIteration(X.Testing, false); Console.WriteLine("Epoch test f-score: {0}", (testAccuracy1 * 100).ToString("f3")); //Console.WriteLine("Epoch test best f-score: {0}", (testAccuracy * 100).ToString("f3")); Global.swLog.WriteLine("Epoch test fscore: {0}", (testAccuracy1 * 100).ToString("f3")); //Global.swLog.WriteLine("Epoch test best fscore: {0}", (testAccuracy * 100).ToString("f3")); Postprocessing.transfer("data/temp/" + "test_raw.txt"); Console.WriteLine("Finished"); } //else if (Global.mode == "test") //{ // Console.WriteLine("begain testing......"); // testAccuracy1 = runtestIteration(X.Testing, false); // Postprocessing.transfer(Global.readFile); // Console.WriteLine("predict fininshed"); // Global.swLog.WriteLine("predict fininshed"); //} else if (Global.mode == "train") { for (int iter = 0; iter < Global.trainIter; iter++) { DateTime begin = DateTime.Now; Console.WriteLine("\niter: {0}", iter + 1); Global.swLog.WriteLine("\niter: {0}", iter + 1); double trainAccuracy = runtrainIteration(X.Training, X.Testing, true, iter); if (double.IsNaN(trainAccuracy) || double.IsInfinity(trainAccuracy)) { Console.WriteLine("WARNING: invalid value for training loss. Try lowering learning rate."); } //test testAccuracy1 = runtestIteration(X.Testing, false); if (testAccuracy <= testAccuracy1) { LSTMLayer.SerializeWordembedding("model//deepNetwork//embedding"); //LSTMLayer.SerializeBigramWordembedding(); Global.upLSTMLayer.saveLSTM("model//deepNetwork//lstmmodel.txt"); Global.upLSTMLayerr.saveLSTM("model//deepNetwork//lstmmodelr.txt"); Global.GRNNLayer1.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.GRNNLayer2.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.GRNNLayer3.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.GRNNLayer4.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.feedForwardLayer.saveFFmodel("model//deepNetwork//feedforwardmodel.txt"); testAccuracy = testAccuracy1; } DateTime end = DateTime.Now; // Console.WriteLine("train f-score: {0}", (trainAccuracy*100).ToString("f3")); Console.WriteLine("test f-score: {0}", (testAccuracy * 100).ToString("f3")); Console.WriteLine("test f-score: {0}", (testAccuracy1 * 100).ToString("f3")); Console.WriteLine("time used: {0}", end - begin); //Global.swLog.WriteLine("train f-score: {0}", (trainAccuracy * 100).ToString("f3")); Global.swLog.WriteLine("test f-score: {0}", (testAccuracy * 100).ToString("f3")); Global.swLog.WriteLine("test f-score: {0}", (testAccuracy1 * 100).ToString("f3")); Global.swLog.WriteLine("time used: {0}", end - begin); } } }
public static double runtrainIteration(List <DataSeq> X, List <DataSeq> Xtest, bool train, int iter) { List <DataSeq> x = new List <DataSeq>(); if (train) { x = shuffle(X);//shuffle every window (point) } TrainThread runThread = new TrainThread(train); List <ManualResetEvent> manualEvents = new List <ManualResetEvent>(); List <DataStep> temp = new List <DataStep>(); int i = 0, j = 0; int length = x.Count(); while (i < length) { if (i != 0 && i % 16 == 0) { testAccuracy1 = runtestIteration(Xtest, false); if (testAccuracy <= testAccuracy1) { LSTMLayer.SerializeWordembedding("model//deepNetwork//embedding"); //LSTMLayer.SerializeBigramWordembedding(); Global.upLSTMLayer.saveLSTM("model//deepNetwork//lstmmodel.txt"); Global.upLSTMLayerr.saveLSTM("model//deepNetwork//lstmmodelr.txt"); Global.GRNNLayer1.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.GRNNLayer2.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.GRNNLayer3.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.GRNNLayer4.saveGRNN("model//deepNetwork//grnnmodel1.txt"); Global.feedForwardLayer.saveFFmodel("model//deepNetwork//feedforwardmodel.txt"); testAccuracy = testAccuracy1; } Console.WriteLine("test f-score: {0}", (testAccuracy * 100).ToString("f3")); Console.WriteLine("test1 f-score: {0}", (testAccuracy1 * 100).ToString("f3")); Global.swLog.WriteLine("test f-score: {0}", (testAccuracy * 100).ToString("f3")); Global.swLog.WriteLine("test f-score: {0}", (testAccuracy1 * 100).ToString("f3")); } for (int k = 0; k < Global.nThread && i < length; k++, i++) { ManualResetEvent mre = new ManualResetEvent(false); param pa = new param(x[i]); pa.mre = mre; pa.datastep = x[i].datasteps; manualEvents.Add(mre); for (int m = 0; m < x[i].datasteps.Count; m++) { temp.Add(x[i].datasteps[m]); } ThreadPool.QueueUserWorkItem(new WaitCallback(runThread.run), pa); } WaitHandle.WaitAll(manualEvents.ToArray()); if (train) { UpdateWeight_rmProp(temp); } manualEvents.Clear(); temp.Clear(); } return(runThread.accword / runThread.totalword); }