我正在尝试在python中实现隐藏的markov模型训练,因此生成的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

最佳答案

向量化内部循环很简单。这是您的代码第二部分的示例(当然,未经培训):

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倍以上的收益。

09-27 04:55