Skip to content

[MRG] Use joblib.Parallel for CalibratedClassifierCV.fit - #17107

Merged
ogrisel merged 37 commits into
scikit-learn:masterfrom
jjerphan:calibratedclassifierCV_joblib_parallel
Jul 26, 2020
Merged

ogrisel merged 37 commits into
scikit-learn:masterfrom
jjerphan:calibratedclassifierCV_joblib_parallel

Conversation

@jjerphan

@jjerphan jjerphan commented May 2, 2020

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #17097.

What does this implement/fix? Explain your changes.

CalibratedClassifierCV fit estimator sequentially.

This PR introduces joblib.Parallel to calibrate estimators in parallel in CalibratedClassifierCV.fit

Parameters have been added to the constructor of CalibratedClassifierCV to match the ones from joblib.Parallel.

Any other comments?

This is my first contribution to scikit-learn.

I took some time to read the guidelines in the documentation, but I haven't found the kinds of tests such a contribution needs.

@jnothman

jnothman commented May 3, 2020

Copy link
Copy Markdown
Member

I haven't found the kinds of tests such a contribution needs.

Usually a combination of: provide a benchmark in discussion; and add a test checking that the result with parallelism is equal to the result without.

@jjerphan

jjerphan commented May 3, 2020

Copy link
Copy Markdown
Member Author

I've proposed some benchmarks; I'll post the results once I got them executed..

Let me know if you need other ones.

@jjerphan

jjerphan commented May 3, 2020 •

Copy link
Copy Markdown
Member Author

Some first results got on a dataset of 10 000 samples.

BenchmarkNJobsAlgo {'estimator_name': 'LogisticReg.', 'n_jobs': 1} 10 trials: 0.2859 ± 0.1318 s
BenchmarkNJobsAlgo {'estimator_name': 'LogisticReg.', 'n_jobs': 2} 10 trials: 0.2500 ± 0.3721 s
BenchmarkNJobsAlgo {'estimator_name': 'LogisticReg.', 'n_jobs': 4} 10 trials: 0.1737 ± 0.3121 s
BenchmarkNJobsAlgo {'estimator_name': 'LogisticReg.', 'n_jobs': 8} 10 trials: 0.1792 ± 0.1365 s
BenchmarkNJobsAlgo {'estimator_name': 'CART', 'n_jobs': 1} 10 trials: 0.4957 ± 0.0051 s
BenchmarkNJobsAlgo {'estimator_name': 'CART', 'n_jobs': 2} 10 trials: 0.4095 ± 0.2507 s
BenchmarkNJobsAlgo {'estimator_name': 'CART', 'n_jobs': 4} 10 trials: 0.3547 ± 0.2749 s
BenchmarkNJobsAlgo {'estimator_name': 'CART', 'n_jobs': 8} 10 trials: 0.3460 ± 0.1434 s
BenchmarkNJobsAlgo {'estimator_name': 'ExtraTrees', 'n_jobs': 1} 10 trials: 4.0427 ± 0.2084 s
BenchmarkNJobsAlgo {'estimator_name': 'ExtraTrees', 'n_jobs': 2} 10 trials: 3.0686 ± 0.2907 s
BenchmarkNJobsAlgo {'estimator_name': 'ExtraTrees', 'n_jobs': 4} 10 trials: 2.9841 ± 0.3987 s
BenchmarkNJobsAlgo {'estimator_name': 'ExtraTrees', 'n_jobs': 8} 10 trials: 2.4930 ± 0.2843 s
BenchmarkNJobsAlgo {'estimator_name': 'RandomForest', 'n_jobs': 1} 10 trials: 10.7586 ± 0.4182 s
BenchmarkNJobsAlgo {'estimator_name': 'RandomForest', 'n_jobs': 2} 10 trials: 7.1585 ± 0.2358 s
BenchmarkNJobsAlgo {'estimator_name': 'RandomForest', 'n_jobs': 4} 10 trials: 6.4878 ± 0.8545 s
BenchmarkNJobsAlgo {'estimator_name': 'RandomForest', 'n_jobs': 8} 10 trials: 5.0233 ± 0.2879 s


BenchmarkCV {'cv': 2} 10 trials: 0.0634 ± 0.0140 s
BenchmarkCV {'cv': 3} 10 trials: 0.1066 ± 0.0168 s
BenchmarkCV {'cv': 4} 10 trials: 0.1521 ± 0.0490 s
BenchmarkCV {'cv': 5} 10 trials: 0.1958 ± 0.0435 s


BenchmarkNJobsCV {'cv': 2, 'n_jobs': 1} 10 trials: 0.0586 ± 0.0044 s
BenchmarkNJobsCV {'cv': 2, 'n_jobs': 2} 10 trials: 0.1773 ± 0.3157 s
BenchmarkNJobsCV {'cv': 2, 'n_jobs': 4} 10 trials: 0.2049 ± 0.3752 s
BenchmarkNJobsCV {'cv': 2, 'n_jobs': 8} 10 trials: 0.1568 ± 0.1029 s
BenchmarkNJobsCV {'cv': 3, 'n_jobs': 1} 10 trials: 0.0886 ± 0.0096 s
BenchmarkNJobsCV {'cv': 3, 'n_jobs': 2} 10 trials: 0.1720 ± 0.2619 s
BenchmarkNJobsCV {'cv': 3, 'n_jobs': 4} 10 trials: 0.1681 ± 0.3032 s
BenchmarkNJobsCV {'cv': 3, 'n_jobs': 8} 10 trials: 0.1613 ± 0.1106 s
BenchmarkNJobsCV {'cv': 4, 'n_jobs': 1} 10 trials: 0.1140 ± 0.0070 s
BenchmarkNJobsCV {'cv': 4, 'n_jobs': 2} 10 trials: 0.1745 ± 0.2710 s
BenchmarkNJobsCV {'cv': 4, 'n_jobs': 4} 10 trials: 0.1608 ± 0.3285 s
BenchmarkNJobsCV {'cv': 4, 'n_jobs': 8} 10 trials: 0.1737 ± 0.1293 s
BenchmarkNJobsCV {'cv': 5, 'n_jobs': 1} 10 trials: 0.1511 ± 0.0177 s
BenchmarkNJobsCV {'cv': 5, 'n_jobs': 2} 10 trials: 0.1954 ± 0.2658 s
BenchmarkNJobsCV {'cv': 5, 'n_jobs': 4} 10 trials: 0.1687 ± 0.3171 s
BenchmarkNJobsCV {'cv': 5, 'n_jobs': 8} 10 trials: 0.1872 ± 0.1150 s

@jjerphan

jjerphan commented May 9, 2020 •

Copy link
Copy Markdown
Member Author

Here are some results I got on my latest runs (cc @jnothman).


Medium dataset benchmark (i.e. a dataset of shape (10000, 10)).

Results have been obtains on 20 sequential runs.
The default algorithm is LogisticRegression.

Benchmark environment

  • Python: 3.8.2
  • OS: Fedora 32
  • CPU: i7-5500U @ 2.4GHz (4VCores)
  • RAM: 8GB

Single-threaded algorithm

1

Multi-threaded algorithm

2

Number of folds

3

Number of folds and number of jobs

5


Discussions

Paralleling the calibration generally results in shorter running times for medium datasets.

This is not related to this contribution but this is still good information: increasing the number of folds can results in a longer running times. The joint influence of changing the number of folds and the number of jobs has to be studied.

jjerphan added 2 commits May 9, 2020 11:58
The calibration function has to be defined outside of
CalibratedClassifierCV.fit for pickling reasons.

This defines it at the file level.

It has been renamed and documented accordingly.
@jjerphan

jjerphan commented May 9, 2020 •

Copy link
Copy Markdown
Member Author

Large dataset benchmark (i.e. a dataset of shape (100000, 10)).

Results have been obtains on 20 sequential runs.
The default algorithm is LogisticRegression.

Benchmark environment

  • Python: 3.8.2
  • OS: Fedora 32
  • CPU: i7-5500U @ 2.4GHz (4VCores)
  • RAM: 8GB

Single-threaded algorithm

2

Multi-threaded algorithm

Too long to perform on my machine.

Number of folds

1

Number of folds and number of jobs

3


Discussions

Parallelizing the calibration generally also results in shorter running times for larger datasets.

@jjerphan jjerphan changed the title [WIP] Use joblib.Parallel for CalibratedClassifierCV.fit [MRG] Use joblib.Parallel for CalibratedClassifierCV.fit May 9, 2020
@thomasjpfan

Copy link
Copy Markdown
Member

@jjerphan Those look like good results. May you provide the code you use for benchmarking?

@jjerphan

jjerphan commented May 9, 2020

Copy link
Copy Markdown
Member Author

@thomasjpfan: Thanks ! The code is in benchmarks/bench_calibration.py

@jjerphan

jjerphan commented May 17, 2020 •

Copy link
Copy Markdown
Member Author

@thomasjpfan: have you have time to perform those benchmark yourself?
I'll try to run them on other configurations. My hypothesis is that making it parallel might help in some situations but could come with slow downs if bigger datasets are used.

@jjerphan

Copy link
Copy Markdown
Member Author

Here are some new benchmarks I've performed for singly-threaded algorithm.
benchmarknjobssinglethreadalgo_1000_10_20
benchmarknjobssinglethreadalgo_1000_100_20
benchmarknjobssinglethreadalgo_10000_10_20
benchmarknjobssinglethreadalgo_10000_100_20
benchmarknjobssinglethreadalgo_100000_10_20
benchmarknjobssinglethreadalgo_100000_100_20

For multi-threaded algorithms, as they are using joblib.Parallel there might be some Parallel context nesting hence results aren't necessarily better.

@thomasjpfan thomasjpfan left a comment

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.

Thank you for the additional benchmark results. I am wondering if we need the benchmark script in the PR itself. Usually including the benchmark in a gist or the discussion is enough.

For multi-threaded algorithms, as they are using joblib.Parallel there might be some Parallel context nesting hence results aren't necessarily better.

That makes sense. It's nice that joblib handles most of the over subscription as well.

Comment thread sklearn/calibration.py Resolved
Comment thread sklearn/calibration.py Resolved
Comment thread sklearn/tests/test_calibration.py Resolved
Comment thread sklearn/calibration.py Resolved

@thomasjpfan thomasjpfan left a comment

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.

LGTM

Comment thread sklearn/calibration.py Resolved
Co-authored-by: Thomas J. Fan <[email protected]>
@jjerphan

Copy link
Copy Markdown
Member Author

Hi @thomasjpfan, shall I ask someone to be a second reviewer for this PR? 🙂
Thank you!

@jjerphan
jjerphan requested a review from thomasjpfan July 11, 2020 13:40
@sahinbatmaz

Copy link
Copy Markdown

Is it ready to be merged ? 🙂

@jjerphan

Copy link
Copy Markdown
Member Author

It is ready and just needs another approval in order to be merged.

@thomasjpfan thomasjpfan left a comment

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.

Minor name change.

Comment thread sklearn/calibration.py Resolved
Comment thread sklearn/calibration.py Resolved
Comment thread sklearn/calibration.py Resolved
@thomasjpfan

Copy link
Copy Markdown
Member

@glemaitre If you are interested, this should be a pair-straight forward review.

@jjerphan

Copy link
Copy Markdown
Member Author

This message comes as a small follow-up: can this PR be merger? :)

cc @glemaitre.

@ogrisel ogrisel left a comment

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.

Overall this LGTM. Here are two suggestions for improvements:

Comment thread sklearn/tests/test_calibration.py Resolved
Comment thread sklearn/calibration.py Resolved
@jjerphan
jjerphan requested a review from ogrisel July 26, 2020 12:15
@ogrisel
ogrisel merged commit 96a364e into scikit-learn:master Jul 26, 2020
@ogrisel

ogrisel commented Jul 26, 2020

Copy link
Copy Markdown
Member

Thank you very much for your contribution @jjerphan!

@jjerphan

Copy link
Copy Markdown
Member Author

Thank you for your time and reviews @jnothman and @ogrisel!

@jjerphan
jjerphan deleted the calibratedclassifierCV_joblib_parallel branch July 26, 2020 13:39
jayzed82 pushed a commit to jayzed82/scikit-learn that referenced this pull request Oct 22, 2020
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.

CalibratedClassifierCV does not support joblib parallel computing.

5 participants