Hidden Markov Model result(decoding )changes everytime

590 views Asked by At

I use HMM to predict the behavior of humans. Hidden states are Rest walk eat and observations are inside, outside and snack respectively.

hState = ['Rset', 'Walk', 'Eat']
Obs =  ['Inside 1', 'Outside 2', 'Snack 3']
order = [1, 2, 1, 2, 3 ,2, 2, 3, 1, 2 ,2, 2, 1, 2]
no = 3
sProb = np.array([0.1, 0.8, 0.1])
tProb = np.array([[0.2, 0.2, 0.6], 
                     [0.1, 0.8, 0.1], 
                     [0.6, 0.1, 0.3]])
eProb = np.array([[0.7, 0.2, 0.1],  
                    [0.1, 0.6, 0.3],
                    [0.2, 0.3, 0.5]])
h = hmm.MultinomialHMM(3, "full", sProb, tProb)
h.emissionprob_ = eProb
oreder1 = np.array([order]).T
result= hmm.GaussianHMM(n_components=no).fit(oreder1)
result.predict(oreder1)

But the result is changed whenever I run the code again. Sequences of the result are correct but the hidden state id is change The result of the first time run

array([2, 1, 2, 1, 0, 1, 1, 0, 2, 1, 1, 1, 2, 1])

The result of the second time run

array([0, 1, 0, 1, 2, 1, 1, 2, 0, 1, 1, 1, 0, 1])

What I want to know is how can I get constant value for each run or how to finalize the model. Thank you for your answer in advance.

1

There are 1 answers

3
Gordon Bean On BEST ANSWER

Hidden Markov Models (HMMs) are stochastic - they use a random-number generator to help make decisions. So if you run the code multiple times, each execution will be different because the random numbers used behind the scenes are different.

If you want to make each run use the same sequence of random numbers, you'll need to set the random number "seed". Look in the hmm documentation for how to set the seed. Setting it to a fixed value will ensure that each execution uses the same series of random numbers and should result in identical results for each run.

The question of finalizing the model is way beyond the scope of this site. Essentially, you'll have to learn about HMMs, how they work, what assumptions they make, and how your use-case fits (or doesn't!) those assumptions. Then you use your best judgement combined with rigorous testing/training to determine the final parameterization.