提交 51b082c6 authored 作者: Frederic Bastien's avatar Frederic Bastien

Vectorize MultinomialFromUniform.perform() to speed up tests in…

Vectorize MultinomialFromUniform.perform() to speed up tests in FAST_COMPILE/DEBUG_MODE. This cause some failures due to no output for 10m on travis
上级 054aa55a
...@@ -172,28 +172,43 @@ class MultinomialFromUniform(Op): ...@@ -172,28 +172,43 @@ class MultinomialFromUniform(Op):
unis.shape[0], pvals.shape[0], n_samples) unis.shape[0], pvals.shape[0], n_samples)
if z[0] is None or z[0].shape != pvals.shape: if z[0] is None or z[0].shape != pvals.shape:
z[0] = numpy.zeros(pvals.shape, dtype=node.outputs[0].dtype) z[0] = numpy.zeros(pvals.shape, dtype=node.outputs[0].dtype)
else:
z[0].fill(0)
nb_multi = pvals.shape[0] nb_multi = pvals.shape[0]
nb_outcomes = pvals.shape[1] nb_outcomes = pvals.shape[1]
# Original version that is not vectorized. I keep it here as
# it is more readable.
# For each multinomial, loop over each possible outcome # For each multinomial, loop over each possible outcome
# for c in range(n_samples):
# for n in range(nb_multi):
# waiting = True
# cummul = 0
# unis_n = unis[c * nb_multi + n]
# for m in range(nb_outcomes):
# cummul += pvals[n, m]
# if c == 0:
# if (waiting and (cummul > unis_n)):
# z[0][n, m] = 1
# waiting = False
# else:
# # Only needed if we don't init the output to 0
# z[0][n, m] = 0
# else:
# if (cummul > unis_n):
# z[0][n, m] += 1
# break
# Vectorized version that is much faster as all the looping is
# done in C even if this make extra work.
for c in range(n_samples): for c in range(n_samples):
for n in range(nb_multi): for n in range(nb_multi):
waiting = True
cummul = 0
unis_n = unis[c * nb_multi + n] unis_n = unis[c * nb_multi + n]
# The dtype='float64' is important. Otherwise we don't
for m in range(nb_outcomes): # have the same answer as the c code as in the c code
cummul += pvals[n, m] # the cumul is in double precission.
if c == 0: cumsum = pvals[n].cumsum(dtype='float64')
if (waiting and (cummul > unis_n)): z[0][n, (cumsum > unis_n).nonzero()[0][0]] += 1
z[0][n, m] = 1
waiting = False
else:
z[0][n, m] = 0
else:
if (cummul > unis_n):
z[0][n, m] += 1
break
class MultinomialWOReplacementFromUniform(MultinomialFromUniform): class MultinomialWOReplacementFromUniform(MultinomialFromUniform):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论