当前位置: 首页>>代码示例>>C#>>正文


C# Array.Max方法代码示例

本文整理汇总了C#中System.Array.Max方法的典型用法代码示例。如果您正苦于以下问题:C# Array.Max方法的具体用法?C# Array.Max怎么用?C# Array.Max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Array的用法示例。


在下文中一共展示了Array.Max方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: run

        private double run(Array[] observations)
        {
            // Baum-Welch algorithm.

            // The Baum–Welch algorithm is a particular case of a generalized expectation-maximization
            // (GEM) algorithm. It can compute maximum likelihood estimates and posterior mode estimates
            // for the parameters (transition and emission probabilities) of an HMM, when given only
            // emissions as training data.

            // The algorithm has two steps:
            //  - Calculating the forward probability and the backward probability for each HMM state;
            //  - On the basis of this, determining the frequency of the transition-emission pair values
            //    and dividing it by the probability of the entire string. This amounts to calculating
            //    the expected count of the particular transition-emission pair. Each time a particular
            //    transition is found, the value of the quotient of the transition divided by the probability
            //    of the entire string goes up, and this value can then be made the new value of the transition.

            // Grab model information
            int states = model.States;
            var logA = model.Transitions;
            var logP = model.Probabilities;

            // Initialize the algorithm
            int N = observations.Length;
            double logN = Math.Log(N);
            LogKsi = new double[N][][,];
            LogGamma = new double[N][,];

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

                LogKsi[i] = new double[T][,];
                LogGamma[i] = new double[T, states];

                for (int t = 0; t < LogKsi[i].Length; t++)
                    LogKsi[i][t] = new double[states, states];
            }

            bool stop = false;

            int TMax = observations.Max(x => x.Length);
            double[,] lnFwd = new double[TMax, states];
            double[,] lnBwd = new double[TMax, states];

            // Initialize the model log-likelihoods
            double newLogLikelihood = Double.NegativeInfinity;
            convergence.NewValue = Double.NegativeInfinity;

            int itersLeft = 30;
            do // Until convergence or max iterations is reached
            {
                itersLeft--;
                // For each sequence in the observations input
                for (int i = 0; i < observations.Length; i++)
                {
                    int T = observations[i].Length;
                    double[,] logGamma = LogGamma[i];
                    double w = LogWeights[i];

                    // 1st step - Calculating the forward probability and the
                    //            backward probability for each HMM state.
                    ComputeForwardBackward(i, lnFwd, lnBwd);

                    // 2nd step - Determining the frequency of the transition-emission pair values
                    //            and dividing it by the probability of the entire string.

                    // Calculate gamma values for next computations
                    for (int t = 0; t < T; t++)
                    {
                        double lnsum = Double.NegativeInfinity;
                        for (int k = 0; k < states; k++)
                        {
                            logGamma[t, k] = lnFwd[t, k] + lnBwd[t, k] + w;
                            lnsum = Special.LogSum(lnsum, logGamma[t, k]);
                        }

                       // System.Diagnostics.Debug.Assert(!Double.IsNaN(lnsum));

                        // Normalize if different from zero
                        if (lnsum != Double.NegativeInfinity)
                            for (int k = 0; k < states; k++)
                                logGamma[t, k] = logGamma[t, k] - lnsum;
                    }

                    // Calculate ksi values for next computations
                    ComputeKsi(i, lnFwd, lnBwd);

                    // Compute log-likelihood for the given sequence
                    for (int j = 0; j < states; j++)
                        newLogLikelihood = Special.LogSum(newLogLikelihood, lnFwd[T - 1, j]);
                }

                // Average the likelihood for all sequences
                newLogLikelihood /= observations.Length;
                convergence.NewValue = newLogLikelihood;

                // Check for convergence
                if (!convergence.HasConverged)
                {
//.........这里部分代码省略.........
开发者ID:tapans,项目名称:Kinect-and-Machine-Learning,代码行数:101,代码来源:BaseBaumWelchLearning.cs


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