Skip to content

Use scipy.special.xlogy to avoid indefinite limit in 0 for x*log(y) #12915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2019

Conversation

rth
Copy link
Member

@rth rth commented Jan 3, 2019

x*log(y) is undefined when both x and y are zero,

>>> import numpy as np
>>> 0*np.log(0)
__main__:1: RuntimeWarning: divide by zero encountered in log
__main__:1: RuntimeWarning: invalid value encountered in double_scalars
nan

currently we avoid this by clipping x and y to a very small value (e.g. 1e-10) instead of 0.

A cleaner solution is to use scipy.special.xlogy,

>>> from scipy.special import xlogy
>>> xlogy(0, 0)
0.0

which produces the correct limit in 0 and has a comparable performance otherwise.

@@ -297,7 +297,7 @@ def test_multilabel_classification():
max_iter=150, random_state=0, activation='logistic',
learning_rate_init=0.2)
mlp.fit(X, y)
assert_equal(mlp.score(X, y), 1)
assert_greater(mlp.score(X, y), 0.97)
Copy link
Member Author

@rth rth Jan 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails otherwise, as the score is 0.98 in this case.

Testing that mean test accuracy is exactly 1 on this random dataset with those parameters doesn't sound too robust, and the change of the numerical accuracy in log loss might have been enough to break this condition.

@qinhanmin2014
Copy link
Member

I guess we don't need tests and what's new here.

@qinhanmin2014 qinhanmin2014 merged commit 836a812 into scikit-learn:master Jan 3, 2019
@rth rth deleted the xlogy branch January 3, 2019 15:08
adrinjalali pushed a commit to adrinjalali/scikit-learn that referenced this pull request Jan 7, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
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.

3 participants