From 23fc19dde87a0f06a92ff9b8b08e461861116dda Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Sat, 26 Jan 2019 17:14:04 +0800 Subject: [PATCH] =?UTF-8?q?HMM=20-=20Simplify=20the=20implementation=20of?= =?UTF-8?q?=20=E7=AE=97=E6=B3=95=2010.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Fix typo: 三角波->sin波 2. Add source from the book:算法 10.1, 10.2, 10.3, 10.4... 3. Simplify the implementation of 算法 10.1 using np.random.choice --- hmm/hmm.py | 78 +++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/hmm/hmm.py b/hmm/hmm.py index 2894f6a..263059d 100644 --- a/hmm/hmm.py +++ b/hmm/hmm.py @@ -1,7 +1,6 @@ # encoding=utf8 import numpy as np -import csv class HMM(object): def __init__(self,N,M): @@ -21,7 +20,8 @@ def cal_probality(self, O): def forward(self): """ - 前向算法 + 算法 10.2 + 观测序列概率的前向算法 """ self.alpha = np.zeros((self.T,self.N)) @@ -39,7 +39,8 @@ def forward(self): def backward(self): """ - 后向算法 + 算法 10.3 + 观测序列概率的后向算法 """ self.beta = np.zeros((self.T,self.N)) @@ -98,6 +99,10 @@ def init(self): self.B[i][j] = randomlist[j]/Sum def train(self, O, MaxSteps = 100): + """ + 算法 10.4 + Baum-Welch算法 + """ self.T = len(O) self.O = O @@ -116,7 +121,10 @@ def train(self, O, MaxSteps = 100): self.forward() self.backward() - # a_{ij} + """ + 公式 10.39 + 计算a_{ij} + """ for i in range(self.N): for j in range(self.N): numerator=0.0 @@ -126,7 +134,10 @@ def train(self, O, MaxSteps = 100): denominator += self.cal_gamma(i,t) tmp_A[i][j] = numerator/denominator - # b_{jk} + """ + 公式 10.40 + 计算b_{jk} + """ for j in range(self.N): for k in range(self.M): numerator = 0.0 @@ -137,7 +148,10 @@ def train(self, O, MaxSteps = 100): denominator += self.cal_gamma(j,t) tmp_B[j][k] = numerator / denominator - # pi_i + """ + 公式 10.41 + 计算pi_i + """ for i in range(self.N): tmp_pi[i] = self.cal_gamma(i,0) @@ -146,40 +160,25 @@ def train(self, O, MaxSteps = 100): self.Pi = tmp_pi def generate(self, length): - import random + """ + 算法 10.1 + 观测序列的生成 + """ I = [] - - # start - ran = random.randint(0,1000)/1000.0 - i = 0 - while self.Pi[i]