本文整理汇总了C#中HiddenConditionalRandomField.LogLikelihood方法的典型用法代码示例。如果您正苦于以下问题:C# HiddenConditionalRandomField.LogLikelihood方法的具体用法?C# HiddenConditionalRandomField.LogLikelihood怎么用?C# HiddenConditionalRandomField.LogLikelihood使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HiddenConditionalRandomField
的用法示例。
在下文中一共展示了HiddenConditionalRandomField.LogLikelihood方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunTest
public void RunTest()
{
var inputs = QuasiNewtonHiddenLearningTest.inputs;
var outputs = QuasiNewtonHiddenLearningTest.outputs;
HiddenMarkovClassifier hmm = HiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MarkovDiscreteFunction(hmm);
var model = new HiddenConditionalRandomField<int>(function);
var target = new HiddenGradientDescentLearning<int>(model);
target.LearningRate = 1000;
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 ll0 = model.LogLikelihood(inputs, outputs);
double error = Double.NegativeInfinity;
for (int i = 0; i < 50; i++)
error = target.RunEpoch(inputs, outputs);
double ll1 = model.LogLikelihood(inputs, outputs);
for (int i = 0; i < inputs.Length; i++)
{
actual[i] = model.Compute(inputs[i]);
expected[i] = outputs[i];
}
Assert.AreEqual(-0.00046872579976353634, ll0, 1e-10);
Assert.AreEqual(0.00027018722449589916, error, 1e-10);
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);
}
示例2: RunTest
public void RunTest()
{
var inputs = QuasiNewtonHiddenLearningTest.inputs;
var outputs = QuasiNewtonHiddenLearningTest.outputs;
HiddenMarkovClassifier hmm = DiscreteHiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MarkovDiscreteFunction(hmm);
var model = new HiddenConditionalRandomField<int>(function);
var target = new HiddenConjugateGradientLearning<int>(model);
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 ll0 = model.LogLikelihood(inputs, outputs);
double error = target.Run(inputs, outputs);
double ll1 = model.LogLikelihood(inputs, outputs);
for (int i = 0; i < inputs.Length; i++)
{
actual[i] = model.Compute(inputs[i]);
expected[i] = outputs[i];
}
Assert.AreEqual(-0.0019419916698781847, ll0, 1e-10);
Assert.AreEqual(0.00050271005636426391, error, 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);
}
示例3: RunTest
public void RunTest()
{
var hmm = MarkovContinuousFunctionTest.CreateModel1();
var function = new MarkovContinuousFunction(hmm);
var model = new HiddenConditionalRandomField<double>(function);
var target = new HiddenQuasiNewtonLearning<double>(model);
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.0000041736023099758768, ll0, 1e-10);
for (int i = 0; i < inputs.Length; i++)
Assert.AreEqual(expected[i], actual[i]);
Assert.IsTrue(ll1 > ll0);
}
示例4: RunTest2
public void RunTest2()
{
var inputs = QuasiNewtonHiddenLearningTest.inputs;
var outputs = QuasiNewtonHiddenLearningTest.outputs;
Accord.Math.Tools.SetupGenerator(0);
var function = new MarkovDiscreteFunction(2, 2, 2);
var model = new HiddenConditionalRandomField<int>(function);
var target = new HiddenConjugateGradientLearning<int>(model);
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];
}
double ll0 = model.LogLikelihood(inputs, outputs);
double error = target.Run(inputs, outputs);
double ll1 = model.LogLikelihood(inputs, outputs);
for (int i = 0; i < inputs.Length; i++)
{
actual[i] = model.Compute(inputs[i]);
expected[i] = outputs[i];
}
Assert.AreEqual(-5.5451774444795623, ll0, 1e-10);
Assert.AreEqual(0, error, 1e-10);
Assert.IsFalse(double.IsNaN(error));
for (int i = 0; i < inputs.Length; i++)
Assert.AreEqual(expected[i], actual[i]);
Assert.IsTrue(ll1 > ll0);
}
示例5: func
private double func(HiddenConditionalRandomField<double[]> model, double[] parameters, double[][][] inputs, int[] outputs)
{
model.Function.Weights = parameters;
return -model.LogLikelihood(inputs, outputs);
}
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:5,代码来源:MultivariateNormalQuasiNewtonHiddenLearningTest.cs
示例6: RunTest
public void RunTest()
{
HiddenMarkovClassifier hmm = HiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MarkovDiscreteFunction(hmm);
var model = new HiddenConditionalRandomField<int>(function);
var target = new HiddenQuasiNewtonLearning<int>(model);
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 ll0 = model.LogLikelihood(inputs, outputs);
double error = target.Run(inputs, outputs);
double ll1 = model.LogLikelihood(inputs, outputs);
for (int i = 0; i < inputs.Length; i++)
{
actual[i] = model.Compute(inputs[i]);
expected[i] = outputs[i];
}
Assert.AreEqual(-0.00046872579976353634, ll0, 1e-10);
Assert.AreEqual(0.0, error, 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);
}
示例7: RunTest
public void RunTest()
{
var hmm = MultivariateNormalHiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MultivariateNormalMarkovClassifierFunction(hmm);
var model = new HiddenConditionalRandomField<double[]>(function);
var target = new QuasiNewtonHiddenLearning<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.RunEpoch(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);
}
示例8: ComputeTest3
public void ComputeTest3()
{
var model = CreateModel3();
var target = new MarkovMultivariateFunction(model);
var hcrf = new HiddenConditionalRandomField<double[]>(target);
double actual;
double expected;
for (int k = 0; k < 5; k++)
{
foreach (var x in sequences2)
{
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.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));
}
}
}
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));
}
}
}
}
示例9: 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));
}
}
}
示例10: 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));
}
}
示例11: ComputeTest
public void ComputeTest()
{
HiddenMarkovClassifier hmm = HiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
// 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
};
var function = new MarkovDiscreteFunction(hmm);
var target = new HiddenConditionalRandomField<int>(function);
for (int i = 0; i < inputs.Length; i++)
{
int expected = hmm.Compute(inputs[i]);
int actual = target.Compute(inputs[i]);
double h0 = hmm.LogLikelihood(inputs[i], 0);
double h1 = hmm.LogLikelihood(inputs[i], 1);
double c0 = target.LogLikelihood(inputs[i], 0);
double c1 = target.LogLikelihood(inputs[i], 1);
Assert.AreEqual(expected, actual);
Assert.AreEqual(h0, c0, 1e-10);
Assert.AreEqual(h1, c1, 1e-10);
Assert.IsFalse(double.IsNaN(c0));
Assert.IsFalse(double.IsNaN(c1));
}
}
示例12: SimpleGestureRecognitionTest
//.........这里部分代码省略.........
var hmm = new HiddenMarkovClassifier<Independent<NormalDistribution>>
(
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<NormalDistribution>>(hmm,
// Train each model until the log-likelihood changes less than 0.001
modelIndex => new BaumWelchLearning<Independent<NormalDistribution>>(hmm.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 tc1 = hmm.Compute(hello);
int tc2 = hmm.Compute(car);
int tc3 = hmm.Compute(wardrobe);
Assert.AreEqual(0, tc1);
Assert.AreEqual(1, tc2);
Assert.AreEqual(2, tc3);
// Now, we can use the Markov classifier to initialize a HCRF
var function = new MarkovMultivariateFunction(hmm);
var hcrf = new HiddenConditionalRandomField<double[]>(function);
// We can check that both are equivalent, although they have
// formulations that can be learned with different methods
//
for (int i = 0; i < words.Length; i++)
{
// Should be the same
int expected = hmm.Compute(words[i]);
int actual = hcrf.Compute(words[i]);
// Should be the same
double h0 = hmm.LogLikelihood(words[i], 0);
double c0 = hcrf.LogLikelihood(words[i], 0);
double h1 = hmm.LogLikelihood(words[i], 1);
double c1 = hcrf.LogLikelihood(words[i], 1);
double h2 = hmm.LogLikelihood(words[i], 2);
double c2 = hcrf.LogLikelihood(words[i], 2);
Assert.AreEqual(expected, actual);
Assert.AreEqual(h0, c0, 1e-10);
Assert.IsTrue(h1.IsRelativelyEqual(c1, 1e-10));
Assert.IsTrue(h2.IsRelativelyEqual(c2, 1e-10));
Assert.IsFalse(double.IsNaN(c0));
Assert.IsFalse(double.IsNaN(c1));
Assert.IsFalse(double.IsNaN(c2));
}
// Now we can learn the HCRF using one of the best learning
// algorithms available, Resilient Backpropagation learning:
// Create a learning algorithm
var rprop = new HiddenResilientGradientLearning<double[]>(hcrf)
{
Iterations = 50,
Tolerance = 1e-5
};
// Run the algorithm and learn the models
double error = rprop.Run(words, labels);
// At this point, the HCRF should be successfully
// able to distinguish between our three word classes:
//
int hc1 = hcrf.Compute(hello);
int hc2 = hcrf.Compute(car);
int hc3 = hcrf.Compute(wardrobe);
Assert.AreEqual(0, hc1);
Assert.AreEqual(1, hc2);
Assert.AreEqual(2, hc3);
}