Skip to content

[MRG] speed up RBM training with scipy.special.expit - #2882

Merged
larsmans merged 2 commits into
scikit-learn:masterfrom
larsmans:expit
Feb 23, 2014
Merged

larsmans merged 2 commits into
scikit-learn:masterfrom
larsmans:expit

Conversation

@larsmans

Copy link
Copy Markdown
Member

SciPy 0.10 already has an implementation of the logistic function called scipy.special.expit. Moved our logistic_sigmoid to fixes.expit, keeping its log-of-logistic functionality as log_logistic (should that be log_expit? I don't like the name expit at all.)

Using this makes RBM training 12% faster, as measured by observing the time per iteration as reported by the plot_rbm_logistic_classification.py example, disregarding the first iteration as an outlier (it's consistently faster than the rest, not sure why). The speedup is in the expit function, not the inplace operations; those are there to make benchmarking easier, but they certainly won't hurt.

@larsmans

Copy link
Copy Markdown
Member Author

Ping @vene, @ogrisel, @IssamLaradji.

@ogrisel

ogrisel commented Feb 23, 2014

Copy link
Copy Markdown
Member

+1 for using scipy expit implementation. Ok for the log_logistic name as well.

@vene

vene commented Feb 23, 2014

Copy link
Copy Markdown
Member

+1 for the implementation. Undecieded between log_logistic and log_logistic_sigmoid and definitely a dislike for the scipy expit name, sounds too much like spit. However if the two functions have different names the code becomes a bit confusing. We could import expit under a different name but that would kind of go against the community. I don't know what I'd prefer -- depends how personal my (and Lars's) dislike for the expit name is.

@larsmans

Copy link
Copy Markdown
Member Author

In any case, both functions refer to each other and I already got a patch in SciPy to make their documentation less confusing (I found expit only when running SciPy unit tests for another patch), so -1 on importing expit under another name.

@GaelVaroquaux

Copy link
Copy Markdown
Member

so -1 on importing expit under another name.

Same thing here. But the name is so confusing that each import should say
that it is a logistic function.

Comment thread sklearn/utils/extmath.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that it would be good to keep a backward-compatible symbol for one or two versions, as some people might be using it.

@GaelVaroquaux

Copy link
Copy Markdown
Member

I am of course +1 for merge, but I have two comments:

  • I think that expit needs a comment, eg near the import, making it explicit that it is the logistic function
  • We should keep some backward compatibility, even if it is in utils. I can quite clearly see that there is a usage for importing this function in users code.

SciPy 0.10 already has an implementation of the logistic function called
scipy.special.expit.

Using this makes RBM training 12% faster, as measured by observing the time
per iteration as reported by the plot_rbm_logistic_classification.py example,
disregarding the first iteration as an outlier (it's consistently faster than
the rest, not sure why). The speedup is in the expit function, not the
inplace operations; those are there to make benchmarking easier, but they
certainly won't hurt.
For backward compat only, so immediately deprecated.
@larsmans

Copy link
Copy Markdown
Member Author

Ok, reinstated logistic_sigmoid, added the comment in rbm.py.

@GaelVaroquaux

Copy link
Copy Markdown
Member

As far as I am concerned, this is good to merge.

I have one additional nitpick (optional): can/should the _log_logistic_sigmoid function be removed (by inlining it in the log_logistic one) and the _logistic_sigmoid.py file removed? It would be great in terms of simplification of the code base.

@larsmans

Copy link
Copy Markdown
Member Author

That's a Cython function.

@GaelVaroquaux

Copy link
Copy Markdown
Member

That's a Cython function.

I am sorry. I made this comment in St Pancras, before boarding the
eurostar, and I had horrible Internet connection. I had in mind that I
was maybe making a stupid comment. It turns out I was.

👍 for merge. Thanks for your work, as always!

@larsmans

Copy link
Copy Markdown
Member Author

That's ok :)

+1 from Gaël and Olivier, merging; Vlad, if you object to the (shorter) function name, feel free to change it in master.

larsmans added a commit that referenced this pull request Feb 23, 2014
Speed up RBM training with scipy.special.expit
@larsmans
larsmans merged commit c52476a into scikit-learn:master Feb 23, 2014
@larsmans
larsmans deleted the expit branch February 23, 2014 21:41
@IssamLaradji

Copy link
Copy Markdown
Contributor

I am honored to be pinged and to have a say in this (I hope it wasn't by mistake :P), it indeed has made multilayer perceptron faster. In one test case, it decreased the running time from 10.15 seconds to 8.4 seconds; that's around 17% decrease!
Other test cases showed that the larger the number of hidden neurons the more the decrease.
So I definitely support this, and I do support it being named something more meaningful - like logistic_sigmoid.
Thanks for bringing this up!

@GaelVaroquaux

Copy link
Copy Markdown
Member

I am honored to be pinged and to have a say in this (I hope it wasn't by mistake :P),

You are an active contributor knowledgeable on this part of the codebase.
Off course you have a say in this!

Gaël

@IssamLaradji

Copy link
Copy Markdown
Contributor

You are an active contributor knowledgeable on this part of the codebase.
Off course you have a say in this!

+1, Thank you thank you :)

~Issam

@mblondel

Copy link
Copy Markdown
Member

I think expit is a horrible name that won't speak to any ML person. Also, using the name expit introduces an inconsistency: it's not immediately obvious that log_logistic is the log of expit. So not big deal but I personally prefer the old solution of having logistic_sigmoid function in exthmath that uses scipy's expit function internally (if available).

@argriffing

Copy link
Copy Markdown

Just thought I'd point out that scipy.special.expit has been buggy until recently, so you might want to keep your custom implementation for a while...

@larsmans

larsmans commented Mar 2, 2014

Copy link
Copy Markdown
Member Author

Hmm... it passes our stability tests, and I haven't seen trouble with it yet. Maybe we need some better tests.

@larsmans

larsmans commented Mar 2, 2014

Copy link
Copy Markdown
Member Author

Confirmed, when I crank the logit in the test up to 710, I get NaN. 710 is not even an extreme number of features or hidden units...

@GaelVaroquaux

Copy link
Copy Markdown
Member

@argriffing: what's 'recently'? For what scipy version should we start
relying on scipy's expit?

@argriffing

Copy link
Copy Markdown

@GaelVaroquaux I think the PR I linked has made it into 0.14 which is in the process of being released.

@GaelVaroquaux

Copy link
Copy Markdown
Member

@GaelVaroquaux I think the PR I linked has made it into 1.14 which is in the process of being released.

OK, thanks. So I am afraid that that means that we can support scipy's
expit only for 0.14 and greater.

larsmans added a commit that referenced this pull request Mar 3, 2014
Fixes issue reported by @argriffing at #2882: expit is buggy in
SciPy < 0.14 (to be released). Switch implementation back to
old one based on stability test; toughened unit tests.
@ogrisel

ogrisel commented Mar 10, 2014

Copy link
Copy Markdown
Member

@larsmans did you run the test on scipy master branch (future 0.14 release)? do the test pass? Is the speed up still as important with scipy 0.14's expit implementation?

@ogrisel

ogrisel commented Mar 10, 2014

Copy link
Copy Markdown
Member

Also @larsmans what do you think of @mblondel's observation at #2882 (comment) ? I think I tend to agree with him.

@larsmans

Copy link
Copy Markdown
Member Author

@ogrisel I used SciPy 0.12. The next release should get an even faster expit.

As for the name, feel free to change it back. I don't like "expit" either, but I went for compatibility.

@WarrenWeckesser

WarrenWeckesser commented Dec 11, 2021 •

Copy link
Copy Markdown

FYI: SciPy 1.8.0 will have log_expit in the scipy.special subpackage.

I see there is no love for the name expit. The name log_expit was used for compatibility with expit, but if anyone feels strongly enough about it, feel free to create a SciPy issue to propose renaming the functions. (Personally, I wouldn't be opposed to that.)

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.

8 participants