40

I tried to implement soft-max with the following code (out_vec is a numpy vector of floats):

numerator = np.exp(out_vec)
denominator = np.sum(np.exp(out_vec))
out_vec = numerator/denominator

However, I got an overflow error because of np.exp(out_vec). Therefore, I checked (manually) what the upper limit of np.exp() is, and found that np.exp(709) is a number, but np.exp(710) is considered to be np.inf. Thus, to try to avoid the overflow error, I modified my code as follows:

out_vec[out_vec > 709] = 709 #prevent np.exp overflow
numerator = np.exp(out_vec)
denominator = np.sum(np.exp(out_vec))
out_vec = numerator/denominator

Now, I get a different error:

RuntimeWarning: invalid value encountered in greater out_vec[out_vec > 709] = 709

What's wrong with the line I added? I looked up this specific error and all I found is people's advice on how to ignore the error. Simply ignoring the error won't help me, because every time my code encounters this error it does not give the usual results.

Cheshie
  • 2,777
  • 6
  • 32
  • 51

4 Answers4

37

Your problem is caused by the NaN or Inf elements in your out_vec array. You could use the following code to avoid this problem:

if np.isnan(np.sum(out_vec)):
    out_vec = out_vec[~numpy.isnan(out_vec)] # just remove nan elements from vector
out_vec[out_vec > 709] = 709
...

or you could use the following code to leave the NaN values in your array:

out_vec[ np.array([e > 709 if ~np.isnan(e) else False for e in out_vec], dtype=bool) ] = 709
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110
kvorobiev
  • 5,012
  • 4
  • 29
  • 35
  • 4
    Thanks @kvorobiev, but I can't do that - simply removing the elements will cause data loss... – Cheshie Jun 06 '16 at 08:32
14

In my case the warning did not show up when calling this before the comparison (I had NaN values getting compared)

np.warnings.filterwarnings('ignore')
juerg
  • 479
  • 5
  • 16
  • 3
    There is no warnings module within numpy, this (`np.warnings.filterwarnings('ignore')`) is accessing the warnings package built into python's standard library which numpy happens to import. The code is equivalent to `import warnings`, `warnings.filterwarnings('ignore')`, and it will suppress all warnings generated by all code (not just numpy) unless you later re-enable warnings. – scottclowe Jan 28 '20 at 19:54
  • I did the warning suppression too with the warnings module but limited it to the few statements that needed it using: `with np.warnings.filterwarnings('ignore'):` – juerg Jan 29 '20 at 08:28
  • 4
    `np.seterr(invalid='ignore')` seems like a better option – Marcelo Villa-Piñeros Feb 21 '20 at 20:59
  • If you want to go the whole hog, try `np.seterr(all='raise')` – Nihar Karve May 05 '20 at 03:21
8

IMO the better way would be to use a more numerically stable implementation of sum of exponentials.

from scipy.misc import logsumexp
out_vec = np.exp(out_vec - logsumexp(out_vec))
Ramin Barati
  • 81
  • 1
  • 2
2

If this happens because of your NaN value, then this might help:

out_vec[~np.isnan(out_vec)] = out_vec[~np.isnan(out_vec)] > 709

This does the greater operation for none NaN values and the rest remains the same. If you need the rest to be False, then do this too:

out_vec[np.isnan(out_vec)] = False
Maryam Bahrami
  • 1,056
  • 9
  • 18