tensorflow_hmm package

Submodules

tensorflow_hmm.hmm module

class tensorflow_hmm.hmm.HMM(P, p0=None, length=None)

Bases: object

A class for Hidden Markov Models.

The model attributes are: - K :: the number of states - P :: the K by K transition matrix (from state i to state j,

(i, j) in [1..K])
  • p0 :: the initial distribution (defaults to starting in state 0)
class tensorflow_hmm.hmm.HMMNumpy(P, p0=None, length=None)

Bases: tensorflow_hmm.hmm.HMM

forward_backward(y)
viterbi_decode(y)
viterbi_decode_batched(y)

Expects inputs in [B, N, K] layout

class tensorflow_hmm.hmm.HMMTensorflow(P, p0=None, length=None)

Bases: tensorflow_hmm.hmm.HMM

forward_backward(y)

runs forward backward algorithm on state probabilities y

y : np.array : shape (T, K) where T is number of timesteps and
K is the number of states

(posterior, forward, backward) posterior : list of length T of tensorflow graph nodes representing

the posterior probability of each state at each time step
forward : list of length T of tensorflow graph nodes representing
the forward probability of each state at each time step
backward : list of length T of tensorflow graph nodes representing
the backward probability of each state at each time step
viterbi_decode(y)

Runs viterbi decode on state probabilies y.

y : np.array : shape (T, K) where T is number of timesteps and
K is the number of states

(s, pathScores) s : list of length T of tensorflow ints : represents the most likely

state at each time step.
pathScores : list of length T of tensorflow tensor of length K
each value at (t, k) is the log likliehood score in state k at time t. sum(pathScores[t, :]) will not necessary == 1
viterbi_decode_batched(y, onehot=False)

Runs viterbi decode on state probabilies y in batch mode

y : np.array : shape (B, T, K) where T is number of timesteps and
K is the number of states
onehot : boolean : if true, returns a onehot representation of the
most likely states, instead of integer indexes of the most likely states.

(s, pathScores) s : list of length T of tensorflow ints : represents the most likely

state at each time step.
pathScores : list of length T of tensorflow tensor of length K
each value at (t, k) is the log likliehood score in state k at time t. sum(pathScores[t, :]) will not necessary == 1
tensorflow_hmm.hmm.tf_map(fn, arrays)

Apply fn to each of the values in each of the arrays. Implemented in native python would look like:

return map(fn, *arrays)

more explicitly:

output[i] = fn(arrays[0][i], arrays[1][i], … arrays[-1][i])

This function assumes that all arrays have same leading dim.

Module contents