Repository navigation
Conversation
|
@ogrisel I switch to your preferred alternative. Do you have any other thoughts regarding the feature on its own and if this is worth introducing it? |
|
I like the idea of using batches to speed things up. Is there a way to make use of them without exposing this extra parameter? I'm not sure what I'd set it to, a few GBs sounds reasonable - so maybe that? The default is 1GB, so that sounds about right. I'm also thinking that probably the speed up vs used memory curve quickly levels off. Going from one sample per call to 100 per call already cuts down the validation overhead by ~100x, to get another factor 100 reduction in overhead you'd have to pass 10_000 samples. And surely by this time the overhead from validation is pretty small compared to the computation time? All this makes me think: could we make the parameter be a on/off switch that uses a reasonable batch size? Easier to think about for a user, maybe simpler to implement and gets most of the benefit? |
|
Or set |
|
ping @adrinjalali to have your thoughts? |
|
I think people have different enough RAM available to them that it makes sense to have this parameter exposed. One could be running this on a very small machine (cloud or not), could be running on a laptop with 8-16GB, or a laptop with 64GB, or a large machine somewhere. So it makes sense to be able to configure this. The one thing that would make me think we might not want to expose this, is if using 10GB instead of 1GB would NOT make a difference. And a 2-3x difference is already significant enough for me. |
|
Edit: actually the the way the code is shown, we also measure some caching operation from matplotlib so there is no speed up on this particular example :) |
|
I'm surprised that this is what I see on this branch: In [1]: # %%
...: import numpy as np
...: from sklearn.datasets import make_regression
...: from sklearn.ensemble import RandomForestRegressor
...: from sklearn.inspection import partial_dependence
...:
...: X, y = make_regression(n_samples=100_000, n_features=30, random_state=0)
...: model = RandomForestRegressor(n_jobs=8).fit(X, y)
...:
In [2]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)
1min ± 702 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [3]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1_000)
...:
59.8 s ± 551 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [4]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=10_000)
1min 1s ± 669 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) |
|
@adrinjalali Since you have a |
|
I'm not sure how I can trigger the benefit of this PR 😁 Here's more data: In [2]: ...: import numpy as np
...: ...: from sklearn.datasets import make_regression
...: ...: from sklearn.ensemble import RandomForestRegressor
...: ...: from sklearn.inspection import partial_dependence
...: ...:
...: ...: X, y = make_regression(n_samples=100_000, n_features=30, random_state=0)
...: ...: model = RandomForestRegressor(n_jobs=8, max_depth=7).fit(X, y)
...: ...:
In [3]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)
6.91 s ± 35.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [4]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1000)
6.42 s ± 37.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [5]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=10000)
6.51 s ± 75.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [6]: ...: import numpy as np
...: ...: from sklearn.datasets import make_regression
...: ...: from sklearn.ensemble import RandomForestRegressor
...: ...: from sklearn.inspection import partial_dependence
...: ...:
...: ...: X, y = make_regression(n_samples=100_000, n_features=30, random_state=0)
...: ...: model = RandomForestRegressor(n_jobs=8, max_depth=4).fit(X, y)
...: ...:
In [7]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1)
4.85 s ± 63.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [8]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=1000)
4.73 s ± 62.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [9]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=10000)
4.84 s ± 173 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [10]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=20000)
4.7 s ± 23.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [11]: %timeit partial_dependence(model, X, features=[0], method="brute", max_memory_mb=30000)
4.72 s ± 15.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) |
|
I have a question about API: Wouldn't it be something completely new for users to specify some amount of memory (RAM) limit? I would prefer to have a much simpler, more boolean option like |
We have something similar for the However, the current experiment does not make show that we scale with the available amount of memory available. Therefore, if I cannot have something more conclusive, exposing this parameter as-is will not be an option. In case, that actually a 1 MB memory is always optimum then, we just don't need any parameter at all. I'll come back on this PR just right after dealing with the one in the 1.6 milestone to be sure that we release the RC next week. |
Related to #28375
TODO:
This PR proposes to add a new parameter to control the maximum amount of memory allowed to be used during the computation of the partial dependence "brute" computation.
In short, instead of computing a single point from the grid, we could have a batch approach. For each point in the grid, we request the estimator to predict on
X. Therefore, the total memory footprint isn_points * X.bytes. So there is a trade-off between memory and speed. We gain speed by callingpredicton few matrices reducing non-negligible validation overhead at the cost of a much larger memory footprint.The new parameter allows to control the memory footprint to not exceed a certain amount.
Benchmark
When using the private
_partial_dependence_brutefunction in #28375, @mayer79 gets a x10 speed-up that could be handy.Additional thoughts
Note that we could call
_partial_dependence_brutein #28375 and meet the similar speed-up as the current fast computation. I did run the bench exposed in #28375 (comment) and I have similar results. The plus here is that we provide the user a control in case that our default is not suitable.