Skip to content

Instantly share code, notes, and snippets.

@tpott
Last active December 15, 2015 05:39
Show Gist options
  • Save tpott/5210987 to your computer and use it in GitHub Desktop.
Save tpott/5210987 to your computer and use it in GitHub Desktop.
hmm training
# Created from these lecture notes:
# http://cseweb.ucsd.edu/classes/wi13/cse150-a/lecture/lecture11.pdf
# http://cseweb.ucsd.edu/classes/wi13/cse150-a/lecture/lecture12.pdf
# http://cseweb.ucsd.edu/classes/wi13/cse150-a/lecture/lecture13.pdf
# Dependencies:
# init_{a,b,pi} := the initial estimated matrices, can be random
# compute_forward := calculates the alpha matrix
# compute_reverse := calculates the beta matrix
# compute := calculates each of the matrices for the E-step
# update := sums up over each of the observations, except for pi
def learn(obs):
# default values or random
a, b, pi = init_a, init_b, init_pi
n_iters = 100000 # is there a better condition I could check?
while n_iters > 0:
# E-step
alpha = compute_forward(a, b, pi, obs)
beta = compute_reverse(a, b, pi, obs)
# the derivations in the notes don't use pi, but I derived
# P(S_1=i, O_{1..T}) = pi[i]*b[i,obs[1]]*beta[i,1]
# the notes state prob_i is the sum of each column in prob_ij
prob_ij, prob_i, prob_1 = compute(alpha, beta, a, b, obs, pi)
# M-step
a, b, pi = update(prob_ij, prob_i, prob_1)
return a, b, pi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment