Skip to content

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

Merged
qinhanmin2014 merged 3 commits intoscikit-learn:masterfrom
rth:xlogy
Jan 3, 2019
Merged

Use scipy.special.xlogy to avoid indefinite limit in 0 for x*log(y) #12915
qinhanmin2014 merged 3 commits intoscikit-learn:masterfrom
rth:xlogy

Conversation

@rth
Copy link
Copy Markdown
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.

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
Copy Markdown
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
Copy Markdown
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