public void LogForwardTest() { HiddenMarkovClassifier <Independent> hmm = IndependentMarkovClassifierPotentialFunctionTest .CreateModel2(); MarkovMultivariateFunction function = new MarkovMultivariateFunction(hmm, includePriors: false); double[][][] observations = IndependentMarkovClassifierPotentialFunctionTest.sequences; int[] labels = IndependentMarkovClassifierPotentialFunctionTest.labels; foreach (double[][] x in observations) { foreach (int y in labels) { double[,] actual = new double[5, 2]; Accord.Statistics.Models.Fields. ForwardBackwardAlgorithm.LogForward(function.Factors[y], x, y, actual); double[,] expected = new double[5, 2]; Accord.Statistics.Models.Markov. ForwardBackwardAlgorithm.LogForward(hmm.Models[y], x, expected); for (int i = 0; i < actual.GetLength(0); i++) { for (int j = 0; j < actual.GetLength(1); j++) { Assert.AreEqual(expected[i, j], actual[i, j], 1e-10); } } } } }
public void GradientTest4() { var hmm = IndependentMarkovClassifierPotentialFunctionTest.CreateModel2(); var function = new MarkovMultivariateFunction(hmm); var model = new HiddenConditionalRandomField <double[]>(function); var target = new ForwardBackwardGradient <double[]>(model); target.Regularization = 0; FiniteDifferences diff = new FiniteDifferences(function.Weights.Length); diff.Function = parameters => func(model, parameters, IndependentMarkovClassifierPotentialFunctionTest.sequences, IndependentMarkovClassifierPotentialFunctionTest.labels); double[] expected = diff.Compute(function.Weights); double[] actual = target.Gradient(function.Weights, IndependentMarkovClassifierPotentialFunctionTest.sequences, IndependentMarkovClassifierPotentialFunctionTest.labels); for (int i = 0; i < actual.Length; i++) { if (double.IsNaN(expected[i])) { continue; } Assert.AreEqual(expected[i], actual[i], 1e-5); Assert.IsFalse(double.IsNaN(actual[i])); } }
public void ForwardTest2() { HiddenMarkovClassifier <Independent> hmm = IndependentMarkovClassifierPotentialFunctionTest .CreateModel3(); var function = new MarkovMultivariateFunction(hmm, includePriors: false); double[][][] observations = IndependentMarkovClassifierPotentialFunctionTest.sequences2; int[] labels = IndependentMarkovClassifierPotentialFunctionTest.labels2; foreach (double[][] x in observations) { foreach (int y in labels) { double[] scaling1; double logLikelihood1; double[,] actual = Accord.Statistics.Models.Fields. ForwardBackwardAlgorithm.Forward(function.Factors[y], x, y, out scaling1, out logLikelihood1); double[] scaling2; double logLikelihood2; double[,] expected = Accord.Statistics.Models.Markov. ForwardBackwardAlgorithm.Forward(hmm.Models[y], x, out scaling2, out logLikelihood2); for (int i = 0; i < actual.GetLength(0); i++) { for (int j = 0; j < actual.GetLength(1); j++) { Assert.AreEqual(expected[i, j], actual[i, j], 1e-10); Assert.IsFalse(Double.IsNaN(actual[i, j])); } } Assert.AreEqual(logLikelihood1, logLikelihood2, 1e-10); for (int i = 0; i < scaling1.Length; i++) { Assert.AreEqual(scaling1[i], scaling2[i], 1e-8); Assert.IsFalse(Double.IsNaN(scaling1[i])); Assert.IsFalse(Double.IsNaN(scaling2[i])); } } } }