隐马尔可夫模型,对先前实施的澄清

时间:2012-09-29 22:33:54

标签: java hidden-markov-models

我正在尝试使用隐藏的马尔可夫模型。我真的没有使用它们的经验,所以我决定查看几个实现的例子。

看下面的实现,我对Baum-Welch算法(在火车方法下找到)采用可变步骤的目的感到有点困惑。我理解提供训练集,但不提供步骤。有没有人对此有解释,因为我从文档中不了解它。

这是原始代码http://cs.nyu.edu/courses/spring04/G22.2591-001/BW%20demo/HMM.java的链接,因为代码在我的帖子中没有很好地呈现。

import java.text.*;

/** This class implements a Hidden Markov Model, as well as
the Baum-Welch Algorithm for training HMMs.
@author Holger Wunsch (wunsch@sfs.nphil.uni-tuebingen.de) 
*/
public class HMM {
  /** number of states */
  public int numStates;

  /** size of output vocabulary */
  public int sigmaSize;

  /** initial state probabilities */
  public double pi[];

  /** transition probabilities */
  public double a[][];

  /** emission probabilities */
  public double b[][];

  /** initializes an HMM.
  @param numStates number of states
  @param sigmaSize size of output vocabulary 
  */

 public HMM(int numStates, int sigmaSize) {
   this.numStates = numStates;
   this.sigmaSize = sigmaSize;

   pi = new double[numStates];
   a = new double[numStates][numStates];
   b = new double[numStates][sigmaSize];
 }

 /** implementation of the Baum-Welch Algorithm for HMMs.
  @param o the training set
  @param steps the number of steps
 */
  public void train(int[] o, int steps) {
     int T = o.length;
     double[][] fwd;
     double[][] bwd;

     double pi1[] = new double[numStates];
     double a1[][] = new double[numStates][numStates];
     double b1[][] = new double[numStates][sigmaSize];

     for (int s = 0; s < steps; s++) {
      /* calculation of Forward- und Backward Variables from the
 current model */
      fwd = forwardProc(o);
      bwd = backwardProc(o);

      /* re-estimation of initial state probabilities */
      for (int i = 0; i < numStates; i++)
    pi1[i] = gamma(i, 0, o, fwd, bwd);

      /* re-estimation of transition probabilities */ 
      for (int i = 0; i < numStates; i++) {
    for (int j = 0; j < numStates; j++) {
     double num = 0;
     double denom = 0;
     for (int t = 0; t <= T - 1; t++) {
       num += p(t, i, j, o, fwd, bwd);
       denom += gamma(i, t, o, fwd, bwd);
     }
   a1[i][j] = divide(num, denom);
  }
    }

  /* re-estimation of emission probabilities */
  for (int i = 0; i < numStates; i++) {
for (int k = 0; k < sigmaSize; k++) {
  double num = 0;
  double denom = 0;

  for (int t = 0; t <= T - 1; t++) {
    double g = gamma(i, t, o, fwd, bwd);
    num += g * (k == o[t] ? 1 : 0);
    denom += g;
  }
  b1[i][k] = divide(num, denom);
}
  }
  pi = pi1;
  a = a1;
  b = b1;
}
}


  /** calculation of Forward-Variables f(i,t) for state i at time
  t for output sequence O with the current HMM parameters
  @param o the output sequence O
  @return an array f(i,t) over states and times, containing
          the Forward-variables. 
  */

  public double[][] forwardProc(int[] o) {
int T = o.length;
double[][] fwd = new double[numStates][T];

/* initialization (time 0) */
for (int i = 0; i < numStates; i++)
  fwd[i][0] = pi[i] * b[i][o[0]];

/* induction */
for (int t = 0; t <= T-2; t++) {
  for (int j = 0; j < numStates; j++) {
fwd[j][t+1] = 0;
for (int i = 0; i < numStates; i++)
  fwd[j][t+1] += (fwd[i][t] * a[i][j]);
fwd[j][t+1] *= b[j][o[t+1]];
  }
}

return fwd;
}

我遇到的另外两个问题是关于Forw​​ard方法,它实现了Forward-Backward算法的Forward部分。通过阅读HMM,我认为,在训练我的模型之后,我应该使用类似这种方法来预测未来的观察。那么param O(代表输出序列)是直到这一点的观察序列吗?

通过对这种方法的一些实验,我回复了文档所说的Forward-Variables,它看起来就像是一堆概率。这些如何转化为未来的观察结果?

我正在深入研究对我来说很难的编程区域,所以我非常感谢你帮助我理解这些东西!

0 个答案:

没有答案
相关问题