It is straightforward to vectorize your inner loops. Here is an example for the second part of your code (untested of course):
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
It might be possible to also vectorize the outer loops, but by far the biggest gain is usually obtained by focusing on the inner loops only. Some comments:
It seems that most of your
while
loops can be turned intofor
loops. Even though this does not make a lot of difference for speed, it is the preferred way if you know the number of iterations before the loop.The convention is to use
import numpy as np
, and usenp.function
in the rest of the codeSimple loops that just compute a sum (
accum = 0; for item in vector: accum += item
) should be vectorized likeaccum = np.sum(vector)
.Conditional summing in a loop can be converted to a vectorized sum with boolean indexing, so
accum = 0; for i in range(n): if cond[i]: accum += vector[i]
can be replaced withaccum = np.sum(vector[cond])
I am interested to know how much faster your code becomes after these modifications, I guess you can easily gain more than a factor 10.