Sashi
Sashi

Reputation: 89

Params argument in hmmlearn GMMHMM not working as intended

I have one-dimensional (single feature) data that I want to fit a GMMHMM to. There are two hidden states and I know the probability distribution of the output from each of the states. That is, I know the prior distribution and hence the GMM parameters. So I don't want the hmmlearn object to update the means, covars, weights of the GMMs.

I want to accomplish this using the params and init_params arguments by setting them to update only startprob and transmat.

But hmmlearn ends up updating the means, covars, weights as well. How do I stop it from updating these and get it to update only startprob and transmat?

Here's my code

# Initialize the GMMHMM

means_prior = known_means
covars_prior = known_covars
weights_prior = known_weights

gmm_hmm = hmm.GMMHMM(n_components=n_comps, n_mix=n_mix, weights_prior=weights_prior,
                    means_prior=means_prior, covars_prior=covars_prior,
                     covariance_type='spherical', params='st', init_params='st')
gmm_hmm.means_ = means_prior
gmm_hmm.weights_ = weights_prior
gmm_hmm.covars_ = covars_prior

print('Before fitting...')
print('means')
print(gmm_hmm.means_)
print('weights')
print(gmm_hmm.weights_)
print('covars')
print(gmm_hmm.covars_)

# Fit the GMMHMM to the input sequence
gmm_hmm.fit(input_sequence)

print('After fitting...')
print('means')
print(gmm_hmm.means_)
print('weights')
print(gmm_hmm.weights_)
print('covars')
print(gmm_hmm.covars_)

You can see that the weights and covars have changed even though means has remained the same.

Before fitting...
means
[[[51.30211436]
  [53.32515359]]

 [[63.47895865]
  [57.19121711]]]
weights
[[0.58624271 0.41375729]
 [0.48605807 0.51394193]]
covars
[[ 0.6483754   1.2042972 ]
 [13.85258908  1.04639497]]

After fitting...
means
[[[51.16975532]
  [54.19504787]]

 [[65.82853658]
  [54.25868767]]]
weights
[[0.88971249 0.11028751]
 [0.30707459 0.69292541]]
covars
[[ 0.56903044  0.70862057]
 [14.77828965  0.56072741]]

Thanks a lot for the help!

GMMHMM Documentation

init_params : Controls which parameters are initialized prior to training. Can contain any combination of 's' for startprob, 't' for transmat, 'm' for means, 'c' for covars, and 'w' for GMM mixing weights. Defaults to all parameters.

params : Controls which parameters are updated in the training process. Can contain any combination of 's' for startprob, 't' for transmat, 'm' for means, and 'c' for covars, and 'w' for GMM mixing weights.Defaults to all parameters.

Upvotes: 2

Views: 2168

Answers (1)

Jojo Mojo
Jojo Mojo

Reputation: 33

From the documentation: https://hmmlearn.readthedocs.io/en/latest/api.html#hmmlearn.hmm.GaussianHMM Try using the startprob_prior and transmat_prior parameters when initializing the model.

model = hmm.GaussianHMM(n_components=n_components, startprob_prior=pi_mat, transmat_prior=a_mat)

Upvotes: 0

Related Questions