[mlpack-git] master: Fix HMM initial state estimation. (d6d38a1)

gitdub at mlpack.org gitdub at mlpack.org
Tue Apr 12 11:43:08 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/78f53daf5a91e221432c93ea43fbdc9de2ec2f15...d6d38a1a98dee794bf14c67fcb7aa709176181bb

>---------------------------------------------------------------

commit d6d38a1a98dee794bf14c67fcb7aa709176181bb
Author: Ryan Curtin <ryan at ratml.org>
Date:   Tue Apr 12 11:43:08 2016 -0400

    Fix HMM initial state estimation.


>---------------------------------------------------------------

d6d38a1a98dee794bf14c67fcb7aa709176181bb
 src/mlpack/methods/hmm/hmm_impl.hpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/mlpack/methods/hmm/hmm_impl.hpp b/src/mlpack/methods/hmm/hmm_impl.hpp
index 1e05cb5..1d005f0 100644
--- a/src/mlpack/methods/hmm/hmm_impl.hpp
+++ b/src/mlpack/methods/hmm/hmm_impl.hpp
@@ -127,19 +127,20 @@ void HMM<Distribution>::Train(const std::vector<arma::mat>& dataSeq)
       // Add the log-likelihood of this sequence.  This is the E-step.
       loglik += Estimate(dataSeq[seq], stateProb, forward, backward, scales);
 
+      // Add to estimate of initial probability for state j.
+      for (size_t j = 0; j < transition.n_cols; ++j)
+        newInitial[j] += stateProb(j, 0);
+
       // Now re-estimate the parameters.  This is the M-step.
       //   pi_i = sum_d ((1 / P(seq[d])) sum_t (f(i, 0) b(i, 0))
       //   T_ij = sum_d ((1 / P(seq[d])) sum_t (f(i, t) T_ij E_i(seq[d][t]) b(i,
       //           t + 1)))
       //   E_ij = sum_d ((1 / P(seq[d])) sum_{t | seq[d][t] = j} f(i, t) b(i, t)
       // We store the new estimates in a different matrix.
-      for (size_t t = 0; t < dataSeq[seq].n_cols; t++)
+      for (size_t t = 0; t < dataSeq[seq].n_cols; ++t)
       {
-        for (size_t j = 0; j < transition.n_cols; j++)
+        for (size_t j = 0; j < transition.n_cols; ++j)
         {
-          // Add to estimate of initial probability for state j.
-          newInitial[j] = stateProb(j, 0);
-
           if (t < dataSeq[seq].n_cols - 1)
           {
             // Estimate of T_ij (probability of transition from state j to state
@@ -159,7 +160,7 @@ void HMM<Distribution>::Train(const std::vector<arma::mat>& dataSeq)
     }
 
     // Normalize the new initial probabilities.
-    if (dataSeq.size() == 0)
+    if (dataSeq.size() > 1)
       initial = newInitial / dataSeq.size();
 
     // Assign the new transition matrix.  We use %= (element-wise




More information about the mlpack-git mailing list