我试图在python中实现隐藏的马尔可夫模型训练,结果numpy代码看起来很慢。训练模型需要30分钟。下面是我的代码,我同意它非常低效。我尝试了解numpy矢量化和高级索引方法,但无法将其视为如何在我的代码中使用它们。我可以确定大部分执行是集中的,reestimate()
函数占用了99%以上的执行时间,尤其是打印CHK5和CHK6的部分。
def reestimate(self):
newTransition = numpy.zeros(shape=(int(self.num_states),int(self.num_states)))
newOutput = numpy.zeros(shape=(int(self.num_states),int(self.num_symbols)))
numerator = numpy.zeros(shape=(int(self.num_obSeq),))
denominator = numpy.zeros(shape=(int(self.num_obSeq),))
sumP = 0
i = 0
print "CHK1"
while i < self.num_states:
j=0
while j < self.num_states:
if j < i or j > i + self.delta:
newTransition[i][j] = 0
else:
k=0
print "CHK2"
while k < self.num_obSeq:
numerator[k] = denominator[k] = 0
self.setObSeq(self.obSeq[k])
sumP += self.computeAlpha()
self.computeBeta()
t=0
while t < self.len_obSeq - 1:
numerator[k] += self.alpha[t][i] * self.transition[i][j] * self.output[j][self.currentSeq[t + 1]] * self.beta[t + 1][j]
denominator[k] += self.alpha[t][i] * self.beta[t][i]
t += 1
k += 1
denom=0
k=0
print "CHK3"
while k < self.num_obSeq:
newTransition[i,j] += (1 / sumP) * numerator[k]
denom += (1 / sumP) * denominator[k]
k += 1
newTransition[i,j] /= denom
newTransition[i,j] += self.MIN_PROBABILITY
j += 1
i += 1
sumP = 0
i = 0
print "CHK4"
while i < self.num_states:
j=0
while j < self.num_symbols:
k=0
while k < self.num_obSeq:
numerator[k] = denominator[k] = 0
self.setObSeq(self.obSeq[k])
# print self.obSeq[k]
sumP += self.computeAlpha()
self.computeBeta()
t=0
print "CHK5"
while t < self.len_obSeq - 1:
if self.currentSeq[t] == j:
numerator[k] += self.alpha[t,i] * self.beta[t,i]
denominator[k] += self.alpha[t,i] * self.beta[t,i]
t += 1
k += 1
denom=0
k=0
print "CHK6"
while k < self.num_obSeq:
newOutput[i,j] += (1 / sumP) * numerator[k]
denom += (1 / sumP) * denominator[k]
k += 1
newOutput[i,j] /= denom
newOutput[i,j] += self.MIN_PROBABILITY,
j += 1
i += 1
self.transition = newTransition
self.output = newOutput
def train(self):
i = 0
while i < 20:
self.reestimate()
print "reestimating....." ,i
i += 1
答案 0 :(得分:1)
向内化循环是很简单的。以下是代码第二部分的示例(当然未经测试):
print "CHK4"
for i in xrange(self.num_states):
for j in xrange(self.num_symbols):
for k in xrange(self.num_obSeq):
self.setObSeq(self.obSeq[k])
# print self.obSeq[k]
sumP += self.computeAlpha()
self.computeBeta()
alpha_times_beta = self.alpha[:,i] * self.beta[:,i]
numerator[k] = numpy.sum(alpha_times_beta[self.currentSeq == j])
denominator[k] = numpy.sum(alpha_times_beta)
denom = numpy.sum(denominator)
newOutput[i,j] = numpy.sum(numerator) / (sumP * denom) + self.MIN_PROBABILITY
self.transition = newTransition
self.output = newOutput
也可以对外环进行矢量化,但到目前为止,通常仅通过关注内环来获得最大增益。一些评论:
您的大多数while
循环似乎都可以转换为for
循环。尽管这对速度没有太大影响,但如果你知道循环之前的迭代次数,它是首选的方法。
惯例是使用import numpy as np
,并在其余代码中使用np.function
只计算总和(accum = 0; for item in vector: accum += item
)的简单循环应该像accum = np.sum(vector)
一样进行矢量化。
循环中的条件求和可以使用布尔索引转换为矢量化和,因此accum = 0; for i in range(n): if cond[i]: accum += vector[i]
可以替换为accum = np.sum(vector[cond])
我很想知道这些修改后你的代码会变得多快,我猜你可以轻松获得超过10倍的代码。