本文整理汇总了C#中HiddenConditionalRandomField类的典型用法代码示例。如果您正苦于以下问题:C# HiddenConditionalRandomField类的具体用法?C# HiddenConditionalRandomField怎么用?C# HiddenConditionalRandomField使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HiddenConditionalRandomField类属于命名空间,在下文中一共展示了HiddenConditionalRandomField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HiddenConditionalRandomFieldConstructorTest
public void HiddenConditionalRandomFieldConstructorTest()
{
HiddenMarkovClassifier hmm = HiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MarkovDiscreteFunction(hmm);
var target = new HiddenConditionalRandomField<int>(function);
Assert.AreEqual(function, target.Function);
Assert.AreEqual(2, target.Function.Factors[0].States);
}
示例2: ComputeTest
public void ComputeTest()
{
HiddenMarkovClassifier hmm = DiscreteHiddenMarkovClassifierPotentialFunctionTest.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));
}
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例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: GradientTest3
public void GradientTest3()
{
var hmm = MultivariateNormalHiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MarkovMultivariateFunction(hmm);
var model = new HiddenConditionalRandomField<double[]>(function);
var target = new ForwardBackwardGradient<double[]>(model);
target.Regularization = 2;
var inputs = inputs1;
var outputs = outputs1;
FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);
diff.Function = parameters => func(model, parameters, inputs, outputs, target.Regularization);
double[] expected = diff.Compute(function.Weights);
double[] actual = target.Gradient(function.Weights, inputs, outputs);
for (int i = 0; i < actual.Length; i++)
{
Assert.AreEqual(expected[i], actual[i], 1e-3);
Assert.IsFalse(double.IsNaN(actual[i]));
Assert.IsFalse(double.IsNaN(expected[i]));
}
}
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:30,代码来源:MultivariateNormalQuasiNewtonHiddenLearningTest.cs
示例8: GradientTest
public void GradientTest()
{
// Creates a sequence classifier containing 2 hidden Markov Models
// with 2 states and an underlying Normal distribution as density.
MultivariateNormalDistribution density = new MultivariateNormalDistribution(3);
var hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(2, new Ergodic(2), density);
double[][][] inputs =
{
new [] { new double[] { 0, 1, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 1, 0 } },
new [] { new double[] { 1, 6, 2 }, new double[] { 2, 1, 6 }, new double[] { 1, 1, 0 } },
new [] { new double[] { 9, 1, 0 }, new double[] { 0, 1, 5 }, new double[] { 0, 0, 0 } },
};
int[] outputs =
{
0, 0, 1
};
var function = new MarkovMultivariateFunction(hmm);
var model = new HiddenConditionalRandomField<double[]>(function);
var target = new ForwardBackwardGradient<double[]>(model);
FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);
diff.Function = parameters => func(model, parameters, inputs, outputs);
double[] expected = diff.Compute(function.Weights);
double[] actual = target.Gradient(function.Weights, inputs, outputs);
for (int i = 0; i < actual.Length; i++)
{
Assert.AreEqual(expected[i], actual[i], 0.05);
Assert.IsFalse(double.IsNaN(actual[i]));
Assert.IsFalse(double.IsNaN(expected[i]));
}
}
开发者ID:KommuSoft,项目名称:accord_framework,代码行数:39,代码来源:MultivariateNormalQuasiNewtonHiddenLearningTest.cs
示例9: ComputeTest4
public void ComputeTest4()
{
int[] labels;
double[][][] words;
HiddenMarkovClassifier<Independent<NormalDistribution>> model =
CreateModel4(out words, out labels, false);
var target = new MarkovMultivariateFunction(model);
var hcrf = new HiddenConditionalRandomField<double[]>(target);
Assert.AreEqual(3, model.Priors.Length);
Assert.AreEqual(1 / 3.0, model.Priors[0]);
Assert.AreEqual(1 / 3.0, model.Priors[1]);
Assert.AreEqual(1 / 3.0, model.Priors[2]);
check4(words, model, target, hcrf);
}
示例10: 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);
}
示例11: resilientgradienthiddenlearning
private static void resilientgradienthiddenlearning()
{
// Suppose we would like to learn how to classify the
// following set of sequences among three class labels:
int[][] inputSequences =
{
// 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 },
};
// Now consider their respective class labels
int[] outputLabels =
{
/* Sequences 1-3 are from class 0: */ 0, 0, 0,
/* Sequences 4-6 are from class 1: */ 1, 1, 1,
/* Sequences 7-14 are from class 2: */ 2, 2, 2, 2, 2, 2, 2, 2
};
// Create the Hidden Conditional Random Field using a set of discrete features
var function = new MarkovDiscreteFunction(states: 3, symbols: 4, outputClasses: 3);
var classifier = new HiddenConditionalRandomField<int>(function);
// Create a learning algorithm
var teacher = new HiddenResilientGradientLearning<int>(classifier)
{
Iterations = 50
};
// Run the algorithm and learn the models
teacher.Run(inputSequences, outputLabels);
int[] answers = inputSequences.Apply(classifier.Compute);
}
示例12: btnLearnHCRF_Click
private void btnLearnHCRF_Click(object sender, EventArgs e)
{
if (gridSamples.Rows.Count == 0)
{
MessageBox.Show("Please load or insert some data first.");
return;
}
var samples = database.Samples;
var 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 iterations = 100;
double tolerance = 0.01;
hcrf = new HiddenConditionalRandomField<double[]>(
new MarkovMultivariateFunction(hmm));
// Create the learning algorithm for the ensemble classifier
var teacher = new HiddenResilientGradientLearning<double[]>(hcrf)
{
Iterations = iterations,
Tolerance = tolerance
};
// Run the learning algorithm
double error = teacher.Run(inputs, outputs);
foreach (var sample in database.Samples)
{
sample.RecognizedAs = hcrf.Compute(sample.Input);
}
foreach (DataGridViewRow row in gridSamples.Rows)
{
var sample = row.DataBoundItem as Sequence;
row.DefaultCellStyle.BackColor = (sample.RecognizedAs == sample.Output) ?
Color.LightGreen : Color.White;
}
}
示例13: GradientTest2
public void GradientTest2()
{
HiddenMarkovClassifier hmm = HiddenMarkovClassifierPotentialFunctionTest.CreateModel1();
var function = new MarkovDiscreteFunction(hmm);
var model = new HiddenConditionalRandomField<int>(function);
var target = new ForwardBackwardGradient<int>(model);
FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);
diff.Function = parameters => func(model, parameters);
double[] expected = diff.Compute(function.Weights);
double[] actual = target.Gradient(function.Weights, inputs, outputs);
for (int i = 0; i < actual.Length; i++)
{
Assert.AreEqual(expected[i], actual[i], 1e-5);
Assert.IsFalse(double.IsNaN(actual[i]));
Assert.IsFalse(double.IsNaN(expected[i]));
}
}
示例14: GradientTest2
public void GradientTest2()
{
var hmm = CreateModel3();
var function = new MarkovMultivariateFunction(hmm);
var model = new HiddenConditionalRandomField<double[]>(function);
var target = new ForwardBackwardGradient<double[]>(model);
var inputs = sequences2;
var outputs = labels2;
double[] actual = target.Gradient(function.Weights, inputs, outputs);
FiniteDifferences diff = new FiniteDifferences(function.Weights.Length);
diff.Function = parameters => func(model, parameters, inputs, outputs);
double[] expected = diff.Compute(function.Weights);
for (int i = 0; i < actual.Length; i++)
{
Assert.AreEqual(expected[i], actual[i], 1e-3);
Assert.IsFalse(double.IsNaN(actual[i]));
Assert.IsFalse(double.IsNaN(expected[i]));
}
}
示例15: 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));
}
}
}
}