示例#1
0
        public void TestTrainModelReturnsAqnModel()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                var trainer = new QNTrainer();

                var model = trainer.TrainModel(Iterations, di);

                Assert.NotNull(model);
            }
        }
示例#2
0
        public void TestDomainDimensionSanity()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                var correctDomainDimension = di.GetPredLabels().Length *di.GetOutcomeLabels().Length;

                Assert.AreEqual(correctDomainDimension, func.Dimension);
            }
        }
示例#3
0
        public void TestGradientAtNonInitialPoint()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                var gradientAtInitialPoint = func.GradientAt(func.GetInitialPoint());
                var expectedGradient       = new[] { -9.0, -14.0, -17.0, 20.0, 8.5, 9.0, 14.0, 17.0, -20.0, -8.5 };

                Assert.True(CompareDoubleArray(expectedGradient, gradientAtInitialPoint, di, Tolerance1));
            }
        }
示例#4
0
        public void TestValueAtInitialPoint()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                const double expectedValue = 13.86294361;

                var value = func.ValueAt(func.GetInitialPoint());

                Assert.AreEqual(expectedValue, value, Tolerance1);
            }
        }
示例#5
0
        public void TestGradientSanity()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                var initial           = func.GetInitialPoint();
                var gradientAtInitial = func.GradientAt(initial);

                // then
                Assert.NotNull(gradientAtInitial);
            }
        }
示例#6
0
        public void TestSerializationModel()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                var trainer = new QNTrainer();

                var model = trainer.TrainModel(Iterations, di);

                Assert.NotNull(model);

                QNModel deserialized;

                using (var mem = new MemoryStream()) {
                    using (var modelWriter = new GenericModelWriter(model, new UnclosableStream(mem))) {
                        modelWriter.Persist();
                        modelWriter.Close();
                    }

                    mem.Flush();
                    mem.Seek(0, SeekOrigin.Begin);


                    using (var modelReader = new GenericModelReader(new BinaryFileDataReader(mem)))
                        deserialized = modelReader.GetModel() as QNModel;
                }
                Assert.NotNull(deserialized);

                Assert.True(model.Equals(deserialized));

                var features2Classify = new [] {
                    "feature2", "feature3", "feature3",
                    "feature3", "feature3", "feature3",
                    "feature3", "feature3", "feature3",
                    "feature3", "feature3", "feature3"
                };

                var eval01 = model.Eval(features2Classify);
                var eval02 = deserialized.Eval(features2Classify);

                Assert.AreEqual(eval01.Length, eval02.Length);
                for (var i = 0; i < eval01.Length; i++)
                {
                    Assert.AreEqual(eval01[i], eval02[i], 0.00000001);
                }
            }
        }
示例#7
0
        public void TestInitialSanity()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                var initial = func.GetInitialPoint();

                // ReSharper disable once ForCanBeConvertedToForeach
                for (var i = 0; i < initial.Length; i++)
                {
                    Assert.AreEqual(0, initial[i], Tolerance1);
                }
            }
        }
示例#8
0
        public void TestValueAtNonInitialPoint02()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                var nonInitialPoint = new double[] { 3, 2, 3, 2, 3, 2, 3, 2, 3, 2 };
                var value           = func.ValueAt(DealignDoubleArrayForTestData(nonInitialPoint,
                                                                                 di.GetPredLabels(),
                                                                                 di.GetOutcomeLabels()));
                const double expectedValue = 53.163219721099026;

                Assert.AreEqual(expectedValue, value, Tolerance2);
            }
        }
示例#9
0
        public void TestGradientAtInitialPoint()
        {
            using (var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                di.Execute();

                var func = new NegLogLikelihood(di);

                var nonInitialPoint           = new[] { 0.2, 0.5, 0.2, 0.5, 0.2, 0.5, 0.2, 0.5, 0.2, 0.5 };
                var gradientAtNonInitialPoint = func.GradientAt(DealignDoubleArrayForTestData(nonInitialPoint, di.GetPredLabels(), di.GetOutcomeLabels()));
                var expectedGradient          =
                    new[] { -12.755042847945553, -21.227127506102434,
                            -72.57790706276435, 38.03525795198456,
                            15.348650889354925, 12.755042847945557,
                            21.22712750610244, 72.57790706276438,
                            -38.03525795198456, -15.348650889354925 };

                Assert.True(CompareDoubleArray(expectedGradient, gradientAtNonInitialPoint, di, Tolerance1));
            }
        }
示例#10
0
        public void TestInTinyDevSet()
        {
            using (
                var eventStream = new RealValueFileEventStream(Tests.GetFullPath(@"/opennlp/data/maxent/real-valued-weights-training-data.txt"), Encoding.UTF8)) {
                var di = new OnePassRealValueDataIndexer(eventStream, 1);

                var trainer = new QNTrainer();

                var model = trainer.TrainModel(Iterations, di);

                Assert.NotNull(model);

                var features2Classify = new[] {
                    "feature2", "feature3", "feature3",
                    "feature3", "feature3", "feature3",
                    "feature3", "feature3", "feature3",
                    "feature3", "feature3", "feature3"
                };
                var eval = model.Eval(features2Classify);

                Assert.NotNull(eval);
            }
        }