Python - hmmlearn - 负转换
Python - hmmlearn - Negative transmat
我正在尝试使用 hmmlearn 给定一个先验转移矩阵和发射矩阵来拟合模型。拟合后,它在转换矩阵中给出一些负值。
转移矩阵由另一个模型的转移矩阵恢复。
我的意思的示例代码是:
>>> model
GaussianHMM(algorithm='viterbi', covariance_type='diag',covars_prior=0.01,
covars_weight=1, init_params='stmc', means_prior=0, means_weight=0,
n_components=3, n_iter=100, params='stmc', random_state=123,
startprob_prior=1.0, tol=0.5, transmat_prior=1.0, verbose=True)
>>> model.transmat_
array([[ 9.95946216e-01, 2.06359396e-21, 4.05378401e-03],
[ 2.05184679e-21, 9.98355526e-01, 1.64447392e-03],
[ 3.86689326e-03, 1.96383373e-03, 9.94169273e-01]])
>>> new_model= hmm.GaussianHMM(n_components=model.n_components,
random_state=123,
... init_params="mcs", transmat_prior=model.transmat_)
>>> new_model.fit(train_features)
GaussianHMM(algorithm='viterbi', covariance_type='diag', covars_prior=0.01,
covars_weight=1, init_params='mcs', means_prior=0, means_weight=0,
n_components=3, n_iter=10, params='stmc', random_state=123,
startprob_prior=1.0, tol=0.01,
transmat_prior=array([[ 9.95946e-01, 2.06359e-21, 4.05378e-03],
[ 2.05185e-21, 9.98356e-01, 1.64447e-03],
[ 3.86689e-03, 1.96383e-03, 9.94169e-01]]),
verbose=False)
>>> new_model.transmat_
array([[ 9.98145253e-01, 1.86155258e-03, -7.08313729e-06],
[ 2.16330448e-03, 9.93941859e-01, 3.89483667e-03],
[ -5.44842863e-06, 3.52862069e-03, 9.96478546e-01]])
>>>
代码中显示的训练数据也是一样的。
如果我不先验地使用转换矩阵,而是使用发射,例如,它可以正常工作。
我正在使用 Anaconda 2.5 64 位。
hmmlearn 版本是 0.2.0
提示?
谢谢
tl;dr 确保 transmat_prior
>=1.
隐马尔可夫模型的 EM 算法是使用状态指示变量 z
导出的,它保存每个时间步 t
的马尔可夫链状态。以先前的状态 z[t - 1]
为条件,z[t]
跟随 Categorical distribution,其参数由转移概率矩阵定义。
hmmlearn
实现了隐马尔可夫模型的MAP学习,即每个模型参数都有一个先验分布。具体来说,假设转移矩阵的每一行在EM算法的M步中都遵循一个对称的Dirichlet distribution with parameter transmat_prior
. The choice of prior is not random, Dirichlet distribution is conjugate to the Categorical. This gives rise to a simple update rule:
transmat[i, j] = (transmat_prior[i, j] - 1.0 + stats["trans"][i, j]) / normalizer
其中 stat["trans"][i, j]
是 i
和 j
之间的预期转换次数。
从更新规则可以清楚地看出,如果 a) transmat_prior
对某些 i
和 j
和 b) 期望值 stats["trans"]
不足以弥补这一点。
这是分类分布的 MAP 估计中的 known issue,一般建议要求所有状态 transmat_prior
>=1。
我正在尝试使用 hmmlearn 给定一个先验转移矩阵和发射矩阵来拟合模型。拟合后,它在转换矩阵中给出一些负值。
转移矩阵由另一个模型的转移矩阵恢复。
我的意思的示例代码是:
>>> model
GaussianHMM(algorithm='viterbi', covariance_type='diag',covars_prior=0.01,
covars_weight=1, init_params='stmc', means_prior=0, means_weight=0,
n_components=3, n_iter=100, params='stmc', random_state=123,
startprob_prior=1.0, tol=0.5, transmat_prior=1.0, verbose=True)
>>> model.transmat_
array([[ 9.95946216e-01, 2.06359396e-21, 4.05378401e-03],
[ 2.05184679e-21, 9.98355526e-01, 1.64447392e-03],
[ 3.86689326e-03, 1.96383373e-03, 9.94169273e-01]])
>>> new_model= hmm.GaussianHMM(n_components=model.n_components,
random_state=123,
... init_params="mcs", transmat_prior=model.transmat_)
>>> new_model.fit(train_features)
GaussianHMM(algorithm='viterbi', covariance_type='diag', covars_prior=0.01,
covars_weight=1, init_params='mcs', means_prior=0, means_weight=0,
n_components=3, n_iter=10, params='stmc', random_state=123,
startprob_prior=1.0, tol=0.01,
transmat_prior=array([[ 9.95946e-01, 2.06359e-21, 4.05378e-03],
[ 2.05185e-21, 9.98356e-01, 1.64447e-03],
[ 3.86689e-03, 1.96383e-03, 9.94169e-01]]),
verbose=False)
>>> new_model.transmat_
array([[ 9.98145253e-01, 1.86155258e-03, -7.08313729e-06],
[ 2.16330448e-03, 9.93941859e-01, 3.89483667e-03],
[ -5.44842863e-06, 3.52862069e-03, 9.96478546e-01]])
>>>
代码中显示的训练数据也是一样的。 如果我不先验地使用转换矩阵,而是使用发射,例如,它可以正常工作。 我正在使用 Anaconda 2.5 64 位。 hmmlearn 版本是 0.2.0
提示? 谢谢
tl;dr 确保 transmat_prior
>=1.
隐马尔可夫模型的 EM 算法是使用状态指示变量 z
导出的,它保存每个时间步 t
的马尔可夫链状态。以先前的状态 z[t - 1]
为条件,z[t]
跟随 Categorical distribution,其参数由转移概率矩阵定义。
hmmlearn
实现了隐马尔可夫模型的MAP学习,即每个模型参数都有一个先验分布。具体来说,假设转移矩阵的每一行在EM算法的M步中都遵循一个对称的Dirichlet distribution with parameter transmat_prior
. The choice of prior is not random, Dirichlet distribution is conjugate to the Categorical. This gives rise to a simple update rule:
transmat[i, j] = (transmat_prior[i, j] - 1.0 + stats["trans"][i, j]) / normalizer
其中 stat["trans"][i, j]
是 i
和 j
之间的预期转换次数。
从更新规则可以清楚地看出,如果 a) transmat_prior
对某些 i
和 j
和 b) 期望值 stats["trans"]
不足以弥补这一点。
这是分类分布的 MAP 估计中的 known issue,一般建议要求所有状态 transmat_prior
>=1。