Click or drag to resize
Accord.NET (logo)

HiddenMarkovClassifierLearning Class

Learning algorithm for discrete-density generative hidden Markov sequence classifiers.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.Models.Markov.LearningBaseHiddenMarkovClassifierLearningHiddenMarkovClassifier, HiddenMarkovModel, GeneralDiscreteDistribution, Int32
    Accord.Statistics.Models.Markov.LearningHiddenMarkovClassifierLearning

Namespace:  Accord.Statistics.Models.Markov.Learning
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
public class HiddenMarkovClassifierLearning : BaseHiddenMarkovClassifierLearning<HiddenMarkovClassifier, HiddenMarkovModel, GeneralDiscreteDistribution, int>
Request Example View Source

The HiddenMarkovClassifierLearning type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyAlgorithm Obsolete.
Obsolete.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertyClassifier
Gets the classifier being trained by this instance.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertyEmpirical
Gets or sets a value indicating whether the class priors should be estimated from the data, as in an empirical Bayes method.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertyLearner
Gets or sets the configuration function specifying which training algorithm should be used for each of the models in the hidden Markov model set.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertyLogLikelihood
Gets the log-likelihood at the end of the training.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertyParallelOptions
Gets or sets the parallelization options for this algorithm.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertyRejection
Gets or sets a value indicating whether a threshold model should be created or updated after training to support rejection.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public propertySmoothing
Gets or sets the smoothing kernel's sigma for the threshold model.
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Top
Methods
  NameDescription
Public methodComputeError
Compute model error for a given data set.
Protected methodCreate
Creates an instance of the model to be learned. Inheritors of this abstract class must define this method so new models can be created from the training data.
(Overrides BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservationCreate(TObservation, Int32, Int32).)
Protected methodCreateThresholdTopology
Creates the state transition topology for the threshold model. This method can be used to help in the implementation of the Threshold abstract method which has to be defined for implementers of this class.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn
Learns a model that can map the given inputs to the given outputs.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnGenerativeClassModelLearningFinished
Raises the [E:GenerativeClassModelLearningFinished] event.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Protected methodOnGenerativeClassModelLearningStarted
Raises the [E:GenerativeClassModelLearningStarted] event.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public methodRun(Int32, Int32) Obsolete.
Trains each model to recognize each of the output labels.
Protected methodRunT(T, Int32) Obsolete.
Trains each model to recognize each of the output labels.
(Inherited from BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservation.)
Public methodThreshold
Creates a new threshold model for the current set of Markov models in this sequence classifier.
(Overrides BaseHiddenMarkovClassifierLearningTClassifier, TModel, TDistribution, TObservationThreshold.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Events
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

This class acts as a teacher for classifiers based on discrete hidden Markov models. The learning algorithm uses a generative approach. It works by training each model in the generative classifier separately.

This class implements discrete classifiers only. Discrete classifiers can be used whenever the sequence of observations is discrete or can be represented by discrete symbols, such as class labels, integers, and so on. If you need to classify sequences of other entities, such as real numbers, vectors (i.e. multivariate observations), then you can use generic-density hidden Markov models. Those models can be modeled after any kind of probability distribution implementing the IDistribution interface.

For a more thorough explanation on hidden Markov models with practical examples on gesture recognition, please see Sequence Classifiers in C#, Part I: Hidden Markov Models [1].

[1]: http://www.codeproject.com/Articles/541428/Sequence-Classifiers-in-Csharp-Part-I-Hidden-Marko

Examples

The following example shows how to create a hidden Markov model sequence classifier to classify discrete sequences into two disjoint labels: labels for class 0 and labels for class 1. The training data is separated in inputs and outputs. The inputs are the sequences we are trying to learn, and the outputs are the labels associated with each input sequence.

In this example we will be using the Baum-Welch algorithm to learn each model in our generative classifier; however, any other unsupervised learning algorithm could be used.

// Declare some testing data
int[][] inputs = new int[][]
{
    new int[] { 0,1,2,0 },   // Class 0
    new int[] { 0,0,2,0 },   // Class 0
    new int[] { 0,1,2,1,0 }, // Class 0
    new int[] { 0,1,2,0 },     // Class 0

    new int[] { 1,0,2,1 },   // Class 1
    new int[] { 1,1,2,1 },   // Class 1
    new int[] { 1,0,2,0,1 }, // Class 1
    new int[] { 1,0,2,1 },     // Class 1
};

int[] outputs = new int[]
{
    0,0,0,0, // First four sequences are of class 0
    1,1,1,1, // Last four sequences are of class 1
};

// Create a new learning algorithm to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning()
{
    // Train each model until the log-likelihood changes less than 0.001
    Learner = (i) => new BaumWelchLearning()
    {
        Tolerance = 0.001,
        Iterations = 0,
        NumberOfStates = 2,
    }
};

// Train the sequence classifier 
HiddenMarkovClassifier classifier = teacher.Learn(inputs, outputs);

// Obtain classification labels for the output
int[] predicted = classifier.Decide(inputs);

// Obtain prediction scores for the outputs
double[] lls = classifier.LogLikelihood(inputs);

It is also possible to learn a hidden Markov classifier with support for rejection. When a classifier is configured to use rejection, it will be able to detect when a sample does not belong to any of the classes that it has previously seen.

// Declare some testing data
int[][] inputs = new int[][]
{
    new int[] { 0,0,1,2 },     // Class 0
    new int[] { 0,1,1,2 },     // Class 0
    new int[] { 0,0,0,1,2 },   // Class 0
    new int[] { 0,1,2,2,2 },   // Class 0

    new int[] { 2,2,1,0 },     // Class 1
    new int[] { 2,2,2,1,0 },   // Class 1
    new int[] { 2,2,2,1,0 },   // Class 1
    new int[] { 2,2,2,2,1 },   // Class 1
};

int[] outputs = new int[]
{
    0,0,0,0, // First four sequences are of class 0
    1,1,1,1, // Last four sequences are of class 1
};


// Create a new learning algorithm to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning()
{
    Learner = (i) => new BaumWelchLearning()
    {
        NumberOfStates = 3,
        Tolerance = 0.001,
        Iterations = 0,
    },

    Rejection = true // Enable support for sequence rejection
};

// Train the sequence classifier 
var classifier = teacher.Learn(inputs, outputs);

// Obtain prediction classes for the outputs
int[] prediction = classifier.Decide(inputs);

// Obtain prediction scores for the outputs
double[] lls = classifier.LogLikelihood(inputs);
See Also