Skip to content

Conversation

@naftaliharris
Copy link
Contributor

This avoids an unnecessary computation and also possible math overflow errors.

This avoids an unnecessary computation and also possible math overflow errors.
@AmplabJenkins
Copy link

Can one of the admins verify this patch?

@mateiz
Copy link
Contributor

mateiz commented Jul 29, 2014

@mengxr what do you think about this?

@mateiz
Copy link
Contributor

mateiz commented Jul 29, 2014

Actually the Python version of this code seems like it needs to be brought in line with the threshold API added to Java / Scala. After that, the change won't work any more, though it is correct for threshold = 0.5.

@mengxr
Copy link
Contributor

mengxr commented Jul 29, 2014

I think we should keep it as it is now and add support for setting thresholds.

@naftaliharris
Copy link
Contributor Author

My point actually is that you get OverflowError's if margin is too large, for example:

>>> from math import exp
>>> margin = -1000
>>> prob = 1/(1 + exp(-margin))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: math range error
>>>

This actually happened to me, which is how I saw this issue.

There are (at least) two ways to solve this: One is to check margin instead of prob; this has the advantage of also saving a computation. Even if you allow the user to specify the probability threshold p at some later date, you can just change the margin cutoff from 0 to log(p/(1-p)).

The second solution is to replace math.exp with np.exp, which gives a warning rather than an exception:

>>> import numpy as np
>>> margin = -1000
>>> prob = 1/(1 + np.exp(-margin))
__main__:1: RuntimeWarning: overflow encountered in exp
>>> prob
0.0
>>>

For robustness of the code, perhaps you should at least do that.

Thanks!

@mateiz
Copy link
Contributor

mateiz commented Jul 29, 2014

That makes sense. I believe this was fixed just recently in here though: #1493. Does the fix look okay to you?

@naftaliharris
Copy link
Contributor Author

Yeah, that fixes the overflow errors! The second probability calculation has some slight numerical issues, (see in-line comment), but not a huge deal.

@mateiz
Copy link
Contributor

mateiz commented Aug 30, 2014

Hey @naftaliharris, might closing this pull request now that this has been fixed in other PRs?

@naftaliharris
Copy link
Contributor Author

@mateiz oh yeah, no problem. Thanks again for the fixes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants