読者です 読者をやめる 読者になる 読者になる

俺とプログラミング

某IT企業でエンジニアをしてます。このブログではプログラミングに関わることを幅広く発信します。

How to write and read HMM in GHMM

This article shows how to write and read Hidden Markov Model in GHMM.
It is very easy.

Setup

from ghmm import *

A = [[0.6, 0.4, 0], [0, 0.6, 0.4], [0.0, 0.0, 1.0]]
B = [[0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25]]
pi = [1.0, 0, 0]
sigma = IntegerRange(0, 4)

hmm = HMMFromMatrices(sigma, DiscreteDistribution(sigma), A, B, pi)
hmm.normalize()

train_seq = EmissionSequence(sigma, [0, 0, 0, 1, 3, 2, 2, 3])
hmm.baumWelch(train_seq)

Write HMM to a file

filename = 'trained_hmm'
hmm.write(filename)

Read HMM from a file

from ghmm import *

filename = 'trained_hmm'

hmm = HMMOpenXML.openNewXML(filename, None)

#can be use hmm normally
test_seq = EmissionSequence(sigma, [0, 0, 0, 1, 3, 2, 2])
result = hmm.viterbi(test_seq)


Pattern Recognition and Machine Learning

Copyright © 2016 ttlg All Rights Reserved.