示例#1
0
        public void ComputeTest()
        {
            HiddenMarkovModel hmm = DiscreteHiddenMarkovModelFunctionTest.CreateModel2();

            int states = hmm.States;


            var    function = new MarkovDiscreteFunction(hmm);
            var    target = new ConditionalRandomField <int>(states, function);
            double p1, p2;

            int[] observations, expected, actual;

            observations = new int[] { 0, 0, 1, 1, 1, 2 };
            expected     = hmm.Decode(observations, out p1);
            actual       = target.Compute(observations, out p2);

            Assert.IsTrue(expected.IsEqual(actual));
            Assert.AreEqual(p1, p2, 1e-6);


            observations = new int[] { 0, 1, 2, 2, 2 };
            expected     = hmm.Decode(observations, out p1);
            actual       = target.Compute(observations, out p2);

            Assert.IsTrue(expected.IsEqual(actual));
            Assert.AreEqual(p1, p2, 1e-6);
        }
        public void RunTest()
        {
            Accord.Math.Random.Generator.Seed = 0;

            int nstates = 3;
            int symbols = 3;

            int[][] sequences = new int[][]
            {
                new int[] { 0, 1, 1, 1, 2 },
                new int[] { 0, 1, 1, 1, 2, 2, 2 },
                new int[] { 0, 0, 1, 1, 2, 2 },
                new int[] { 0, 1, 1, 1, 2, 2, 2 },
                new int[] { 0, 1, 1, 1, 2, 2 },
                new int[] { 0, 1, 1, 2, 2 },
                new int[] { 0, 0, 1, 1, 1, 2, 2 },
                new int[] { 0, 0, 0, 1, 1, 1, 2, 2 },
                new int[] { 0, 1, 1, 2, 2, 2 },
            };


            var function = new MarkovDiscreteFunction(nstates, symbols, new NormalDistribution());
            var model    = new ConditionalRandomField <int>(nstates, function);


            for (int i = 0; i < sequences.Length; i++)
            {
                double p;
                int[]  s = sequences[i];
                int[]  r = model.Compute(s, out p);
                Assert.IsFalse(s.IsEqual(r));
            }

            var target = new QuasiNewtonLearning <int>(model);

            target.ParallelOptions.MaxDegreeOfParallelism = 1;

            int[][] labels       = sequences;
            int[][] observations = sequences;

            double ll0 = model.LogLikelihood(observations, labels);

            double actual = target.Run(observations, labels);

            double ll1 = model.LogLikelihood(observations, labels);

            Assert.IsTrue(ll1 > ll0);


            Assert.AreEqual(-0.0010766857305242183, actual, 1e-6);

            for (int i = 0; i < sequences.Length; i++)
            {
                double p;
                int[]  s = sequences[i];
                int[]  r = model.Compute(s, out p);
                Assert.IsTrue(s.IsEqual(r));
            }
        }
        public void RunTest()
        {
            int nstates = 3;
            int symbols = 3;

            int[][] sequences = new int[][]
            {
                new int[] { 0, 1, 1, 1, 2 },
                new int[] { 0, 1, 1, 1, 2, 2, 2 },
                new int[] { 0, 0, 1, 1, 2, 2 },
                new int[] { 0, 1, 1, 1, 2, 2, 2 },
                new int[] { 0, 1, 1, 1, 2, 2 },
                new int[] { 0, 1, 1, 2, 2 },
                new int[] { 0, 0, 1, 1, 1, 2, 2 },
                new int[] { 0, 0, 0, 1, 1, 1, 2, 2 },
                new int[] { 0, 1, 1, 2, 2, 2 },
            };


            var function = new MarkovDiscreteFunction(nstates, symbols);
            var model    = new ConditionalRandomField <int>(nstates, function);


            for (int i = 0; i < sequences.Length; i++)
            {
                double p;
                int[]  s = sequences[i];
                int[]  r = model.Compute(s, out p);
                Assert.IsFalse(s.IsEqual(r));
            }

            var target = new QuasiNewtonLearning <int>(model);

            int[][] labels       = sequences;
            int[][] observations = sequences;

            double ll0 = model.LogLikelihood(observations, labels);

            double actual = target.Run(observations, labels);

            double ll1 = model.LogLikelihood(observations, labels);

            Assert.IsTrue(ll1 > ll0);


            Assert.AreEqual(0, actual, 1e-8);

            for (int i = 0; i < sequences.Length; i++)
            {
                double p;
                int[]  s = sequences[i];
                int[]  r = model.Compute(s, out p);
                Assert.IsTrue(s.IsEqual(r));
            }
        }