【问题标题】:Python Viterbi algorithmPython 维特比算法
【发布时间】:2015-08-19 18:54:28
【问题描述】:

下面的代码是我在 HMM 模型中使用的 Viterbi 算法的 here 找到的 Python 实现。该链接还提供了一个测试用例。

__init__,我明白:

  • initialProb 是从给定状态开始的概率,
  • transProb 是在任何给定时间从一种状态移动到另一种状态的概率,但是

我看不懂的参数是obsProb。谁能解释一下?

import numpy as np

'''
N: number of hidden states
'''
class Decoder(object):
def __init__(self, initialProb, transProb, obsProb):
    self.N = initialProb.shape[0]
    self.initialProb = initialProb
    self.transProb = transProb
    self.obsProb = obsProb
    assert self.initialProb.shape == (self.N, 1)
    assert self.transProb.shape == (self.N, self.N)
    assert self.obsProb.shape[0] == self.N

def Obs(self, obs):
    return self.obsProb[:, obs, None]

def Decode(self, obs):
    trellis = np.zeros((self.N, len(obs)))
    backpt = np.ones((self.N, len(obs)), 'int32') * -1

    # initialization
    trellis[:, 0] = np.squeeze(self.initialProb * self.Obs(obs[0]))

    for t in xrange(1, len(obs)):
        trellis[:, t] = (trellis[:, t-1, None].dot(self.Obs(obs[t]).T) * self.transProb).max(0)
        backpt[:, t] = (np.tile(trellis[:, t-1, None], [1, self.N]) * self.transProb).argmax(0)
    # termination
    tokens = [trellis[:, -1].argmax()]
    for i in xrange(len(obs)-1, 0, -1):
        tokens.append(backpt[tokens[-1], i])
    return tokens[::-1]

【问题讨论】:

标签: python algorithm


【解决方案1】:

具有N 隐藏状态和M 可能离散观测值的HMM 由以下参数定义:

  • initialProb(大小为N的向量):初始状态分布。条目 initialProb[i]P(x_0 = i) 最初(在时间 0)处于状态 i 的概率。
  • transProb(大小矩阵NxN):转移概率矩阵。条目transProb[i][j]P(x_{t+1} = j | x_t = i) 从状态i 转换到j 的概率。
  • obsProb(矩阵大小NxM):发射概率矩阵。条目obsProb[i][j] 是从状态i 发射符号j 的概率P(y_t = j | x_t = i)

通常,这些参数分别命名为\piTE,或\piAB

顺便说一下,HMM 的标准参考是tutorial by Rabiner

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 2013-02-25
    • 2014-06-27
    • 2011-05-02
    • 2011-12-22
    • 2017-03-24
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多