• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# MarkovMultivariateFunction类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中MarkovMultivariateFunction的典型用法代码示例。如果您正苦于以下问题:C# MarkovMultivariateFunction类的具体用法?C# MarkovMultivariateFunction怎么用?C# MarkovMultivariateFunction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



MarkovMultivariateFunction类属于命名空间,在下文中一共展示了MarkovMultivariateFunction类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: RunTest

        public void RunTest()
        {
            var hmm = MultivariateNormalHiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
            var function = new MarkovMultivariateFunction(hmm);

            var model = new HiddenConditionalRandomField<double[]>(function);
            var target = new HiddenQuasiNewtonLearning<double[]>(model);

            var inputs = inputs1;
            var outputs = outputs1;

            double[] actual = new double[inputs.Length];
            double[] expected = new double[inputs.Length];

            for (int i = 0; i < inputs.Length; i++)
            {
                actual[i] = model.Compute(inputs[i]);
                expected[i] = outputs[i];
            }

            for (int i = 0; i < inputs.Length; i++)
                Assert.AreEqual(expected[i], actual[i]);

            double llm = hmm.LogLikelihood(inputs, outputs);
            double ll0 = model.LogLikelihood(inputs, outputs);
            Assert.AreEqual(llm, ll0, 1e-10);
            Assert.IsFalse(double.IsNaN(llm));
            Assert.IsFalse(double.IsNaN(ll0));

            double error = target.Run(inputs, outputs);
            double ll1 = model.LogLikelihood(inputs, outputs);
            Assert.AreEqual(-ll1, error, 1e-10);
            Assert.IsFalse(double.IsNaN(ll1));
            Assert.IsFalse(double.IsNaN(error));


            for (int i = 0; i < inputs.Length; i++)
            {
                actual[i] = model.Compute(inputs[i]);
                expected[i] = outputs[i];
            }

            Assert.AreEqual(-0.0000041736023117522336, ll0, 1e-10);
            
            Assert.AreEqual(error, -ll1);
            Assert.IsFalse(Double.IsNaN(ll0));
            Assert.IsFalse(Double.IsNaN(error));

            for (int i = 0; i < inputs.Length; i++)
                Assert.AreEqual(expected[i], actual[i]);

            Assert.IsTrue(ll1 > ll0);
        }
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:53,代码来源:MultivariateNormalQuasiNewtonHiddenLearningTest.cs


示例2: ForwardTest

        public void ForwardTest()
        {
            double[][][] observations;
            int[] labels;

            var hmm = IndependentMarkovFunctionTest.CreateModel2(out observations, out labels);

            var function = new MarkovMultivariateFunction(hmm, includePriors: false);


            foreach (double[][] x in observations)
            {
                foreach (int y in labels)
                {
                    double[] scaling1;
                    double logLikelihood1;

                    double[,] actual = Accord.Statistics.Models.Fields.
                        ForwardBackwardAlgorithm.Forward(function.Factors[y], x, y, out scaling1, out logLikelihood1);

                    double[] scaling2;
                    double logLikelihood2;
                    double[,] expected = Accord.Statistics.Models.Markov.
                        ForwardBackwardAlgorithm.Forward(hmm.Models[y], x, out scaling2, out logLikelihood2);

                    for (int i = 0; i < actual.GetLength(0); i++)
                        for (int j = 0; j < actual.GetLength(1); j++)
                        {
                            Assert.AreEqual(expected[i, j], actual[i, j], 1e-10);
                            Assert.IsFalse(Double.IsNaN(actual[i, j]));
                        }

                    Assert.AreEqual(logLikelihood1, logLikelihood2, 1e-10);

                    for (int i = 0; i < scaling1.Length; i++)
                        Assert.AreEqual(scaling1[i], scaling2[i], 1e-10);
                }
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:39,代码来源:IndependentForwardBackwardAlgorithmTest.cs


示例3: LogForwardGesturesPriorsDeoptimizedTest

        public void LogForwardGesturesPriorsDeoptimizedTest()
        {
            int[] labels;
            double[][][] words;
            var classifier = IndependentMarkovFunctionTest.CreateModel4(out words, out labels, true);

            var deopFun = new MarkovMultivariateFunction(classifier);
            deopFun.Deoptimize();
            var target1 = new HiddenConditionalRandomField<double[]>(deopFun);

            var function = new MarkovMultivariateFunction(classifier);
            var target2 = new HiddenConditionalRandomField<double[]>(function);

            foreach (var word in words)
            {
                for (int c = 0; c < 3; c++)
                {
                    for (int y = 0; y < 3; y++)
                    {
                        var actual = Accord.Statistics.Models.Fields.ForwardBackwardAlgorithm
                            .LogForward(target1.Function.Factors[c], word, y);

                        var expected = Accord.Statistics.Models.Fields.ForwardBackwardAlgorithm
                            .LogForward(target2.Function.Factors[c], word, y);

                        for (int i = 0; i < actual.GetLength(0); i++)
                        {
                            for (int j = 0; j < actual.GetLength(1); j++)
                            {
                                double a = actual[i, j];
                                double e = expected[i, j];
                                Assert.IsTrue(e.IsRelativelyEqual(a, 0.1));
                            }

                        }
                    }
                }
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:39,代码来源:IndependentForwardBackwardAlgorithmTest.cs


示例4: SaveLoadTest

        public void SaveLoadTest()
        {
            double[][] hello =
            {
                new double[] { 1.0, 0.1, 0.0, 0.0 }, // let's say the word
                new double[] { 0.0, 1.0, 0.1, 0.1 }, // hello took 6 frames
                new double[] { 0.0, 1.0, 0.1, 0.1 }, // to be recorded.
                new double[] { 0.0, 0.0, 1.0, 0.0 },
                new double[] { 0.0, 0.0, 1.0, 0.0 },
                new double[] { 0.0, 0.0, 0.1, 1.1 },
            };

            double[][] car =
            {
                new double[] { 0.0, 0.0, 0.0, 1.0 }, // the car word
                new double[] { 0.1, 0.0, 1.0, 0.1 }, // took only 4.
                new double[] { 0.0, 0.0, 0.1, 0.0 },
                new double[] { 1.0, 0.0, 0.0, 0.0 },
            };

            double[][] wardrobe =
            {
                new double[] { 0.0, 0.0, 1.0, 0.0 }, // same for the
                new double[] { 0.1, 0.0, 1.0, 0.1 }, // wardrobe word.
                new double[] { 0.0, 0.1, 1.0, 0.0 },
                new double[] { 0.1, 0.0, 1.0, 0.1 },
            };

            double[][][] words = { hello, car, wardrobe };

            int[] labels = { 0, 1, 2 };

            var initial = new Independent
            (
                new NormalDistribution(0, 1),
                new NormalDistribution(0, 1),
                new NormalDistribution(0, 1),
                new NormalDistribution(0, 1)
            );

            int numberOfWords = 3;
            int numberOfStates = 5;

            var classifier = new HiddenMarkovClassifier<Independent>
            (
               classes: numberOfWords,
               topology: new Forward(numberOfStates),
               initial: initial
            );

            var teacher = new HiddenMarkovClassifierLearning<Independent>(classifier,
                modelIndex => new BaumWelchLearning<Independent>(classifier.Models[modelIndex])
                {
                    Tolerance = 0.001,
                    Iterations = 100,
                    FittingOptions = new IndependentOptions()
                    {
                        InnerOption = new NormalOptions() { Regularization = 1e-5 }
                    }
                }
            );

            double logLikelihood = teacher.Run(words, labels);

            var function = new MarkovMultivariateFunction(classifier);
            var hcrf = new HiddenConditionalRandomField<double[]>(function);


            MemoryStream stream = new MemoryStream();

            hcrf.Save(stream);

            stream.Seek(0, SeekOrigin.Begin);

            var target = HiddenConditionalRandomField<double[]>.Load(stream);

            Assert.AreEqual(hcrf.Function.Factors.Length, target.Function.Factors.Length);
            for (int i = 0; i < hcrf.Function.Factors.Length; i++)
            {
                var e = hcrf.Function.Factors[i];
                var a = target.Function.Factors[i];
                Assert.AreEqual(e.Index, target.Function.Factors[i].Index);
                Assert.AreEqual(e.States, target.Function.Factors[i].States);

                Assert.AreEqual(e.EdgeParameters.Count, a.EdgeParameters.Count);
                Assert.AreEqual(e.EdgeParameters.Offset, a.EdgeParameters.Offset);
                Assert.AreEqual(e.FactorParameters.Count, a.FactorParameters.Count);
                Assert.AreEqual(e.FactorParameters.Offset, a.FactorParameters.Offset);

                Assert.AreEqual(e.OutputParameters.Count, a.OutputParameters.Count);
                Assert.AreEqual(e.OutputParameters.Offset, a.OutputParameters.Offset);
                Assert.AreEqual(e.StateParameters.Count, a.StateParameters.Count);
                Assert.AreEqual(e.StateParameters.Offset, a.StateParameters.Offset);

                Assert.AreEqual(target.Function, a.Owner);
                Assert.AreEqual(hcrf.Function, e.Owner);    
            }
            
            Assert.AreEqual(hcrf.Function.Features.Length, target.Function.Features.Length);
            for (int i = 0; i < hcrf.Function.Factors.Length; i++)
//.........这里部分代码省略.........
开发者ID:accord-net,项目名称:framework,代码行数:101,代码来源:HiddenConditionalRandomFieldTest.cs


示例5: ComputeTest

        public void ComputeTest()
        {
            var model = CreateModel1();

            var target = new MarkovMultivariateFunction(model);

            double actual;
            double expected;

            double[][] x = { new double[] { 0 }, new double[] { 1 } };

            for (int c = 0; c < model.Classes; c++)
            {
                for (int i = 0; i < model[c].States; i++)
                {
                    // Check initial state transitions
                    expected = model.Priors[c] * Math.Exp(model[c].Probabilities[i]) * model[c].Emissions[i].ProbabilityDensityFunction(x[0]);
                    actual = Math.Exp(target.Factors[c].Compute(-1, i, x, 0, c));
                    Assert.AreEqual(expected, actual, 1e-6);
                    Assert.IsFalse(double.IsNaN(actual));
                }

                for (int t = 1; t < x.Length; t++)
                {
                    // Check normal state transitions
                    for (int i = 0; i < model[c].States; i++)
                    {
                        for (int j = 0; j < model[c].States; j++)
                        {
                            expected = Math.Exp(model[c].Transitions[i, j]) * model[c].Emissions[j].ProbabilityDensityFunction(x[t]);
                            actual = Math.Exp(target.Factors[c].Compute(i, j, x, t, c));
                            Assert.AreEqual(expected, actual, 1e-6);
                            Assert.IsFalse(double.IsNaN(actual));
                        }
                    }
                }
            }
        }
开发者ID:qusma,项目名称:framework,代码行数:38,代码来源:MultivariateNormalHiddenMarkovClassifierFunctionTest.cs


示例6: ComputeTest3

        public void ComputeTest3()
        {
            var hmm = MultivariateMarkovFunctionTest.CreateModel1();

            var owner = new MarkovMultivariateFunction(hmm);


            double[][] x = 
            { 
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 3 },
                new double[] { 1 },
                new double[] { 2 },
                new double[] { 8 },
                new double[] { 0 },
                new double[] { 10 },
                new double[] { System.Math.PI },
            };

            foreach (var factor in owner.Factors)
            {
                for (int y = 0; y < owner.Outputs; y++)
                {
                    double[,] fwd = Accord.Statistics.Models.Fields
                        .ForwardBackwardAlgorithm.Forward(factor, x, y);

                    double[,] bwd = Accord.Statistics.Models.Fields
                        .ForwardBackwardAlgorithm.Backward(factor, x, y);

                    double[,] lnfwd = Accord.Statistics.Models.Fields
                        .ForwardBackwardAlgorithm.LogForward(factor, x, y);

                    double[,] lnbwd = Accord.Statistics.Models.Fields
                        .ForwardBackwardAlgorithm.LogBackward(factor, x, y);


                    for (int i = 0; i < fwd.GetLength(0); i++)
                        for (int j = 0; j < fwd.GetLength(1); j++)
                            Assert.AreEqual(System.Math.Log(fwd[i, j]), lnfwd[i, j], 1e-10);

                    for (int i = 0; i < bwd.GetLength(0); i++)
                        for (int j = 0; j < bwd.GetLength(1); j++)
                            Assert.AreEqual(System.Math.Log(bwd[i, j]), lnbwd[i, j], 1e-10);


                    foreach (var feature in factor)
                    {
                        double expected = System.Math.Log(feature.Marginal(fwd, bwd, x, y));
                        double actual = feature.LogMarginal(lnfwd, lnbwd, x, y);

                        Assert.AreEqual(expected, actual, 1e-10);
                        Assert.IsFalse(Double.IsNaN(actual));
                    }

                }
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:58,代码来源:FeatureTest.cs


示例7: GradientTest3

        public void GradientTest3()
        {
            var hmm = MultivariateNormalHiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
            var function = new MarkovMultivariateFunction(hmm);

            var model = new HiddenConditionalRandomField<double[]>(function);
            var target = new ForwardBackwardGradient<double[]>(model);
            target.Regularization = 2;

            var inputs = inputs1;
            var outputs = outputs1;



            FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);

            diff.Function = parameters => func(model, parameters, inputs, outputs, target.Regularization);

            double[] expected = diff.Compute(function.Weights);
            double[] actual = target.Gradient(function.Weights, inputs, outputs);


            for (int i = 0; i < actual.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 1e-3);

                Assert.IsFalse(double.IsNaN(actual[i]));
                Assert.IsFalse(double.IsNaN(expected[i]));
            }
        }
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:30,代码来源:MultivariateNormalQuasiNewtonHiddenLearningTest.cs


示例8: LogForwardTest4

        public void LogForwardTest4()
        {
            var hmmc = Accord.Tests.Statistics.Models.Fields.
                MultivariateMarkovFunctionTest.CreateModel3();

            for (int c = 0; c < hmmc.Classes; c++)
            {
                var hmm = hmmc[c];

                var function = new MarkovMultivariateFunction(hmm);

                var sequences = Accord.Tests.Statistics.Models.Fields.
                    MultivariateMarkovFunctionTest.inputTest;

                for (int i = 0; i < sequences.Length; i++)
                {
                    var observations = sequences[i];

                    double expectedLogLikelihood;
                    double[,] expected = Accord.Statistics.Models.Markov
                        .ForwardBackwardAlgorithm.LogForward(hmm, observations, out expectedLogLikelihood);

                    double actualLogLikelihood;
                    double[,] actual = Accord.Statistics.Models.Fields
                        .ForwardBackwardAlgorithm.LogForward(function.Factors[0], observations, 0, out actualLogLikelihood);


                    Assert.IsTrue(expected.IsEqual(actual, 1e-10));

                    Assert.AreEqual(expectedLogLikelihood, actualLogLikelihood, 1e-6);
                    Assert.IsFalse(Double.IsNaN(actualLogLikelihood));
                }
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:34,代码来源:ForwardBackwardAlgorithmTest.cs


示例9: LogForwardTest3

        public void LogForwardTest3()
        {
            MultivariateNormalDistribution density = new MultivariateNormalDistribution(3);
            var hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(2, new Ergodic(2), density);

            double[][][] inputs =
            {
                new [] { new double[] { 0, 1, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 1, 0 } },
                new [] { new double[] { 1, 6, 2 }, new double[] { 2, 1, 6 }, new double[] { 1, 1, 0 } },
                new [] { new double[] { 9, 1, 0 }, new double[] { 0, 1, 5 }, new double[] { 0, 0, 0 } },
            };

            int[] outputs = 
            {
                0, 0, 1
            };

            var function = new MarkovMultivariateFunction(hmm);

            var observations = inputs[0];

            double[,] expected = Matrix.Log(Accord.Statistics.Models.Fields.
                ForwardBackwardAlgorithm.Forward(function.Factors[0], observations, 0));

            double logLikelihood;
            double[,] actual = Accord.Statistics.Models.Fields.
                ForwardBackwardAlgorithm.LogForward(function.Factors[0], observations, 0, out logLikelihood);

            Assert.IsTrue(expected.IsEqual(actual, 1e-10));

            double p = 0;
            for (int i = 0; i < hmm[0].States; i++)
                p += Math.Exp(actual[observations.Length - 1, i]);

            Assert.AreEqual(Math.Exp(logLikelihood), p, 1e-8);
            Assert.IsFalse(double.IsNaN(p));
        }
开发者ID:accord-net,项目名称:framework,代码行数:37,代码来源:ForwardBackwardAlgorithmTest.cs


示例10: ComputeDeoptimizeTest4

        public void ComputeDeoptimizeTest4()
        {
            int[] labels;
            double[][][] words;
            var model = CreateModel4(out words, out labels, false);

            var target = new MarkovMultivariateFunction(model);

#pragma warning disable 0618
            target.Deoptimize();
#pragma warning restore 0618

            var hcrf = new HiddenConditionalRandomField<double[]>(target);


            Assert.AreEqual(3, model.Priors.Length);
            Assert.AreEqual(1 / 3.0, model.Priors[0]);
            Assert.AreEqual(1 / 3.0, model.Priors[1]);
            Assert.AreEqual(1 / 3.0, model.Priors[2]);

            check4(words, model, target, hcrf);
        }
开发者ID:RLaumeyer,项目名称:framework,代码行数:22,代码来源:IndependentMarkovFunctionTest.cs


示例11: ComputeDeoptimizeTest3

        public void ComputeDeoptimizeTest3()
        {
            double[][][] sequences;
            int[] labels;
            var model = CreateModel3(out sequences, out labels);

            var target = new MarkovMultivariateFunction(model);

#pragma warning disable 0618
            target.Deoptimize();
#pragma warning restore 0618

            var hcrf = new HiddenConditionalRandomField<double[]>(target);


            Assert.AreEqual(2, model.Priors.Length);
            Assert.AreEqual(1 / 2.0, model.Priors[0]);
            Assert.AreEqual(1 / 2.0, model.Priors[1]);

            check4(sequences, model, target, hcrf);
        }
开发者ID:RLaumeyer,项目名称:framework,代码行数:21,代码来源:IndependentMarkovFunctionTest.cs


示例12: ComputeTest4

        public void ComputeTest4()
        {
            int[] labels;
            double[][][] words;
            HiddenMarkovClassifier<Independent<NormalDistribution>> model =
                CreateModel4(out words, out labels, false);

            var target = new MarkovMultivariateFunction(model);

            var hcrf = new HiddenConditionalRandomField<double[]>(target);


            Assert.AreEqual(3, model.Priors.Length);
            Assert.AreEqual(1 / 3.0, model.Priors[0]);
            Assert.AreEqual(1 / 3.0, model.Priors[1]);
            Assert.AreEqual(1 / 3.0, model.Priors[2]);

            check4(words, model, target, hcrf);
        }
开发者ID:RLaumeyer,项目名称:framework,代码行数:19,代码来源:IndependentMarkovFunctionTest.cs


示例13: ComputeTest2

        public void ComputeTest2()
        {
            double[][][] sequences;
            int[] labels;
            var model = CreateModel2(out sequences, out labels);

            var target = new MarkovMultivariateFunction(model);
            var hcrf = new HiddenConditionalRandomField<double[]>(target);


            double actual;
            double expected;

            double[][] x = { new double[] { 0, 1.7 }, new double[] { 2, 2.1 } };

            for (int c = 0; c < model.Classes; c++)
            {
                for (int i = 0; i < model[c].States; i++)
                {
                    // Check initial state transitions
                    expected = model.Priors[c] * Math.Exp(model[c].Probabilities[i]) * model[c].Emissions[i].ProbabilityDensityFunction(x[0]);
                    actual = Math.Exp(target.Factors[c].Compute(-1, i, x, 0, c));
                    Assert.AreEqual(expected, actual, 1e-6);
                    Assert.IsFalse(double.IsNaN(actual));
                }

                for (int t = 1; t < x.Length; t++)
                {
                    // Check normal state transitions
                    for (int i = 0; i < model[c].States; i++)
                    {
                        for (int j = 0; j < model[c].States; j++)
                        {
                            double xb = Math.Exp(model[c].Transitions[i, j]);
                            double xc = model[c].Emissions[j].ProbabilityDensityFunction(x[t]);
                            expected = xb * xc;
                            actual = Math.Exp(target.Factors[c].Compute(i, j, x, t, c));
                            Assert.AreEqual(expected, actual, 1e-6);
                            Assert.IsFalse(double.IsNaN(actual));
                        }
                    }
                }

                actual = model.LogLikelihood(x, c);
                expected = hcrf.LogLikelihood(x, c);
                Assert.AreEqual(expected, actual);
                Assert.IsFalse(double.IsNaN(actual));
            }
        }
开发者ID:RLaumeyer,项目名称:framework,代码行数:49,代码来源:IndependentMarkovFunctionTest.cs


示例14: LogBackwardTest2

        public void LogBackwardTest2()
        {
            HiddenMarkovClassifier<Independent> hmm = IndependentMarkovClassifierPotentialFunctionTest
                .CreateModel3();

            MarkovMultivariateFunction function = new MarkovMultivariateFunction(hmm);

            double[][][] observations = IndependentMarkovClassifierPotentialFunctionTest.sequences2;
            int[] labels = IndependentMarkovClassifierPotentialFunctionTest.labels2;

            foreach (double[][] x in observations)
            {
                foreach (int y in labels)
                {
                    double[,] actual = new double[x.Length, 5];
                    Accord.Statistics.Models.Fields.
                        ForwardBackwardAlgorithm.LogBackward(function.Factors[y], x, y, actual);

                    double[,] expected = new double[x.Length, 5];
                    Accord.Statistics.Models.Markov.
                        ForwardBackwardAlgorithm.LogBackward(hmm.Models[y], x, expected);

                    for (int i = 0; i < actual.GetLength(0); i++)
                        for (int j = 0; j < actual.GetLength(1); j++)
                        {
                            Assert.AreEqual(expected[i, j], actual[i, j], 1e-10);
                            Assert.IsFalse(Double.IsNaN(actual[i, j]));
                        }
                }
            }
        }
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:31,代码来源:IndependentForwardBackwardAlgorithmTest.cs


示例15: NumberOfFeaturesTest

        public void NumberOfFeaturesTest()
        {
            Independent initial = new Independent(
                      new GeneralDiscreteDistribution(46), // output,
                      new NormalDistribution(0, 1),        // headAngle, 
                      new NormalDistribution(0, 1),        // handAngle, 
                      new NormalDistribution(0, 1),        // relHandX, 
                      new NormalDistribution(0, 1)         // relHandY, 
            );



            var model = new HiddenMarkovClassifier<Independent>(
                classes: 13, topology: new Forward(5), initial: initial);

            var function = new MarkovMultivariateFunction(model);
            var field = new HiddenConditionalRandomField<double[]>(function);



            int discreteDistributions = 1;
            int continuousDistributions = 4;
            int symbols = 46;
            int states = 5;
            int classes = 13;
            int expected = classes *
                (1 + states + states * states +
                states * discreteDistributions * symbols +
                states * continuousDistributions * 3);

            Assert.AreEqual(expected, field.Function.Features.Length);
            Assert.AreEqual(expected, field.Function.Weights.Length);
            Assert.AreEqual(4173, expected);
        }
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:34,代码来源:HiddenConditionalRandomFieldTest.cs


示例16: LogForwardTest

        public void LogForwardTest()
        {
            double[][][] observations;
            int[] labels;
            var hmm = IndependentMarkovFunctionTest.CreateModel2(out observations, out labels);

            MarkovMultivariateFunction function = new MarkovMultivariateFunction(hmm, includePriors: false);

            foreach (double[][] x in observations)
            {
                foreach (int y in labels)
                {
                    double[,] actual = new double[5, 2];
                    Accord.Statistics.Models.Fields.
                        ForwardBackwardAlgorithm.LogForward(function.Factors[y], x, y, actual);

                    double[,] expected = new double[5, 2];
                    Accord.Statistics.Models.Markov.
                        ForwardBackwardAlgorithm.LogForward(hmm.Models[y], x, expected);

                    for (int i = 0; i < actual.GetLength(0); i++)
                        for (int j = 0; j < actual.GetLength(1); j++)
                            Assert.AreEqual(expected[i, j], actual[i, j], 1e-10);
                }
            }
        }
开发者ID:accord-net,项目名称:framework,代码行数:26,代码来源:IndependentForwardBackwardAlgorithmTest.cs


示例17: check4

        private static void check4(double[][][] words, HiddenMarkovClassifier<Independent> model, MarkovMultivariateFunction target, HiddenConditionalRandomField<double[]> hcrf)
        {
            double actual;
            double expected;

            foreach (var x in words)
            {
                for (int c = 0; c < model.Classes; c++)
                {
                    for (int i = 0; i < model[c].States; i++)
                    {
                        // Check initial state transitions
                        double xa = model.Priors[c];
                        double xb = Math.Exp(model[c].Probabilities[i]);
                        double xc = model[c].Emissions[i].ProbabilityDensityFunction(x[0]);
                        expected = xa * xb * xc;
                        actual = Math.Exp(target.Factors[c].Compute(-1, i, x, 0, c));
                        Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-10));
                        Assert.IsFalse(double.IsNaN(actual));
                    }

                    for (int t = 1; t < x.Length; t++)
                    {
                        // Check normal state transitions
                        for (int i = 0; i < model[c].States; i++)
                        {
                            for (int j = 0; j < model[c].States; j++)
                            {
                                double xb = Math.Exp(model[c].Transitions[i, j]);
                                double xc = model[c].Emissions[j].ProbabilityDensityFunction(x[t]);
                                expected = xb * xc;
                                actual = Math.Exp(target.Factors[c].Compute(i, j, x, t, c));
                                Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-10));
                                Assert.IsFalse(double.IsNaN(actual));
                            }
                        }
                    }

                    actual = Math.Exp(model.LogLikelihood(x, c));
                    expected = Math.Exp(hcrf.LogLikelihood(x, c));
                    Assert.AreEqual(expected, actual, 1e-10);
                    Assert.IsFalse(double.IsNaN(actual));

                    actual = model.Compute(x);
                    expected = hcrf.Compute(x);
                    Assert.AreEqual(expected, actual);
                    Assert.IsFalse(double.IsNaN(actual));
                }
            }
        }
开发者ID:RLaumeyer,项目名称:framework,代码行数:50,代码来源:IndependentMarkovFunctionTest.cs


示例18: ForwardTest4

        public void ForwardTest4()
        {
            var hmmc = Accord.Tests.Statistics.Models.Fields.
                MultivariateMarkovFunctionTest.CreateModel3();

            var hmm = hmmc[0];

            var function = new MarkovMultivariateFunction(hmm);

            var observations = Accord.Tests.Statistics.Models.Fields.
                MultivariateMarkovFunctionTest.inputTest[3];

            double expectedLogLikelihood;
            double[,] expected = Accord.Statistics.Models.Markov
                .ForwardBackwardAlgorithm.Forward(hmm, observations, out expectedLogLikelihood);

            double actualLogLikelihood;
            double[,] actual = Accord.Statistics.Models.Fields.
                ForwardBackwardAlgorithm.Forward(function.Factors[0], observations, 0, out actualLogLikelihood);


            Assert.IsTrue(expected.IsEqual(actual, 1e-10));


            Assert.AreEqual(expectedLogLikelihood, actualLogLikelihood, 1e-6);
            Assert.IsFalse(Double.IsNaN(actualLogLikelihood));

        }
开发者ID:accord-net,项目名称:framework,代码行数:28,代码来源:ForwardBackwardAlgorithmTest.cs


示例19: GradientDeoptimizeTest3

        public void GradientDeoptimizeTest3()
        {
            double[][][] sequences2;
            int[] labels2;
            var hmm = CreateModel3(out sequences2, out labels2);
            var function = new MarkovMultivariateFunction(hmm);

#pragma warning disable 0618
            function.Deoptimize();
#pragma warning restore 0618

            var model = new HiddenConditionalRandomField<double[]>(function);
            var target = new ForwardBackwardGradient<double[]>(model);
            target.Regularization = 2;

            var inputs = sequences2;
            var outputs = labels2;


            FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);

            diff.Function = parameters => func(model, parameters, inputs, outputs, target.Regularization);

            double[] expected = diff.Compute(function.Weights);
            double[] actual = target.Gradient(function.Weights, inputs, outputs);


            for (int i = 0; i < actual.Length; i++)
            {
                double e = expected[i];
                double a = actual[i];
                Assert.AreEqual(e, a, 1e-3);

                Assert.IsFalse(double.IsNaN(actual[i]));
                Assert.IsFalse(double.IsNaN(expected[i]));
            }
        }
开发者ID:RLaumeyer,项目名称:framework,代码行数:37,代码来源:IndependentMarkovFunctionTest.cs


示例20: GradientTest

        public void GradientTest()
        {
            // Creates a sequence classifier containing 2 hidden Markov Models
            //  with 2 states and an underlying Normal distribution as density.
            MultivariateNormalDistribution density = new MultivariateNormalDistribution(3);
            var hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(2, new Ergodic(2), density);

            double[][][] inputs =
            {
                new [] { new double[] { 0, 1, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 1, 0 } },
                new [] { new double[] { 1, 6, 2 }, new double[] { 2, 1, 6 }, new double[] { 1, 1, 0 } },
                new [] { new double[] { 9, 1, 0 }, new double[] { 0, 1, 5 }, new double[] { 0, 0, 0 } },
            };

            int[] outputs = 
            {
                0, 0, 1
            };

            var function = new MarkovMultivariateFunction(hmm);

            var model = new HiddenConditionalRandomField<double[]>(function);
            var target = new ForwardBackwardGradient<double[]>(model);

            FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);

            diff.Function = parameters => func(model, parameters, inputs, outputs);

            double[] expected = diff.Compute(function.Weights);
            double[] actual = target.Gradient(function.Weights, inputs, outputs);


            for (int i = 0; i < actual.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 0.05);
                Assert.IsFalse(double.IsNaN(actual[i]));
                Assert.IsFalse(double.IsNaN(expected[i]));
            }
        }
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:39,代码来源:MultivariateNormalQuasiNewtonHiddenLearningTest.cs



注:本文中的MarkovMultivariateFunction类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# MarshalByRefObject类代码示例发布时间:2022-05-24
下一篇:
C# MarkovDiscreteFunction类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap