本文整理汇总了C#中HiddenMarkovClassifier.Compute方法的典型用法代码示例。如果您正苦于以下问题:C# HiddenMarkovClassifier.Compute方法的具体用法?C# HiddenMarkovClassifier.Compute怎么用?C# HiddenMarkovClassifier.Compute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HiddenMarkovClassifier
的用法示例。
在下文中一共展示了HiddenMarkovClassifier.Compute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LearnTest1
public void LearnTest1()
{
// Create a Continuous density Hidden Markov Model Sequence Classifier
// to detect a univariate sequence and the same sequence backwards.
double[][] sequences = new double[][]
{
new double[] { 0,1,2,3,4 }, // This is the first sequence with label = 0
new double[] { 4,3,2,1,0 }, // This is the second sequence with label = 1
};
// Labels for the sequences
int[] labels = { 0, 1 };
// Creates a sequence classifier containing 2 hidden Markov Models
// with 2 states and an underlying Normal distribution as density.
NormalDistribution density = new NormalDistribution();
var classifier = new HiddenMarkovClassifier<NormalDistribution>(2, new Ergodic(2), density);
// Configure the learning algorithms to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning<NormalDistribution>(classifier,
// Train each model until the log-likelihood changes less than 0.001
modelIndex => new BaumWelchLearning<NormalDistribution>(classifier.Models[modelIndex])
{
Tolerance = 0.0001,
Iterations = 0
}
);
// Train the sequence classifier using the algorithm
double logLikelihood = teacher.Run(sequences, labels);
// Calculate the probability that the given
// sequences originated from the model
double likelihood1, likelihood2;
// Try to classify the first sequence (output should be 0)
int c1 = classifier.Compute(sequences[0], out likelihood1);
// Try to classify the second sequence (output should be 1)
int c2 = classifier.Compute(sequences[1], out likelihood2);
Assert.AreEqual(0, c1);
Assert.AreEqual(1, c2);
Assert.AreEqual(-13.271981026832929, logLikelihood, 1e-10);
Assert.AreEqual(0.99999791320102149, likelihood1, 1e-10);
Assert.AreEqual(0.99999791320102149, likelihood2, 1e-10);
Assert.IsFalse(double.IsNaN(logLikelihood));
Assert.IsFalse(double.IsNaN(likelihood1));
Assert.IsFalse(double.IsNaN(likelihood2));
}
示例2: Recognize
public int Recognize(ISoundSignalReader signal, HiddenMarkovClassifier hmm, out string name,
SignalVisitor voiceVisitor = null)
{
var featureUtility = new FeatureUtility(_engineParameters);
signal.Reset();
var features = featureUtility.ExtractFeatures(signal, voiceVisitor).First();
var observations = _codeBook.Quantize(features.Select(item => new Point(item)).ToArray());
double[] responsabilities;
var ret = hmm.Compute(observations, out responsabilities);
var models = hmm.Models;
var likelyHoodValue = Double.MinValue;
name = string.Empty;
foreach (var model in models)
{
var val = model.Evaluate(observations);
if (val > likelyHoodValue)
{
likelyHoodValue = val;
name = model.Tag.ToString();
}
}
return ret;
}
示例3: button1_Click
private void button1_Click(object sender, EventArgs e)
{
var classes = 4;
var states = new[]{1,2,2,3};
var cat = new[] {"ខ្ញុំ", "ទៅ", "ខ្លួន", "ក"};
//var cat = new[] { "A", "B" };
_hmmc = new HiddenMarkovClassifier(classes, states, 4, cat);
// Train the ensemble
var sequences = new[]
{
new[] {1, 1, 1},
new[] {0, 2},
new[] {0, 1, 2},
new[] {1, 2}
};
var labels = new[] {0, 1, 2, 3};
var teacher = new HiddenMarkovClassifierLearning(_hmmc, i =>
new BaumWelchLearning(_hmmc.Models[i])
{
Iterations = 0,
Tolerance = 0.0001
}
);
teacher.Run(sequences, labels);
var m = _hmmc.Models;
var test = new[]{1,2};
double likelihood;
var label = _hmmc.Compute(test, out likelihood);
MessageBox.Show(_hmmc.Models[label].Tag.ToString()+ " P =" + likelihood);
}
示例4: 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));
}
}
}
示例5: RecognizeAsync
public void RecognizeAsync(ISoundSignalReader signal, HiddenMarkovClassifier hmm, Action<string> handleMessage,
SignalVisitor voiceVisitor = null)
{
Action<List<double[]>> action = features =>
{
var observations = _codeBook.Quantize(features.Select(item => new Point(item)).ToArray());
double[] responsabilities;
var ret = hmm.Compute(observations, out responsabilities);
var models = hmm.Models;
var likelyHoodValue = Double.MinValue;
foreach (var model in models)
{
var val = model.Evaluate(observations);
if (val > likelyHoodValue)
{
likelyHoodValue = val;
}
}
handleMessage(hmm[ret].Tag.ToString());
};
var featureUtility = new FeatureUtility(_engineParameters);
featureUtility.ExtractFeaturesAsync(signal, action, voiceVisitor);
}
示例6: LearnTest2
public void LearnTest2()
{
// 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
};
// We are trying to predict two different classes
int classes = 2;
// Each sequence may have up to 3 symbols (0,1,2)
int symbols = 3;
// Nested models will have 3 states each
int[] states = new int[] { 3, 3 };
// Creates a new Hidden Markov Model Classifier with the given parameters
HiddenMarkovClassifier classifier = new HiddenMarkovClassifier(classes, states, symbols);
// Create a new learning algorithm to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning(classifier,
// Train each model until the log-likelihood changes less than 0.001
modelIndex => new BaumWelchLearning(classifier.Models[modelIndex])
{
Tolerance = 0.001,
Iterations = 0
}
);
// Enable support for sequence rejection
teacher.Rejection = true;
// Train the sequence classifier using the algorithm
double likelihood = teacher.Run(inputs, outputs);
HiddenMarkovModel threshold = classifier.Threshold;
Assert.AreEqual(6, threshold.States);
Assert.AreEqual(classifier.Models[0].Transitions[0, 0], threshold.Transitions[0, 0], 1e-10);
Assert.AreEqual(classifier.Models[0].Transitions[1, 1], threshold.Transitions[1, 1], 1e-10);
Assert.AreEqual(classifier.Models[0].Transitions[2, 2], threshold.Transitions[2, 2], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[0, 0], threshold.Transitions[3, 3], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[1, 1], threshold.Transitions[4, 4], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[2, 2], threshold.Transitions[5, 5], 1e-10);
for (int i = 0; i < 3; i++)
for (int j = 3; j < 6; j++)
Assert.AreEqual(Double.NegativeInfinity, threshold.Transitions[i, j]);
for (int i = 3; i < 6; i++)
for (int j = 0; j < 3; j++)
Assert.AreEqual(Double.NegativeInfinity, threshold.Transitions[i, j]);
Assert.IsFalse(Matrix.HasNaN(threshold.Transitions));
classifier.Sensitivity = 0.5;
// Will assert the models have learned the sequences correctly.
for (int i = 0; i < inputs.Length; i++)
{
int expected = outputs[i];
int actual = classifier.Compute(inputs[i], out likelihood);
Assert.AreEqual(expected, actual);
}
int[] r0 = new int[] { 1, 1, 0, 0, 2 };
double logRejection;
int c = classifier.Compute(r0, out logRejection);
Assert.AreEqual(-1, c);
Assert.AreEqual(0.99906957195279988, logRejection);
Assert.IsFalse(double.IsNaN(logRejection));
logRejection = threshold.Evaluate(r0);
Assert.AreEqual(-4.5653702970734793, logRejection, 1e-10);
Assert.IsFalse(double.IsNaN(logRejection));
//.........这里部分代码省略.........
示例7: LearnTest9
public void LearnTest9()
{
double[][][] inputs = large_gestures;
int[] outputs = large_outputs;
int states = 5;
int iterations = 100;
double tolerance = 0.01;
bool rejection = true;
double sensitivity = 1E-85;
int dimension = inputs[0][0].Length;
var hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(2,
new Forward(states), new MultivariateNormalDistribution(dimension));
// Create the learning algorithm for the ensemble classifier
var teacher = new HiddenMarkovClassifierLearning<MultivariateNormalDistribution>(hmm,
// Train each model using the selected convergence criteria
i => new BaumWelchLearning<MultivariateNormalDistribution>(hmm.Models[i])
{
Tolerance = tolerance,
Iterations = iterations,
FittingOptions = new NormalOptions()
{
Regularization = 1e-5
}
}
);
teacher.Empirical = true;
teacher.Rejection = rejection;
// Run the learning algorithm
double logLikelihood = teacher.Run(inputs, outputs);
hmm.Sensitivity = sensitivity;
for (int i = 0; i < large_gestures.Length; i++)
{
int actual = hmm.Compute(large_gestures[i]);
int expected = large_outputs[i];
Assert.AreEqual(expected,actual);
}
}
示例8: SimpleGestureRecognitionTest
//.........这里部分代码省略.........
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 },
};
// Here, please note that a real-world example would involve *lots*
// of samples for each word. Here, we are considering just one from
// each class which is clearly sub-optimal and should _never_ be done
// on practice. For example purposes, however, please disregard this.
// Those are the words we have in our vocabulary:
//
double[][][] words = { hello, car, wardrobe };
// Now, let's associate integer labels with them. This is needed
// for the case where there are multiple samples for each word.
//
int[] labels = { 0, 1, 2 };
// We will create our classifiers assuming an independent
// Gaussian distribution for each component in our feature
// vectors (like assuming a Naive Bayes assumption).
Independent initial = new Independent
(
new NormalDistribution(0, 1),
new NormalDistribution(0, 1),
new NormalDistribution(0, 1),
new NormalDistribution(0, 1)
);
// Now, we can proceed and create our classifier.
//
int numberOfWords = 3; // we are trying to distinguish between 3 words
int numberOfStates = 5; // this value can be found by trial-and-error
var classifier = new HiddenMarkovClassifier<Independent>
(
classes: numberOfWords,
topology: new Forward(numberOfStates), // word classifiers should use a forward topology
initial: initial
);
// Create a new learning algorithm to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning<Independent>(classifier,
// Train each model until the log-likelihood changes less than 0.001
modelIndex => new BaumWelchLearning<Independent>(classifier.Models[modelIndex])
{
Tolerance = 0.001,
Iterations = 100,
// This is necessary so the code doesn't blow up when it realize
// there is only one sample per word class. But this could also be
// needed in normal situations as well.
//
FittingOptions = new IndependentOptions()
{
InnerOption = new NormalOptions() { Regularization = 1e-5 }
}
}
);
// Finally, we can run the learning algorithm!
double logLikelihood = teacher.Run(words, labels);
// At this point, the classifier should be successfully
// able to distinguish between our three word classes:
//
int c1 = classifier.Compute(hello);
int c2 = classifier.Compute(car);
int c3 = classifier.Compute(wardrobe);
Assert.AreEqual(0, c1);
Assert.AreEqual(1, c2);
Assert.AreEqual(2, c3);
}
示例9: LearnTest
public void LearnTest()
{
// Declare some testing data
int[][] inputs = new int[][]
{
new int[] { 0,1,1,0 }, // Class 0
new int[] { 0,0,1,0 }, // Class 0
new int[] { 0,1,1,1,0 }, // Class 0
new int[] { 0,1,0 }, // Class 0
new int[] { 1,0,0,1 }, // Class 1
new int[] { 1,1,0,1 }, // Class 1
new int[] { 1,0,0,0,1 }, // Class 1
new int[] { 1,0,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
};
// We are trying to predict two different classes
int classes = 2;
// Each sequence may have up to two symbols (0 or 1)
int symbols = 2;
// Nested models will have two states each
int[] states = new int[] { 2, 2 };
// Creates a new Hidden Markov Model Classifier with the given parameters
HiddenMarkovClassifier classifier = new HiddenMarkovClassifier(classes, states, symbols);
// Create a new learning algorithm to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning(classifier,
// Train each model until the log-likelihood changes less than 0.001
modelIndex => new BaumWelchLearning(classifier.Models[modelIndex])
{
Tolerance = 0.001,
Iterations = 0
}
);
// Train the sequence classifier using the algorithm
double likelihood = teacher.Run(inputs, outputs);
// Will assert the models have learned the sequences correctly.
for (int i = 0; i < inputs.Length; i++)
{
int expected = outputs[i];
int actual = classifier.Compute(inputs[i], out likelihood);
Assert.AreEqual(expected, actual);
}
}
示例10: LearnTest4
public void LearnTest4()
{
// Create a Continuous density Hidden Markov Model Sequence Classifier
// to detect a multivariate sequence and the same sequence backwards.
double[][][] sequences = new double[][][]
{
new double[][]
{
// This is the first sequence with label = 0
new double[] { 0 },
new double[] { 1 },
new double[] { 2 },
new double[] { 3 },
new double[] { 4 },
},
new double[][]
{
// This is the second sequence with label = 1
new double[] { 4 },
new double[] { 3 },
new double[] { 2 },
new double[] { 1 },
new double[] { 0 },
}
};
// Labels for the sequences
int[] labels = { 0, 1 };
// Create a mixture of two 1-dimensional normal distributions (by default,
// initialized with zero mean and unit covariance matrices).
var density = new MultivariateMixture<MultivariateNormalDistribution>(
new MultivariateNormalDistribution(1),
new MultivariateNormalDistribution(1));
// Creates a sequence classifier containing 2 hidden Markov Models with 2 states
// and an underlying multivariate mixture of Normal distributions as density.
var classifier = new HiddenMarkovClassifier<MultivariateMixture<MultivariateNormalDistribution>>(
2, new Ergodic(2), density);
// Configure the learning algorithms to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning<MultivariateMixture<MultivariateNormalDistribution>>(
classifier,
// Train each model until the log-likelihood changes less than 0.0001
modelIndex => new BaumWelchLearning<MultivariateMixture<MultivariateNormalDistribution>>(
classifier.Models[modelIndex])
{
Tolerance = 0.0001,
Iterations = 0,
}
);
// Train the sequence classifier using the algorithm
double logLikelihood = teacher.Run(sequences, labels);
// Calculate the probability that the given
// sequences originated from the model
double likelihood1, likelihood2;
// Try to classify the 1st sequence (output should be 0)
int c1 = classifier.Compute(sequences[0], out likelihood1);
// Try to classify the 2nd sequence (output should be 1)
int c2 = classifier.Compute(sequences[1], out likelihood2);
Assert.AreEqual(0, c1);
Assert.AreEqual(1, c2);
Assert.AreEqual(-13.271981026832933, logLikelihood, 1e-10);
Assert.AreEqual(0.99999791320102149, likelihood1, 1e-10);
Assert.AreEqual(0.99999791320102149, likelihood2, 1e-10);
Assert.IsFalse(double.IsNaN(logLikelihood));
Assert.IsFalse(double.IsNaN(likelihood1));
Assert.IsFalse(double.IsNaN(likelihood2));
}
示例11: btnTrain_Click
private void btnTrain_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("Please load or insert some data first.");
return;
}
int states = (int)numStates.Value;
int iterations = (int)numIterations.Value;
double tolerance = (double)numConvergence.Value;
if (rbStopIterations.Checked) tolerance = 0.0;
if (rbStopConvergence.Checked) iterations = 0;
// Retrieve the training data from the data grid view
int rows = dataGridView1.Rows.Count;
int[] outputs = new int[rows];
var sequences = new int[rows][];
for (int i = 0; i < rows; i++)
{
outputs[i] = (int)dataGridView1.Rows[i].Cells["colLabel"].Value - 1;
sequences[i] = GetFeatures((double[][])dataGridView1.Rows[i].Tag);
}
int classes = outputs.Distinct().Count();
string[] labels = new string[classes];
for (int i = 0; i < labels.Length; i++)
labels[i] = (i+1).ToString();
// Create a sequence classifier for 3 classes
classifier = new HiddenMarkovClassifier(labels.Length,
new Forward(states), symbols: 20, names: labels);
// Create the learning algorithm for the ensemble classifier
var teacher = new HiddenMarkovClassifierLearning(classifier,
// Train each model using the selected convergence criteria
i => new BaumWelchLearning(classifier.Models[i])
{
Tolerance = tolerance,
Iterations = iterations,
}
);
// Create and use a rejection threshold model
teacher.Rejection = cbRejection.Checked;
teacher.Empirical = true;
teacher.Smoothing = (double)numSmoothing.Value;
// Run the learning algorithm
teacher.Run(sequences, outputs);
double error = classifier.LogLikelihood(sequences, outputs);
int hits = 0;
toolStripProgressBar1.Visible = true;
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Step = 1;
toolStripProgressBar1.Maximum = dataGridView1.Rows.Count;
for (int i = 0; i < rows; i++)
{
double likelihood;
int index = classifier.Compute(sequences[i], out likelihood);
DataGridViewRow row = dataGridView1.Rows[i];
if (index == -1)
{
row.Cells["colClassification"].Value = String.Empty;
}
else
{
row.Cells["colClassification"].Value = classifier.Models[index].Tag;
}
int expected = (int)row.Cells["colLabel"].Value;
if (expected == index + 1)
{
row.Cells[0].Style.BackColor = Color.LightGreen;
row.Cells[1].Style.BackColor = Color.LightGreen;
row.Cells[2].Style.BackColor = Color.LightGreen;
hits++;
}
else
{
row.Cells[0].Style.BackColor = Color.White;
row.Cells[1].Style.BackColor = Color.White;
row.Cells[2].Style.BackColor = Color.White;
}
//.........这里部分代码省略.........
示例12: testThresholdModel
private static double testThresholdModel(double[][] inputs, int[] outputs, HiddenMarkovClassifier<GeneralDiscreteDistribution> classifier, double likelihood)
{
var threshold = classifier.Threshold;
Assert.AreEqual(classifier.Models[0].Transitions[0, 0], threshold.Transitions[0, 0], 1e-10);
Assert.AreEqual(classifier.Models[0].Transitions[1, 1], threshold.Transitions[1, 1], 1e-10);
Assert.AreEqual(classifier.Models[0].Transitions[2, 2], threshold.Transitions[2, 2], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[0, 0], threshold.Transitions[3, 3], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[1, 1], threshold.Transitions[4, 4], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[2, 2], threshold.Transitions[5, 5], 1e-10);
for (int i = 0; i < 3; i++)
for (int j = 3; j < 6; j++)
Assert.AreEqual(Double.NegativeInfinity, threshold.Transitions[i, j]);
for (int i = 3; i < 6; i++)
for (int j = 0; j < 3; j++)
Assert.AreEqual(Double.NegativeInfinity, threshold.Transitions[i, j]);
Assert.IsFalse(Matrix.HasNaN(threshold.Transitions));
classifier.Sensitivity = 0.5;
// Will assert the models have learned the sequences correctly.
for (int i = 0; i < inputs.Length; i++)
{
int expected = outputs[i];
int actual = classifier.Compute(inputs[i], out likelihood);
Assert.AreEqual(expected, actual);
}
double[] r0 = new double[] { 1, 1, 0, 0, 2 };
double logRejection;
int c = classifier.Compute(r0, out logRejection);
Assert.AreEqual(-1, c);
Assert.AreEqual(0.99993993054384978, logRejection);
Assert.IsFalse(double.IsNaN(logRejection));
logRejection = threshold.Evaluate(r0);
Assert.AreEqual(-5.6367018741984483, logRejection);
Assert.IsFalse(double.IsNaN(logRejection));
threshold.Decode(r0, out logRejection);
Assert.AreEqual(-8.1618027917853073, logRejection);
Assert.IsFalse(double.IsNaN(logRejection));
foreach (var model in classifier.Models)
{
double[,] A = model.Transitions;
for (int i = 0; i < A.GetLength(0); i++)
{
double[] row = A.Exp().GetRow(i);
double sum = row.Sum();
Assert.AreEqual(1, sum, 1e-10);
}
}
{
double[,] A = classifier.Threshold.Transitions;
for (int i = 0; i < A.GetLength(0); i++)
{
double[] row = A.GetRow(i);
double sum = row.Exp().Sum();
Assert.AreEqual(1, sum, 1e-6);
}
}
return likelihood;
}
示例13: testThresholdModel
private static double testThresholdModel(int[][] inputs, int[] outputs, HiddenMarkovClassifier classifier, double likelihood)
{
HiddenMarkovModel threshold = classifier.Threshold;
Assert.AreEqual(6, threshold.States);
Assert.AreEqual(classifier.Models[0].Transitions[0, 0], threshold.Transitions[0, 0], 1e-10);
Assert.AreEqual(classifier.Models[0].Transitions[1, 1], threshold.Transitions[1, 1], 1e-10);
Assert.AreEqual(classifier.Models[0].Transitions[2, 2], threshold.Transitions[2, 2], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[0, 0], threshold.Transitions[3, 3], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[1, 1], threshold.Transitions[4, 4], 1e-10);
Assert.AreEqual(classifier.Models[1].Transitions[2, 2], threshold.Transitions[5, 5], 1e-10);
for (int i = 0; i < 3; i++)
for (int j = 3; j < 6; j++)
Assert.AreEqual(Double.NegativeInfinity, threshold.Transitions[i, j]);
for (int i = 3; i < 6; i++)
for (int j = 0; j < 3; j++)
Assert.AreEqual(Double.NegativeInfinity, threshold.Transitions[i, j]);
Assert.IsFalse(Matrix.HasNaN(threshold.Transitions));
classifier.Sensitivity = 0.5;
// Will assert the models have learned the sequences correctly.
for (int i = 0; i < inputs.Length; i++)
{
int expected = outputs[i];
int actual = classifier.Compute(inputs[i], out likelihood);
Assert.AreEqual(expected, actual);
}
int[] r0 = new int[] { 1, 1, 0, 0, 2 };
double logRejection;
int c = classifier.Compute(r0, out logRejection);
Assert.AreEqual(-1, c);
Assert.AreEqual(0.99994164708402866, logRejection);
Assert.IsFalse(double.IsNaN(logRejection));
logRejection = threshold.Evaluate(r0);
Assert.AreEqual(-5.6077079936209504, logRejection, 1e-10);
Assert.IsFalse(double.IsNaN(logRejection));
threshold.Decode(r0, out logRejection);
Assert.AreEqual(-9.3103554170761686, logRejection, 1e-10);
Assert.IsFalse(double.IsNaN(logRejection));
foreach (var model in classifier.Models)
{
double[,] A = model.Transitions;
for (int i = 0; i < A.GetLength(0); i++)
{
double[] row = A.Exp().GetRow(i);
double sum = row.Sum();
Assert.AreEqual(1, sum, 1e-10);
}
}
{
double[,] A = classifier.Threshold.Transitions;
for (int i = 0; i < A.GetLength(0); i++)
{
double[] row = A.GetRow(i);
double sum = row.Exp().Sum();
Assert.AreEqual(1, sum, 1e-6);
}
}
return likelihood;
}
示例14: learnHMM
/// <summary>
/// learn Hmm model for samples in given database using Baum Welch unsupervised learning algorithm
/// then used the learned model to classify the training samples
/// </summary>
/// <param name="database"></param>
/// <returns></returns>
static HiddenMarkovClassifier<MultivariateNormalDistribution> learnHMM(Database database)
{
BindingList<Sequence> samples = database.Samples;
BindingList<String> classes = database.Classes;
double[][][] inputs = new double[samples.Count][][];
int[] outputs = new int[samples.Count];
for (int i = 0; i < inputs.Length; i++)
{
inputs[i] = samples[i].Input;
outputs[i] = samples[i].Output;
}
int states = 5;
int iterations = 0;
double tolerance = 0.1;
bool rejection = true;
HiddenMarkovClassifier<MultivariateNormalDistribution> hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(classes.Count,
new Forward(states), new MultivariateNormalDistribution(2), classes.ToArray());
// Create the learning algorithm for the ensemble classifier
var teacher = new HiddenMarkovClassifierLearning<MultivariateNormalDistribution>(hmm,
// Train each model using the selected convergence criteria
i => new BaumWelchLearning<MultivariateNormalDistribution>(hmm.Models[i])
{
Tolerance = tolerance,
Iterations = iterations,
FittingOptions = new NormalOptions()
{
Regularization = 1e-5
}
}
);
teacher.Empirical = true;
teacher.Rejection = rejection;
// Run the learning algorithm
double error = teacher.Run(inputs, outputs);
// Classify all training instances
foreach (var sample in database.Samples)
{
sample.RecognizedAs = hmm.Compute(sample.Input);
}
return hmm;
}
示例15: runDiscreteDensityHiddenMarkovClassifierLearningExample
static void runDiscreteDensityHiddenMarkovClassifierLearningExample()
{
// Observation sequences should only contain symbols that are greater than or equal to 0, and lesser than the number of symbols.
int[][] observationSequences =
{
// First class of sequences: starts and ends with zeros, ones in the middle.
new[] { 0, 1, 1, 1, 0 },
new[] { 0, 0, 1, 1, 0, 0 },
new[] { 0, 1, 1, 1, 1, 0 },
// Second class of sequences: starts with twos and switches to ones until the end.
new[] { 2, 2, 2, 2, 1, 1, 1, 1, 1 },
new[] { 2, 2, 1, 2, 1, 1, 1, 1, 1 },
new[] { 2, 2, 2, 2, 2, 1, 1, 1, 1 },
// Third class of sequences: can start with any symbols, but ends with three.
new[] { 0, 0, 1, 1, 3, 3, 3, 3 },
new[] { 0, 0, 0, 3, 3, 3, 3 },
new[] { 1, 0, 1, 2, 2, 2, 3, 3 },
new[] { 1, 1, 2, 3, 3, 3, 3 },
new[] { 0, 0, 1, 1, 3, 3, 3, 3 },
new[] { 2, 2, 0, 3, 3, 3, 3 },
new[] { 1, 0, 1, 2, 3, 3, 3, 3 },
new[] { 1, 1, 2, 3, 3, 3, 3 },
};
// Consider their respective class labels.
// Class labels have to be zero-based and successive integers.
int[] classLabels =
{
0, 0, 0, // Sequences 1-3 are from class 0.
1, 1, 1, // Sequences 4-6 are from class 1.
2, 2, 2, 2, 2, 2, 2, 2 // Sequences 7-14 are from class 2.
};
// Use a single topology for all inner models.
ITopology forward = new Forward(states: 3);
// Create a hidden Markov classifier with the given topology.
HiddenMarkovClassifier hmc = new HiddenMarkovClassifier(classes: 3, topology: forward, symbols: 4);
// Create a algorithms to teach each of the inner models.
var trainer = new HiddenMarkovClassifierLearning(
hmc,
// Specify individual training options for each inner model.
modelIndex => new BaumWelchLearning(hmc.Models[modelIndex])
{
Tolerance = 0.001, // iterate until log-likelihood changes less than 0.001.
Iterations = 0 // don't place an upper limit on the number of iterations.
}
);
// Call its Run method to start learning.
double averageLogLikelihood = trainer.Run(observationSequences, classLabels);
Console.WriteLine("average log-likelihood for the observations = {0}", averageLogLikelihood);
// Check the output classificaton label for some sequences.
int y1 = hmc.Compute(new[] { 0, 1, 1, 1, 0 }); // output is y1 = 0.
Console.WriteLine("output class = {0}", y1);
int y2 = hmc.Compute(new[] { 0, 0, 1, 1, 0, 0 }); // output is y2 = 0.
Console.WriteLine("output class = {0}", y2);
int y3 = hmc.Compute(new[] { 2, 2, 2, 2, 1, 1 }); // output is y3 = 1.
Console.WriteLine("output class = {0}", y3);
int y4 = hmc.Compute(new[] { 2, 2, 1, 1 }); // output is y4 = 1.
Console.WriteLine("output class = {0}", y4);
int y5 = hmc.Compute(new[] { 0, 0, 1, 3, 3, 3 }); // output is y5 = 2.
Console.WriteLine("output class = {0}", y4);
int y6 = hmc.Compute(new[] { 2, 0, 2, 2, 3, 3 }); // output is y6 = 2.
Console.WriteLine("output class = {0}", y6);
}