3

I'm trying to compute a vector, whose sum is 1 and whose elements are defined as such:

v[i] = exp(tmp[i])/exp(tmp).sum()

The problem is that the value in the exponential may be large (between -10^2 and 10^2), making the exponential to evaluate to inf or 0.

I tried some variations, like substracting the biggest element or the mean of tmp to the numerator and the denominator, but it's still not enough.

Basically, I'm need of a transformation that reduces the mean value and the dispersion in tmp or of a clever ordering for this computation.

I'm using numpy arrays as containers and exp is numpy.exp.

cpa
  • 3,742
  • 4
  • 16
  • 22

1 Answers1

5
>>> tmp = np.array([-10**10, 10**10])
>>> tmp_max = tmp.max()
>>> log_D = log(sum(exp(tmp - tmp_max))) + tmp_max
>>> log_v = tmp - log_D
>>> v = np.exp(log_v)
>>> v
array([ 0.,  1.])

Or use scipy.misc.logsumexp, which uses the exact same algorithm.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • That's not what worked for me, but it gave me some directions in my investigations. Thanks! – cpa Apr 23 '12 at 12:33