예제 #1
0
        public PerceptronTagger()
        {
            random = new Helpers.XorShiftRandom();
            featureTemplatesTag   = new List <FeatureTemplate>();
            featureTemplatesLemma = new List <FeatureTemplate>();
            normalizers           = new List <Normalizer>(); // todo: fix new possible clone in Tag()
            unnormalizers         = new List <Unnormalizer>();

            Iterations      = 1;
            Reverse         = false;
            Average         = false;
            WeightThreshold = 0;
        }
예제 #2
0
        public PerceptronTagger()
        {
            random = new Helpers.XorShiftRandom();
            featureTemplatesTag = new List<FeatureTemplate>();
            featureTemplatesLemma = new List<FeatureTemplate>();
            normalizers = new List<Normalizer>(); // todo: fix new possible clone in Tag()
            unnormalizers = new List<Unnormalizer>();

            Iterations = 1;
            Reverse = false;
            Average = false;
            WeightThreshold = 0;
        }
예제 #3
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);
        }