Ejemplo n.º 1
0
        public void TestRealValuedWeightsVsRepeatWeighting()
        {
            var rvfes1 = new RealValueFileEventStream(Tests.OpenFile("opennlp/data/maxent/real-valued-weights-training-data.txt"));
            var rvfes2 = new FileEventStream(Tests.OpenFile("opennlp/data/maxent/repeat-weighting-training-data.txt"));

            var realModel   = GIS.TrainModel(100, new OnePassRealValueDataIndexer(rvfes1, 1));
            var repeatModel = GIS.TrainModel(100, new OnePassRealValueDataIndexer(rvfes2, 1));

            rvfes1.Dispose();
            rvfes2.Dispose();

            Assert.NotNull(realModel);
            Assert.NotNull(repeatModel);

            var features2Classify = new[] { "feature2", "feature5" };
            var realResults       = realModel.Eval(features2Classify);
            var repeatResults     = repeatModel.Eval(features2Classify);

            Assert.AreEqual(realResults.Length, repeatResults.Length);

            for (var i = 0; i < realResults.Length; i++)
            {
                Assert.AreEqual(realResults[i], repeatResults[i], 0.01f);
            }

            features2Classify = new[] { "feature1", "feature2", "feature3", "feature4", "feature5" };
            realResults       = realModel.Eval(features2Classify, new[] { 5.5f, 6.1f, 9.1f, 4.0f, 1.8f });
            repeatResults     = repeatModel.Eval(features2Classify, new[] { 5.5f, 6.1f, 9.1f, 4.0f, 1.8f });

            Assert.AreEqual(realResults.Length, repeatResults.Length);
            for (var i = 0; i < realResults.Length; i++)
            {
                Assert.AreEqual(realResults[i], repeatResults[i], 0.01f);
            }
        }
Ejemplo n.º 2
0
        public void TestScaleResults()
        {
            const string smallValues = "predA=0.1 predB=0.2 A\n" + "predB=0.3 predA=0.1 B\n";
            const string largeValues = "predA=10 predB=20 A\n" + "predB=30 predA=10 B\n";

            const string smallTest = "predA=0.2 predB=0.2";
            const string largeTest = "predA=20 predB=20";

            var smallReader      = new StringReader(smallValues);
            var smallEventStream = new RealBasicEventStream(new PlainTextByLineStream(smallReader));

            var smallModel = GIS.TrainModel(100, new OnePassRealValueDataIndexer(smallEventStream, 0), false);

            var contexts     = smallTest.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var values       = RealValueFileEventStream.ParseContexts(contexts);
            var smallResults = smallModel.Eval(contexts, values);

            var smallResultString = smallModel.GetAllOutcomes(smallResults);

            Console.Out.WriteLine("smallResults: " + smallResultString);

            var largeReader      = new StringReader(largeValues);
            var largeEventStream = new RealBasicEventStream(new PlainTextByLineStream(largeReader));

            var largeModel = GIS.TrainModel(100, new OnePassRealValueDataIndexer(largeEventStream, 0), false);

            contexts = largeTest.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            values   = RealValueFileEventStream.ParseContexts(contexts);
            var largeResults = largeModel.Eval(contexts, values);

            var largeResultString = smallModel.GetAllOutcomes(largeResults);

            Console.Out.WriteLine("largeResults: " + largeResultString);

            Assert.AreEqual(smallResults.Length, largeResults.Length);
            for (var i = 0; i < smallResults.Length; i++)
            {
                /*
                 * System.out.println(string.format(
                 * "classify with smallModel: %1$s = %2$f", smallModel.getOutcome(i),
                 * smallResults[i]));
                 * System.out.println(string.format(
                 * "classify with largeModel: %1$s = %2$f", largeModel.getOutcome(i),
                 * largeResults[i])); */

                Assert.AreEqual(smallResults[i], largeResults[i], 0.01f);
            }
        }