Exemple #1
0
        public CrossValidationResults CrossValidate(int folds = 10)
        {
            CrossValidation <T> xval = new CrossValidation <T>();

            xval.Folds = folds;
            xval.Sentences.AddRange(corpus);
            CrossValidationResults results = xval.Evaluate();

            return(results);
        }
Exemple #2
0
        public CrossValidationResults Evaluate()
        {
            Helpers.XorShiftRandom random    = new Helpers.XorShiftRandom(RandomSeed);
            List <Sentence>        sentences = Randomize ? Sentences.OrderBy(s => random.NextUInt()).ToList() : Sentences;

            List <Sentence>[] folds = new List <Sentence> [Folds];
            for (int i = 0; i < Folds; i++)
            {
                folds[i] = new List <Sentence>();
            }

            for (int i = 0; i < sentences.Count; i++)
            {
                folds[i % Folds].Add(sentences[i]);
            }

            CrossValidationResults results = new CrossValidationResults();
            DateTime start = DateTime.Now;

            int iterationsCount = 0;

            Parallel.For(0, Folds, (i) =>
            {
                CrossValidationResults.Result result = new CrossValidationResults.Result();
                result.Fold  = i + 1;
                result.Test  = folds[i].Select(sentence => sentence.Clone()).ToList();
                result.Train = new List <Sentence>();

                for (int j = 0; j < Folds; j++)
                {
                    if (j != i)
                    {
                        result.Train.AddRange(folds[j].Select(sentence => sentence.Clone()));
                    }
                }

                ITrainableTagger tagger = Activator.CreateInstance <T>();

                if (tagger is Tagging.Perceptron.PerceptronTagger)
                {
                    Tagging.Perceptron.PerceptronTagger ptagger = tagger as Tagging.Perceptron.PerceptronTagger;
                    ptagger.IterationFinished += (it) =>
                    {
                        lock (folds) iterationsCount++;
                        if (Progress != null)
                        {
                            Progress(((double)iterationsCount / (ptagger.Iterations * Folds)));
                        }
                    };
                }

                tagger.Train(result.Train);
                tagger.Tag(result.Test);

                results.Add(result);
            });

            results.Duration = DateTime.Now - start;

            return(results);
        }