You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are several projects working on smarter ways to search parameters.
Right now, BaseSearchCV makes it really hard to customize parts, mostly because the fit function is really long, and changing anything in the parameter loop means copying the whole fit method, see here.
Maybe factoring this out into a method, or just splitting up fit a bit more might be a good idea?
Reacted by Scott Sievert, Dave Liu and joshdunnlime
I think (without looking in detail) that the SMAC implementation could have reused BaseSearchCV/RandomizedSearchCV more, albeit with a little overhead in each run.
I would be +1 for something like:
def search_cv(estimator, candidates, X, y, ...):
...
return cv_results_
Maybe. You'd have to consolidate all the dictionaries you get at the end. I guess that would be simpler than copying all the code.
Hm it depends a bit what candidates is in your example. I don't think that's actually needed, since we already have _get_param_iterator, and any optimization would create these on the fly?
From my point of view the tricky bit is passing feedback from _fit_and_score to the iterator from _get_param_iterator. So I would suggest splitting things somewhere around here and replace the first part in scikit-optimize.
@betatim yeah that's about where I thought. Though I wouldn't split the fit method, I would just encapsulate the call that generates the candidates and gets the results.
I agree that co-routine is probably the best pattern for this. Do you know how that would work with the Parallel interface, though?
I would still not have skopt overwrite fit, because most of the fit is parameter validation and result parsing, and these are exactly the parts they don't want to copy.
Btw, @betatim, the problem with passing results back from _fit_and_score is that I don't want to make any promises about its interface. I would rather return a list of dicts even if it takes some overhead to transform it. I'm still not sure what the right format for returning things here is.
We can also achieve a similar effect to a coroutine with a class having an evaluate method (rather than the awkward send). It makes it slightly messier to use a parallel context manager, but perhaps produces a cleaner interface overall.
Thinking out loud here: there are a lot of things already in RandomSearch that I'd prefer not to have to write myself: argument validation (first part of fit), the contents of _fit_and_score and the aggregation of results (second part of fit).
"All" I want to do is be given results so far and asked for a new set of parameters to try. How can we make it so that these lines become customisable while keeping everything else fixed? I like the idea of sending stuff into a generator. How would it work interface wise for that generator to return more than one new point per iteration (for parallelism)? My guess: you always send in all past results and the generator returns a list of next points per iteration. Does mean the generator has to know something about how "parallel" you want to be running.
With a setup like this all skopt would have to do is provide BaseSearchCV with a custom generator as argument to __init__ right?
could _fit_and_score return estimator with ret? then in BaseSearchCV().fit(), we can get all trained estimators, and can get top 5 or top 10 best estimators, other than using refit=True to retrain the best estimator and get the best_estimator_
@jnothman i think each estimator was assigned to hyper-parameter other than fold, every parameter can get an estimator which was trained in the func _fit_and_score, so we can get or pickle it(and how to pickle these estimator? i cannot find examples), and not need to fit the best estimator again by set refit=True
Should _generate_candidates be a function of fit arguments (X, y, ...)?
you mean that it would get X and y as parameters? I don't see the immediate advantage.
There is one more related thing that is not immediately supported, which is the manipulating (subsampling) of the X and y by the search coroutine. (Is this what you meant?) If this would somehow be possible that would be great, but I suppose there are also other ways of achieving this within the search routine (e.g., wrapping the estimator).
#9599 implements the functionality I'd like to see: there is some function call after each evaluation, which means it works for adaptive selection schemes (e.g., Bayesian approaches and Hyperband). I've added a review, and will be watching. This would enable the adaptive implementations discussed in dask/dask-ml#161.
Please explain your use case for overriding _format_results, @janvanrijn? If it's adding a measure that can be estimated independently for each fit, could you do it with a custom scorer?
Please explain your use case for overriding _format_results, @janvanrijn? If it's adding a measure that can be estimated independently for each fit, could you do it with a custom scorer?
Sorry for the slow reply!
For a hyperband or successive halving implementation, it wouldn't be a bad idea to add meta-data to the cv_results_ field, that indicate stuff such as
the subsample size (indicating which stage of successive halving we are)
the bracket number (indicating which stage of hyperband we are)
the hyperband iteration number (since the algorithm can be repeated multiple times)
This can be used by the callable refit function as implemented in #11354, to determine which hyperparameter setting should be chosen (the original algorithm claims that only hyperparameters as ran on the maximum subsample size can be recommended)
I'd rather implementers not play with _format_results, but we should have some contact about how the custom search function is allowed to modify the results. Hmmm.
I'd rather implementers not play with _format_results, but we should have some contact about how the custom search function is allowed to modify the results. Hmmm.
I'll be at SciPy next week.
Currently, I do not completely get the whole idea behind results_container (line 682). Some questions that I wasn't able to answer:
why is it a list of dict (we seem only interested in element 0)
line 732: why results = results_container[0] if we can also grab the result from the return of _run_search
if point 2 were the case, the evaluate_candidates fn could be wrapped and the results could be enhanced by search implementations (I think .. )
Please help improve the documentation in my branch.
Still interested in this? How should I add stuff? Mention it here or PR to your fork?
results_container is because python 2 has "local" and "global" variable
scope, but not "nonlocal". it is an implementation detail (a hack) that can
disappear when we end python 2 support.
Yes, I've considered letting _run_search return the last results... But in
most cases this is unnecessary. it implies that _run_search can modify the
results arbitrarily, and I don't like that idea,or that then we might need
to implement checks on the results returned if we support custom
_run_search implementations.
And it's best to comment on the pr if it's about implementation
@jnothman , what was the motivation for passing evaluate_candidates() to _run_search versus e.g. just making it a private method?
If a method, how would evaluate_candidates be provided the other variables it needs to do its job? evaluate_candidates is a closure over several other variables available in _fit. If it were a method, then those variables would need to be stored on the object. This is a cleaner functional abstraction (but maybe it's more functional paradigmatically than a lot of code in scikit-learn)
Dear sir, how to use GridSearchCV or RandomizedSearchCV in a huge dataset? I want to load the data in batch from disk with a generator to avoid MemoryError, then I can feed the batch data to fit method.
Maybe like this:
model = RandomizedSearchCV()
model.fit(batch_data) #where batch_data is a generator`
replace:
model = RandomizedSearchCV()
model.fit(x, y)
As you can see, we can feed a generator into tensorflow model's fit method and this is pretty useful to huge dataset, like a images dataset.
Thank you very much. @jnothman
Activity
jnothman commentedon Aug 5, 2017
I think (without looking in detail) that the SMAC implementation could have reused BaseSearchCV/RandomizedSearchCV more, albeit with a little overhead in each run.
I would be +1 for something like:
WDYT?
amueller commentedon Aug 9, 2017
yeah, that would be good, I think. I don't think SMAC could have reused more. How?
jnothman commentedon Aug 9, 2017
amueller commentedon Aug 11, 2017
Maybe. You'd have to consolidate all the dictionaries you get at the end. I guess that would be simpler than copying all the code.
Hm it depends a bit what
candidatesis in your example. I don't think that's actually needed, since we already have_get_param_iterator, and any optimization would create these on the fly?betatim commentedon Aug 13, 2017
From my point of view the tricky bit is passing feedback from
_fit_and_scoreto the iterator from_get_param_iterator. So I would suggest splitting things somewhere around here and replace the first part in scikit-optimize.jnothman commentedon Aug 13, 2017
There's also a subtle trickiness in ensuring the CV splits are the same across calls and the fact that we could reuse the parallel backend.
So we basically want a coroutine. Either:
and the search does something like:
or inverted:
which may make more sense.
jnothman commentedon Aug 13, 2017
Perhaps the latter case more refined is:
In the latter case, your skopt code might be:
amueller commentedon Aug 13, 2017
@betatim yeah that's about where I thought. Though I wouldn't split the fit method, I would just encapsulate the call that generates the candidates and gets the results.
I agree that co-routine is probably the best pattern for this. Do you know how that would work with the Parallel interface, though?
I would still not have skopt overwrite fit, because most of the fit is parameter validation and result parsing, and these are exactly the parts they don't want to copy.
jnothman commentedon Aug 13, 2017
jnothman commentedon Aug 14, 2017
Btw, @betatim, the problem with passing results back from
_fit_and_scoreis that I don't want to make any promises about its interface. I would rather return a list of dicts even if it takes some overhead to transform it. I'm still not sure what the right format for returning things here is.We can also achieve a similar effect to a coroutine with a class having an
evaluatemethod (rather than the awkwardsend). It makes it slightly messier to use a parallel context manager, but perhaps produces a cleaner interface overall.betatim commentedon Aug 21, 2017
Thinking out loud here: there are a lot of things already in
RandomSearchthat I'd prefer not to have to write myself: argument validation (first part offit), the contents of_fit_and_scoreand the aggregation of results (second part offit)."All" I want to do is be given results so far and asked for a new set of parameters to try. How can we make it so that these lines become customisable while keeping everything else fixed? I like the idea of sending stuff into a generator. How would it work interface wise for that generator to return more than one new point per iteration (for parallelism)? My guess: you always send in all past results and the generator returns a list of next points per iteration. Does mean the generator has to know something about how "parallel" you want to be running.
With a setup like this all
skoptwould have to do is provideBaseSearchCVwith a custom generator as argument to__init__right?jnothman commentedon Aug 22, 2017
I was a bit uncertain about your statement, so I just mocked up one possible solution at #9599.
jerryfuyu0104 commentedon Mar 8, 2018
could _fit_and_score return estimator with ret? then in BaseSearchCV().fit(), we can get all trained estimators, and can get top 5 or top 10 best estimators, other than using refit=True to retrain the best estimator and get the best_estimator_
jnothman commentedon Mar 8, 2018
jerryfuyu0104 commentedon Mar 8, 2018
@jnothman i think each estimator was assigned to hyper-parameter other than fold, every parameter can get an estimator which was trained in the func _fit_and_score, so we can get or pickle it(and how to pickle these estimator? i cannot find examples), and not need to fit the best estimator again by set refit=True
16 remaining items
janvanrijn commentedon Jun 15, 2018
you mean that it would get X and y as parameters? I don't see the immediate advantage.
There is one more related thing that is not immediately supported, which is the manipulating (subsampling) of the X and y by the search coroutine. (Is this what you meant?) If this would somehow be possible that would be great, but I suppose there are also other ways of achieving this within the search routine (e.g., wrapping the estimator).
jnothman commentedon Jun 15, 2018
No, I didn't mean for that purpose... we assume constant CV indices through all experiments.
I meant perhaps so that the search could select good initial candidates given the data.
janvanrijn commentedon Jun 15, 2018
Ah, of course. That would make perfect sense indeed.
stsievert commentedon Jun 15, 2018
#9599 implements the functionality I'd like to see: there is some function call after each evaluation, which means it works for adaptive selection schemes (e.g., Bayesian approaches and Hyperband). I've added a review, and will be watching. This would enable the adaptive implementations discussed in dask/dask-ml#161.
I'll try to make #11266 work with #9599.
That's been my experience too.
jnothman commentedon Jun 16, 2018
Please explain your use case for overriding
_format_results, @janvanrijn? If it's adding a measure that can be estimated independently for each fit, could you do it with a custom scorer?janvanrijn commentedon Jul 2, 2018
Sorry for the slow reply!
For a hyperband or successive halving implementation, it wouldn't be a bad idea to add meta-data to the
cv_results_field, that indicate stuff such asThis can be used by the callable refit function as implemented in #11354, to determine which hyperparameter setting should be chosen (the original algorithm claims that only hyperparameters as ran on the maximum subsample size can be recommended)
jnothman commentedon Jul 3, 2018
I'd rather implementers not play with _format_results, but we should have some contact about how the custom search function is allowed to modify the results. Hmmm.
janvanrijn commentedon Jul 3, 2018
I'll be at SciPy next week.
Currently, I do not completely get the whole idea behind results_container (line 682). Some questions that I wasn't able to answer:
results = results_container[0]if we can also grab the result from the return of_run_searchevaluate_candidatesfn could be wrapped and the results could be enhanced by search implementations (I think .. )Still interested in this? How should I add stuff? Mention it here or PR to your fork?
jnothman commentedon Jul 3, 2018
NicolasHug commentedon Oct 3, 2019
@jnothman , what was the motivation for passing
evaluate_candidates()to_run_searchversus e.g. just making it a private method?jnothman commentedon Oct 4, 2019
If a method, how would
evaluate_candidatesbe provided the other variables it needs to do its job?evaluate_candidatesis a closure over several other variables available in_fit. If it were a method, then those variables would need to be stored on the object. This is a cleaner functional abstraction (but maybe it's more functional paradigmatically than a lot of code in scikit-learn)wangyexiang commentedon Nov 12, 2019
Dear sir, how to use
GridSearchCVorRandomizedSearchCVin a huge dataset? I want to load the data in batch from disk with a generator to avoid MemoryError, then I can feed the batch data tofitmethod.Maybe like this:
replace:
As you can see, we can feed a generator into tensorflow model's
fitmethod and this is pretty useful to huge dataset, like a images dataset.Thank you very much.
@jnothman
jnothman commentedon Nov 12, 2019