Пример #1
0
        public void run(object param)
        {
            param           pa   = (param)param;
            List <DataStep> x1   = pa.datastep;
            List <Matrix>   xpro = new List <Matrix>();
            ForwdBackwdProp g    = new ForwdBackwdProp(_train);

            for (int i = 0; i < x1.Count; i++)
            {
                DataStep x = x1[i];

                List <Matrix> add = new List <Matrix>();
                for (int k = 0; k < 5; k++)
                {
                    add.Add(Global.wordEmbedding[x.inputs[k]]);
                }
                List <Matrix> returnObj2 = Global.GRNNLayer1.activate(add, g);

                List <Matrix> returnObj3 = Global.GRNNLayer2.activate(returnObj2, g);
                List <Matrix> returnObj4 = Global.GRNNLayer3.activate(returnObj3, g);
                List <Matrix> returnObj5 = Global.GRNNLayer4.activate(returnObj4, g);

                xpro.Add(returnObj5[0]);
            }

            List <Matrix> returnObj6 = Global.upLSTMLayer.activate(xpro, g);

            List <Matrix> returnObj7 = Global.upLSTMLayerr.activate(reverse(xpro), g);


            List <Matrix> sum = new List <Matrix>();

            for (int inde = 0; inde < returnObj6.Count(); inde++)
            {
                sum.Add(g.Add(returnObj6[inde], returnObj7[returnObj7.Count - inde - 1]));
            }

            for (int i = 0; i < returnObj6.Count; i++)
            {
                Matrix returnObj9 = Global.feedForwardLayer.Activate(sum[i], g);
                double loss       = LossSoftmax.getLoss(returnObj9, x1[i].goldOutput);
                if (double.IsNaN(loss) || double.IsInfinity(loss))
                {
                    Console.WriteLine("WARNING!!!");
                    Global.swLog.WriteLine("WARNING!!!");
                    pa.mre.Set();
                    return;
                }
                LossSoftmax.getGrad(returnObj9, x1[i].goldOutput);
            }
            g.backwardProp();
            pa.mre.Set();
        }
Пример #2
0
        public static double runtestIteration(List <DataSeq> X, bool train)
        {
            double _total = 0, _prTotal = 0, _correct = 0;

            double total = 0, accuracy = 0, recall = 0, f_score = 0;
            double _total4 = 0, _prTotal4 = 0, _correct4 = 0, accuracy4 = 0, f_score4 = 0, recall4 = 0;

            Global.threadList = new List <TrainThread>();


            TrainThread runThread = new TrainThread(train);


            List <ManualResetEvent> manualEvents = new List <ManualResetEvent>();
            List <DataStep>         temp         = new List <DataStep>();
            StreamWriter            sw           = new StreamWriter("data/temp/answer.txt");


            string[] write_strings = new string[X.Count];
            Parallel.ForEach(X, (ex) =>
            {
                param pa = new param(ex);
                runThread.runtest(pa);
            });
            for (int i = 0; i < X.Count; i++)
            {
                sw.Write(X[i].write_string);
            }


            sw.Close();
            accuracy = runThread._correct / runThread._prTotal;
            recall   = runThread._correct / runThread._total;

            accuracy4 = runThread._correct4 / runThread._prTotal4;
            recall4   = runThread._correct4 / runThread._total4;

            f_score  = 2 * accuracy * recall / (accuracy + recall);
            f_score4 = 2 * accuracy4 * recall4 / (accuracy4 + recall4);
            //Console.WriteLine("total windows: " + _total);
            //Console.WriteLine("acc: " + (accuracy4 * 100).ToString("f3"));
            //Console.WriteLine("recall: " + (recall4 * 100).ToString("f3"));
            //Console.WriteLine("fscore: " + f_score4);
            //Global.swLog.WriteLine("total windows: " + total);
            //Global.swLog.WriteLine("acc: " + (accuracy4 * 100).ToString("f3"));
            //Global.swLog.WriteLine("recall: " + (recall4 * 100).ToString("f3"));
            //Global.swLog.WriteLine("fscore: " + f_score4);
            //Global.swLog.Flush();
            return(f_score4);
        }
Пример #3
0
        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);
        }
Пример #4
0
        public void runtest(object param)
        {
            param           pa = (param)param;
            List <DataStep> x1 = pa.datastep;

            int[]  ires4, igold4, wordindeis;
            string str = "", str1 = "";

            igold4     = new int[x1.Count];
            ires4      = new int[x1.Count];
            wordindeis = new int[x1.Count];
            int             index = 0, arraynum = 0;
            ForwdBackwdProp g = new ForwdBackwdProp(_train);

            int dim = 0;

            dim = x1.Count;


            Matrix[]   xpro       = new Matrix[dim];
            List <int> ires_model = new List <int>();


            //Parallel.For(0, temp.Count, i =>
            for (int i = 0; i < x1.Count; i++)
            {
                List <Matrix> add = new List <Matrix>();

                for (int k = 0; k < 5; k++)
                {
                    add.Add(Global.wordEmbedding[x1[i].inputs[k]]);
                }
                List <Matrix> returnObj2 = Global.GRNNLayer1.activate(add, g);
                List <Matrix> returnObj3 = Global.GRNNLayer2.activate(returnObj2, g);
                List <Matrix> returnObj4 = Global.GRNNLayer3.activate(returnObj3, g);
                List <Matrix> returnObj5 = Global.GRNNLayer4.activate(returnObj4, g);
                xpro[i] = returnObj5[0];
            }//);
            List <Matrix> returnObj6 = Global.upLSTMLayer.activate(xpro.ToList(), g);
            List <Matrix> returnObj7 = Global.upLSTMLayerr.activate(reverse(xpro.ToList()), g);
            List <Matrix> sum        = new List <Matrix>();

            for (int inde = 0; inde < returnObj6.Count(); inde++)
            {
                sum.Add(g.Add(returnObj6[inde], returnObj7[returnObj7.Count - inde - 1]));
            }

            for (int i = 0; i < xpro.Length; i++)
            {
                Matrix returnObj9 = Global.feedForwardLayer.Activate(sum[i], g);
                igold4[i] = LossSoftmax.getMax(x1[i].goldOutput);
                ires4[i]  = LossSoftmax.getMax(returnObj9);
            }

            //);


            //fscore.backprocess(wordindeis, ires4);

            pa.seq.write_string = "BOS O O" + "\n";
            //pa.sw.WriteLine("BOS O O");
            for (int i = 0; i < ires4.Count(); i++)
            {
                pa.seq.write_string += (Global.word[x1[i].wordindex] + " " + ires4[i] + " " + igold4[i] + "\n");
            }
            pa.seq.write_string += ("EOS O O" + "\n");
            pa.seq.write_string += "\n";


            List <string> res = fscore.getChunks4(ires4);

            str1 = fscore.calcorrect(fscore.getChunks4(igold4), res);

            lock (thislock)
            {
                string[] strs1 = str1.Split();
                _total4   += Int32.Parse(strs1[0]);
                _prTotal4 += Int32.Parse(strs1[1]);
                _correct4 += Int32.Parse(strs1[2]);
            }
        }