我正在尝试在我的 jupyter 笔记本中使用 hmmlearn 的隐藏马尔可夫模型。无论我如何尝试与它交互,我都会遇到相同的运行时错误(我不理解并且无法找到有关其发生原因的任何信息)。这是我的代码示例:
model = hmm.GaussianHMM(n_components=3, n_iter=100)
model.startprob_ = np.array([0.5, 0.3, 0.2])
model.transmat_ = np.array([[0.6, 0.3, 0.1], [0.3, 0.4, 0.3], [0.1, 0.3, 0.6]])
model.emissionprob_ = np.array([[0.6, 0.3, 0.1], [0.3, 0.4, 0.3], [0.1, 0.3, 0.6]])
data = [0, 1, 0, 2, 1, 0, 0, 1, 2, 2, 1, 0, 1, 2, 0]
observations = np.array(data).reshape(-1, 1)
model.fit(observations)
hidden_states = model.predict(observations)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[26], line 8
6 data = [0, 1, 0, 2, 1, 0, 0, 1, 2, 2, 1, 0, 1, 2, 0]
7 observations = np.array(data).reshape(-1, 1)
----> 8 model.fit(observations)
9 hidden_states = model.predict(observations)
File ~/Documents/NTNU/9sem/masters/masters-prep-project/.conda/lib/python3.9/site-packages/hmmlearn/base.py:485, in _AbstractHMM.fit(self, X, lengths)
482 self.monitor_._reset()
484 for iter in range(self.n_iter):
--> 485 stats, curr_logprob = self._do_estep(X, lengths)
487 # Compute lower bound before updating model parameters
488 lower_bound = self._compute_lower_bound(curr_logprob)
File ~/Documents/NTNU/9sem/masters/masters-prep-project/.conda/lib/python3.9/site-packages/hmmlearn/base.py:764, in _AbstractHMM._do_estep(self, X, lengths)
762 curr_logprob = 0
763 for sub_X in _utils.split_X_lengths(X, lengths):
--> 764 lattice, logprob, posteriors, fwdlattice, bwdlattice = impl(sub_X)
765 # Derived HMM classes will implement the following method to
766 # update their probability distributions, so keep
767 # a single call to this method for simplicity.
768 self._accumulate_sufficient_statistics(
769 stats, sub_X, lattice, posteriors, fwdlattice,
770 bwdlattice)
...
886 bwdlattice = _hmmc.backward_log(
887 self.startprob_, self.transmat_, log_frameprob)
888 posteriors = self._compute_posteriors_log(fwdlattice, bwdlattice)
RuntimeError: pybind11::handle::inc_ref() PyGILState_Check() failure.
我找不到任何关于这个 pybind11 错误的有用信息。
我尝试过的事情
我正在尝试实现一个无监督 HMM 来查看数据的聚类。
我正在使用Python版本3.9和hmmlearn版本0.3.0,我无法复制你的错误,但我收到一条评论,即使设置了startprob_和transmat_,它们也会被覆盖,因为init_params设置为stmc 默认。
将模型初始化更改为此可以消除该问题:
model = hmm.GaussianHMM(n_components=3, n_iter=100, init_params='mc')
尽管这是一个旧线程,但我很好奇这是否对您的问题有帮助。