/// <summary>
        ///   Constructs a new potential function modeling Hidden Markov Models.
        /// </summary>
        ///
        /// <param name="classifier">The classifier model.</param>
        ///
        public HiddenMarkovClassifierFunction(HiddenMarkovClassifier classifier)
        {
            this.Symbols = classifier.Symbols;
            this.Outputs = classifier.Classes;

            var l = new List <double>();
            var f = new List <IFeature <int> >();

            this.Factors = new FactorPotential <int> [Outputs];

            int index = 0;

            // Create features for initial class probabilities
            for (int c = 0; c < classifier.Classes; c++)
            {
                int parameters = 0;
                var model      = classifier[c];

                // l.Add(Math.Log(classifier.Priors[c]));
                // f.Add(new OutputFeature<int>(this, c, c));
                // parameters++;

                // Create features for initial state probabilities
                for (int i = 0; i < model.States; i++)
                {
                    l.Add(model.Probabilities[i]);
                    f.Add(new InitialFeature <int>(this, c, i));
                    parameters++;
                }

                // Create features for state transition probabilities
                for (int i = 0; i < model.States; i++)
                {
                    for (int j = 0; j < model.States; j++)
                    {
                        l.Add(model.Transitions[i, j]);
                        f.Add(new TransitionFeature <int>(this, c, i, j));
                        parameters++;
                    }
                }

                // Create features for symbol emission probabilities
                for (int i = 0; i < model.States; i++)
                {
                    for (int j = 0; j < model.Symbols; j++)
                    {
                        l.Add(model.LogEmissions[i, j]);
                        f.Add(new EmissionFeature(this, c, i, j));
                        parameters++;
                    }
                }

                Factors[c] = new FactorPotential <int>(this, index, model.States, parameters);
                index     += parameters;
            }

            this.Weights  = l.ToArray();
            this.Features = f.ToArray();
        }
Example #2
0
 internal void Deoptimize()
 {
     for (int i = 0; i < Factors.Length; i++)
     {
         var factor = Factors[i];
         Factors[i] = new FactorPotential <T>(factor.Owner, factor.States, factor.Index,
                                              factor.EdgeParameters.Offset, factor.EdgeParameters.Count,
                                              factor.StateParameters.Offset, factor.StateParameters.Count,
                                              factor.OutputParameters.Offset, factor.OutputParameters.Count);
     }
 }
        /// <summary>
        ///   Constructs a new potential function modeling Hidden Markov Models.
        /// </summary>
        ///
        /// <param name="classifier">A hidden Markov sequence classifier.</param>
        ///
        public MultivariateNormalHiddenMarkovClassifierFunction(
            HiddenMarkovClassifier <MultivariateNormalDistribution> classifier)
        {
            this.Outputs = classifier.Classes;

            var l = new List <double>();
            var f = new List <IFeature <double[]> >();

            this.Factors = new FactorPotential <double[]> [Outputs];

            int index = 0;

            // Create features for initial class probabilities
            for (int c = 0; c < classifier.Classes; c++)
            {
                int parameters = 0;
                var model      = classifier[c];

                //l.Add(Math.Log(classifier.Priors[c]));
                //f.Add(new OutputFeature<double[]>(this, c, c));
                //parameters++;

                // Create features for initial state probabilities
                for (int i = 0; i < model.States; i++)
                {
                    l.Add(model.Probabilities[i]);
                    f.Add(new InitialFeature <double[]>(this, c, i));
                    parameters++;
                }

                // Create features for state transition probabilities
                for (int i = 0; i < model.States; i++)
                {
                    for (int j = 0; j < model.States; j++)
                    {
                        l.Add(model.Transitions[i, j]);
                        f.Add(new TransitionFeature <double[]>(this, c, i, j));
                        parameters++;
                    }
                }

                // Create features emission probabilities
                for (int i = 0; i < model.States; i++)
                {
                    for (int j = 0; j < model.Emissions[i].Mean.Length; j++)
                    {
                        double mean = model.Emissions[i].Mean[j];
                        double var  = model.Emissions[i].Variance[j];

                        // Occupancy
                        l.Add(-0.5 * (Math.Log(2.0 * Math.PI * var) + (mean * mean) / var));
                        f.Add(new OccupancyFeature <double[]>(this, c, i));
                        parameters++;

                        // 1st Moment (x)
                        l.Add(mean / var);
                        f.Add(new MultivariateFirstMomentFeature(this, c, i, j));
                        parameters++;

                        // 2nd Moment (x²)
                        l.Add(-1.0 / (2.0 * var));
                        f.Add(new MultivariateSecondMomentFeature(this, c, i, j));
                        parameters++;
                    }
                }

                Factors[c] = new FactorPotential <double[]>(this, index, model.States, parameters);
                index     += parameters;
            }

            this.Weights  = l.ToArray();
            this.Features = f.ToArray();
        }
        /// <summary>
        ///   Constructs a new potential function modeling Hidden Markov Models.
        /// </summary>
        ///
        /// <param name="states">The number of states.</param>
        /// <param name="symbols">The number of symbols.</param>
        /// <param name="outputClasses">The number of output classes.</param>
        ///
        public HiddenMarkovClassifierFunction(int states, int symbols, int outputClasses)
        {
            this.Symbols = symbols;
            this.Outputs = outputClasses;

            var l = new List <double>();
            var f = new List <IFeature <int> >();

            this.Factors = new FactorPotential <int> [Outputs];

            int index = 0;

            // Create features for initial class probabilities
            for (int c = 0; c < outputClasses; c++)
            {
                int parameters = 0;
                l.Add(Math.Log(1.0 / outputClasses));
                f.Add(new OutputFeature <int>(this, c, c));
                parameters++;

                // Create features for initial state probabilities
                for (int i = 0; i < states; i++)
                {
                    if (i == 0)
                    {
                        l.Add(Math.Log(1.0));
                    }
                    else
                    {
                        l.Add(Math.Log(0.0));
                    }
                    f.Add(new InitialFeature <int>(this, c, i));
                    parameters++;
                }

                // Create features for state transition probabilities
                for (int i = 0; i < states; i++)
                {
                    for (int j = 0; j < states; j++)
                    {
                        l.Add(Math.Log(1.0 / states));
                        f.Add(new TransitionFeature <int>(this, c, i, j));
                        parameters++;
                    }
                }

                // Create features for symbol emission probabilities
                for (int i = 0; i < states; i++)
                {
                    for (int j = 0; j < symbols; j++)
                    {
                        l.Add(Math.Log(1.0 / symbols));
                        f.Add(new EmissionFeature(this, c, i, j));
                        parameters++;
                    }
                }

                Factors[c] = new FactorPotential <int>(this, index, states, parameters);
                index     += parameters;
            }

            this.Features = f.ToArray();
            this.Weights  = l.ToArray();
        }