public void Iteration() { HiddenMarkovModel nhmm; nhmm = _method.Clone(); var allGamma = new double[_training.SequenceCount][][]; double[][] aijNum = EngineArray.AllocateDouble2D(_method.StateCount, _method.StateCount); var aijDen = new double[_method.StateCount]; EngineArray.Fill(aijDen, 0.0); for (int i = 0; i < _method.StateCount; i++) { EngineArray.Fill(aijNum[i], 0.0); } int g = 0; foreach (IMLDataSet obsSeq in _training.Sequences) { ForwardBackwardCalculator fbc = GenerateForwardBackwardCalculator( obsSeq, _method); double[][][] xi = EstimateXi(obsSeq, fbc, _method); double[][] gamma = allGamma[g++] = EstimateGamma(xi, fbc); for (int i = 0; i < _method.StateCount; i++) { for (int t = 0; t < (obsSeq.Count - 1); t++) { aijDen[i] += gamma[t][i]; for (int j = 0; j < _method.StateCount; j++) { aijNum[i][j] += xi[t][i][j]; } } } } for (int i = 0; i < _method.StateCount; i++) { if (aijDen[i] == 0.0) { for (int j = 0; j < _method.StateCount; j++) { nhmm.TransitionProbability[i][j] = _method.TransitionProbability[i][j]; } } else { for (int j = 0; j < _method.StateCount; j++) { nhmm.TransitionProbability[i][j] = aijNum[i][j] / aijDen[i]; } } } /* compute pi */ for (int i = 0; i < _method.StateCount; i++) { nhmm.Pi[i] = 0.0; } for (int o = 0; o < _training.SequenceCount; o++) { for (int i = 0; i < _method.StateCount; i++) { nhmm.Pi[i] += (allGamma[o][0][i] / _training .SequenceCount); } } /* compute pdfs */ for (int i = 0; i < _method.StateCount; i++) { var weights = new double[_training.Count]; double sum = 0.0; int j = 0; int o = 0; foreach (IMLDataSet obsSeq in _training.Sequences) { for (int t = 0; t < obsSeq.Count; t++, j++) { sum += weights[j] = allGamma[o][t][i]; } o++; } for (j--; j >= 0; j--) { weights[j] /= sum; } IStateDistribution opdf = nhmm.StateDistributions[i]; opdf.Fit(_training, weights); } _method = nhmm; Iterations++; }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// /// <returns> /// A new object that is a copy of this instance. /// </returns> /// public override object Clone() { return(model.Clone()); }
public void issue_154() { int[][] sequences = new int[][] { new int[] { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 }, new int[] { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1 }, new int[] { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, new int[] { 0, 1, 1, 1, 1, 1 }, new int[] { 0, 1, 1, 1, 1, 1, 1 }, new int[] { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, new int[] { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; var hmm0 = new HiddenMarkovModel(states: 3, symbols: 2); var hmm1 = hmm0.Clone() as HiddenMarkovModel; var hmm2 = hmm0.Clone() as HiddenMarkovModel; var hmm3 = hmm0.Clone() as HiddenMarkovModel; var hmm4 = hmm0.Clone() as HiddenMarkovModel; { Accord.Math.Random.Generator.Seed = 0; var teacher = new BaumWelchLearning(hmm1) { Tolerance = 0.0001, Iterations = 1 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(1, teacher.CurrentIteration); teacher = new BaumWelchLearning(hmm1) { Tolerance = 0.0001, Iterations = 1 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(1, teacher.CurrentIteration); teacher = new BaumWelchLearning(hmm1) { Tolerance = 0.0001, Iterations = 1 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(1, teacher.CurrentIteration); teacher = new BaumWelchLearning(hmm1) { Tolerance = 0.0001, Iterations = 1 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(1, teacher.CurrentIteration); } { Accord.Math.Random.Generator.Seed = 0; var teacher = new BaumWelchLearning(hmm2) { Tolerance = 0.0001, Iterations = 2 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(2, teacher.CurrentIteration); teacher.MaxIterations = 4; teacher.Learn(sequences); Assert.AreEqual(4, teacher.CurrentIteration); } { Accord.Math.Random.Generator.Seed = 0; var teacher = new BaumWelchLearning(hmm3) { Tolerance = 0.0001, Iterations = 3 }; teacher.Learn(sequences); Assert.AreEqual(3, teacher.CurrentIteration); teacher = new BaumWelchLearning(hmm3) { Tolerance = 0.0001, Iterations = 1 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(1, teacher.CurrentIteration); } { Accord.Math.Random.Generator.Seed = 0; var teacher = new BaumWelchLearning(hmm4) { Tolerance = 0.0001, Iterations = 4 }; Assert.AreEqual(0, teacher.CurrentIteration); teacher.Learn(sequences); Assert.AreEqual(4, teacher.CurrentIteration); } { var teacher = new BaumWelchLearning(hmm0) { Tolerance = 0.0001, Iterations = 0 }; teacher.Learn(sequences); Assert.AreEqual(13, teacher.CurrentIteration); } compare(hmm1, hmm2); compare(hmm1, hmm3); compare(hmm1, hmm4); }