Skip to content

Instantly share code, notes, and snippets.

@trivialfis
Created December 22, 2018 19:10
Show Gist options
  • Save trivialfis/784fee1ccc3d0c9dd4322e66f912282f to your computer and use it in GitHub Desktop.
Save trivialfis/784fee1ccc3d0c9dd4322e66f912282f to your computer and use it in GitHub Desktop.
Re-implementation of "A Revealing Introduction to Hidden Markov Models".
'''
Copyright (c) 2018 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
====
Re-implementaion of:
A Revealing Introduction to Hidden Markov Models
https://www.cs.sjsu.edu/~stamp/RUA/HMM.pdf
'''
import numpy as np
import math
from time import time
class HMM(object):
def __init__(self, n_states: int, n_symbols: int, max_iter: int):
self.N: int = n_states
self.M: int = n_symbols
self.a: np.ndarray = np.zeros(shape=(self.N, self.N)) # transition
self.a.fill(1/self.N)
self.b: np.ndarray = np.zeros(shape=(self.N, self.M)) # emission
self.b.fill(1/self.M)
self.pi: np.ndarray = np.zeros(shape=(self.N,)) # initial distribution
self.pi.fill(1/self.N)
self.max_iter: int = max_iter
def __str__(self):
return ('\n{\n a:\n' + str(self.a) +
'\n b:\n' + str(self.b) +
'\n c:\n' + str(self.pi) + '\n}')
def forward(self, sequence: np.ndarray) -> np.ndarray:
assert len(sequence.shape) == 1, f'Got: {sequence.shape()}.'
self.c: np.ndarray = np.zeros(shape=(sequence.size,)) # scale factors
self.c[0] = 0
alpha: np.ndarray = np.zeros(shape=(sequence.size, self.N))
for i in range(0, self.N):
alpha[0, i] = self.pi[i] * self.b[i, sequence[0]]
self.c[0] = self.c[0] + alpha[0, i]
self.c[0] = 1 / self.c[0]
for i in range(0, self.N):
alpha[0, i] = self.c[0] * alpha[0, i]
for t in range(1, sequence.size):
self.c[t] = 0
for i in range(0, self.N):
alpha[t, i] = 0
for j in range(0, self.N):
alpha[t, i] = alpha[t, i] + alpha[t-1, j] * self.a[j, i]
alpha[t, i] = alpha[t, i] * self.b[i, sequence[t]]
self.c[t] = self.c[t] + alpha[t, i]
self.c[t] = 1 / self.c[t]
for i in range(0, self.N):
alpha[t, i] = self.c[t] * alpha[t, i]
return alpha
def backward(self, sequence: np.ndarray) -> np.ndarray:
T: int = sequence.size
beta: np.ndarray = np.zeros(shape=(T, self.N))
for i in range(0, self.N):
beta[T-1, i] = self.c[T-1]
t: int = T-2
while t >= 0:
for i in range(0, self.N):
beta[t, i] = 0
for j in range(0, self.N):
beta[t, i] = (
beta[t, i] +
self.a[i, j] * self.b[j, sequence[t+1]] * beta[t+1, j])
beta[t, i] = self.c[t] * beta[t, i]
t = t - 1
return beta
def gammas(self, sequence: np.ndarray) -> (np.ndarray, np.ndarray):
T: int = sequence.size
gamma: np.ndarray = np.zeros(shape=(T, self.N))
digamma: np.ndarray = np.zeros(shape=(T, self.N, self.N))
alpha: np.ndarray = self.forward(sequence)
beta: np.ndarray = self.backward(sequence)
for t in range(0, T-1):
for i in range(0, self.N):
gamma[t, i] = 0
for j in range(0, self.N):
digamma[t, i, j] = (
alpha[t, i] * self.a[i, j] *
self.b[j, sequence[t+1]] * beta[t+1, j])
gamma[t, i] = gamma[t, i] + digamma[t, i, j]
for i in range(0, self.N):
gamma[T-1, i] = alpha[T-1, i]
return (gamma, digamma)
def restimate(self, sequence: np.ndarray):
gamma, digamma = self.gammas(sequence)
T: int = sequence.size
for i in range(0, self.N):
self.pi[i] = gamma[0, i]
for i in range(0, self.N):
denom = 0
for t in range(0, T-1):
denom = denom + gamma[t, i]
for j in range(0, self.N):
numer = 0
for t in range(0, T-1):
numer = numer + digamma[t, i, j]
self.a[i, j] = numer / denom
for i in range(0, self.N):
denom = 0
for t in range(0, T):
denom = denom + gamma[t, i]
for j in range(0, self.M):
numer = 0
for t in range(0, T):
if sequence[t] == j:
numer = numer + gamma[t, i]
self.b[i, j] = numer / denom
def train(self, sequence: np.ndarray) -> (
np.ndarray, np.ndarray, np.ndarray):
it = 0
old_prob = - np.inf
new_prob = - 1000
while it < self.max_iter and new_prob >= old_prob:
old_prob = new_prob
self.restimate(sequence)
new_prob = self.log_prob(sequence)
print(f'It: {it}, Prob: {new_prob}')
it += 1
return (self.pi, self.a, self.b)
def log_prob(self, sequence: np.ndarray):
prob, T = 0, sequence.size
for i in range(0, T):
prob = prob + math.log(self.c[i])
prob = -prob
return prob
if __name__ == '__main__':
hmm = HMM(32, 16, max_iter=8)
np.random.seed(0)
sequence = np.random.randint(1, 16, size=32)
start = time()
model = hmm.train(sequence)
duration = time() - start
print(f'Duration: {duration}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment