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
{{ message }}
Repository navigation
Fitting additional estimators for ensemble methods #1585
I would like to propose an additional instance method to the ensemble estimators to fit additional sub-estimators. I kluged up an implementation for gradient boosting that appears to work through my limited testing. I was thinking the signature would be something like
deffit_extend(self, X, y, n_estimators):
where self.n_estimators += n_estimators is updated as so. I don't think fit_extend is a particularly great name so I'd welcome other suggestions. Perhaps we would want to hash the features and labels when fit() is called so we can check that the same features and labels are provided to this function.
If people think this would be a useful addition I would be willing to put together a PR, it seems like it should be straightforward to implement and add tests/docs for.
This is definitely a feature we want. The question is: what would be the best way to implement it (in terms of API)?
There is something slightly similar in the adaboost pr: #522. That implements predicting with a subset of the estimators, which is also very helpful.
What do you think does the scenario / code look like, where a user wants fit_extend? It is probably most useful in an interactive setting, righ?
There is a slightly related function in SGD, partial_fit. That is actually for online learning, though, so it gets different data.
I'd like to get this feature with adding as little API an names as possible ;)
I would like to train a small number of sub-estimators at a time (and wait a relatively short time). Then test it on my cross-validation set and if my cv score is still falling, I can continue training. As opposed to training a large number of sub-estimators and waiting a long time (several hours for me). That was my motivation.
I can understand being hesitant about adding another instance method. I thought it might be worthwhile to add another optional parameter to fit() but I saw this quote on the contributing page.
fit parameters should be restricted to directly data dependent variables
So I wasn't sure that would be a good idea. Would
deffit(self, X, y, n_estimators=self.n_estimators)
be acceptable? Then if n_estimators > self.n_estimators, we'll then train that many more estimators.
I agree that adding in n_estimators parameter to the prediction method is nice, but I think you'll agree that it solves a different problem. For my problem performing grid search over n_estimators isn't really an option because it takes so long.
Until we agree on a proper interface to do that, you could use the following hack:
# Train a forest of 10 trees
clf1 = RandomForestClassifier(n_estimators=10)
clf1.fit(X, y)
# Train a second forest of 10 trees
clf2 = RandomForestClassifier(n_estimators=10)
clf2.fit(X, y)
# Extend clf1 with clf2
clf1.estimators_.extend(clf2.estimators_)
clf1.n_estimators += clf2.n_estimators
# clf1 now counts 20 trees
Thinking about it a bit more, I think the partial_fit method would be the right interface. In SGD you can call partial_fit either with the same data or new data and it keeps on learning. The difference is that in SGD, if you manually iterate over batches, you get the original algorithm out. For ensembles, that would not be true. You would need to use the whole data on each call to partial_fit.
Just to clarify, what would exactly happen in partial_fit in case of ensembles? Would that add n_estimators more estimators, wheren_estimators is the parameter value from the constructor? (or could we change that value?)
Good question. I also thought about that ;) actually, you would want to change that, right? you could change that afterwards by set_params but that feels awkward :-/
I agree that we need such a functionality, however, I'm not sure if fit_extends is the best solution to the problem that @jwkvam describes. In order to do early stopping the user has to write some some code that basically repeatedly calls fit_extends and then checks the CV error.
I'd rather propose the monitor fit parameter that we discussed in the past: est.fit(X, y, monitor=some_callable) where some_callable will be called after each iteration and is passed the complete state of the estimator. The callable could also return a value whether or not the training should proceed.
Using such an api one could implement not only early stopping but also custom reporting (e.g. interactive plotting the training vs. testing score) and snapshoting (all X iterations dump the estimator object and copy it to some location; this is great if you are running on EC2 spot instances or some other unreliable hardware ;-)
Even with such a monitor API, however, I think there would be a need for an API to fit more estimators once the model has been fitted (i.e. fit_extends) - often one trains a model and does some introspection to find that its probably better to have run more iterations - existing estimators use the warm_start parameter to implement such a functionality (e.g. see linear_model.ElasticNet) - here is the docstring of the parameter::
warm_start : bool, optional
When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution.
Personally, I'd prefer fit_extends (or fit_more) over warm_start - warm start is quite implicit - you have to::
est = GradientBoostingRegressor(n_estimators=1000)
est.fit(X, y)
# now we want to fit more estimators to ``est``
# if you forget warm_start=True you nuke your previous estimators - quite implicit
est.fit(X, y, n_estimators=2000, warm_start=True)
# alternatively - more explicit
est.fit_more(X, y, n_estimators=1000)
After some thoughts, I think we should see the bigger picture here. In a near future, I would like to implement generic meta-ensembles that could combine any kind of estimators together. What I rather see is a "combination" mechanism that would take as input a list of (fitted) estimators and would produce a meta-estimator combining them all.
In practice, I think we can achieve that without adding any new function to our API. For example, one could simply pass such a list of fitted estimators to the constructor of the meta-ensemble.
In terms of API, one could (roughly) implement such ensembles in the following way:
a) Bagging:
constructor: base_estimator (optional), n_estimators (>=0), a list L of fitted estimators (optional).
fit: extend L with n_estimators new instances of base_estimators fitted over (bootstrap copies of) the training samples. If no base estimator is given, then it is equivalent to combining the estimators in L.
b) Stacking:
constructor: base_estimator (optional), n_estimators (>=0), a list L of fitted estimators (optional).
fit: extend L with n_estimators new instances of base_estimators fitted of bootstrap samples, then refit a model over the predictions of the estimators.
c) Forest:
constructor: base_estimator (optional), n_estimators (>=0), a list L of fitted estimators or a forest (optional).
fit: extend L with n_estimators new instances of base_estimators fitted over the training samples. Here we could also check whether the estimators in L are forests or decision trees. Forests would be flattened in order to put all trees on the same level.
Also, in such a framework, computation of an ensemble could easily be distributed over several machines: build your estimators; pickle them; then recombine them into one single meta-estimator. One could even wrap that interface into a MapReduce cluster, without digging into our implementation at all!
What do you think? I am aware this is only relevant to some kind of ensembles though. For instance, GBRT and AdaBoost are (in my opinion) more suited to either warm_restart or partial_fit.
What is the motivation of that interface? I am totally with you in supporting more ensemble methods. I just feel it is quite awkward to have a different interface for GBRT and random forest. I don't really see the motivation for that.
If the main motivation is to distribute embarassingly parallel jobs, then I think we should attack this by implementing a more powerful parallelization. Doing it the way you described seems pretty manual and hacky.
Basically I feel your proposal just solves a very special case and leaves most cases unsolved.
What is the use-case for your interface except parallelization? Or better: in what use cases do you need a different interface for boosted ensembles and bagging?
The use case is when you want to combine several estimators together. It is natural for average-like ensembles, but makes no sense in boosted ensembles. In that perspective, I see "extending an estimator" as "combining" it with more base estimators.
So the setting is that you have trained some bagging estimators and want to combine them together, right?
In which setting do you want to do that except for parallelization? It is not so clear to me but maybe I'm overlooking something obvious.
I'm not sure I get the stacking example. I would have imagined that if we had a stacking interface, you could specify one estimator as the base estimator and another as the one on top.
@jwkvam We recently agreed in #2570 to implement this feature using the warm_start parameter. It is now implemented in GBRT. I'll try to update the forests with the same mechanism before the release.
@glouppe You're right, I forgot I had written this for any ensemble. But really I just wanted it for GBRT :) so in my haste, I decided this issue was resolved. If you like you can reopen it and close it when you are done, it doesn't matter to me.
Activity
amueller commentedon Jan 23, 2013
This is definitely a feature we want. The question is: what would be the best way to implement it (in terms of API)?
There is something slightly similar in the adaboost pr: #522. That implements predicting with a subset of the estimators, which is also very helpful.
What do you think does the scenario / code look like, where a user wants
fit_extend? It is probably most useful in an interactive setting, righ?There is a slightly related function in SGD,
partial_fit. That is actually for online learning, though, so it gets different data.I'd like to get this feature with adding as little API an names as possible ;)
amueller commentedon Jan 23, 2013
Btw, I wouldn't hash
Xandy. I don't see a reason to force the user to provide the same input data.jwkvam commentedon Jan 25, 2013
I would like to train a small number of sub-estimators at a time (and wait a relatively short time). Then test it on my cross-validation set and if my cv score is still falling, I can continue training. As opposed to training a large number of sub-estimators and waiting a long time (several hours for me). That was my motivation.
I can understand being hesitant about adding another instance method. I thought it might be worthwhile to add another optional parameter to fit() but I saw this quote on the contributing page.
So I wasn't sure that would be a good idea. Would
be acceptable? Then if
n_estimators > self.n_estimators, we'll then train that many more estimators.I agree that adding in n_estimators parameter to the prediction method is nice, but I think you'll agree that it solves a different problem. For my problem performing grid search over n_estimators isn't really an option because it takes so long.
glouppe commentedon Jan 25, 2013
Until we agree on a proper interface to do that, you could use the following hack:
glouppe commentedon Jan 25, 2013
Note that this only work for RandomForest and ExtraTrees. The same trick cannot be used with Gradient Boosting.
amueller commentedon Jan 26, 2013
See #1626. Would early stopping be an acceptable solution to you?
jwkvam commentedon Jan 29, 2013
@amueller I share the same opinion as @glouppe here #1626 (comment). I like early stopping but it doesn't resolve this in my opinion.
amueller commentedon Jan 29, 2013
Ok. Then we should look for a solution that allows for early stopping and adding additional estimators.
amueller commentedon Jan 29, 2013
Thinking about it a bit more, I think the
partial_fitmethod would be the right interface. In SGD you can callpartial_fiteither with the same data or new data and it keeps on learning. The difference is that in SGD, if you manually iterate over batches, you get the original algorithm out. For ensembles, that would not be true. You would need to use the whole data on each call topartial_fit.GaelVaroquaux commentedon Jan 29, 2013
I like this suggestion. What do other people think?
glouppe commentedon Jan 30, 2013
Just to clarify, what would exactly happen in
partial_fitin case of ensembles? Would that addn_estimatorsmore estimators, wheren_estimatorsis the parameter value from the constructor? (or could we change that value?)amueller commentedon Jan 30, 2013
Good question. I also thought about that ;) actually, you would want to change that, right? you could change that afterwards by
set_paramsbut that feels awkward :-/pprett commentedon Jan 30, 2013
sorry for joining the discussion so late.
I agree that we need such a functionality, however, I'm not sure if
fit_extendsis the best solution to the problem that @jwkvam describes. In order to do early stopping the user has to write some some code that basically repeatedly callsfit_extendsand then checks the CV error.I'd rather propose the
monitorfit parameter that we discussed in the past:est.fit(X, y, monitor=some_callable)wheresome_callablewill be called after each iteration and is passed the complete state of the estimator. The callable could also return a value whether or not the training should proceed.Using such an api one could implement not only early stopping but also custom reporting (e.g. interactive plotting the training vs. testing score) and snapshoting (all X iterations dump the estimator object and copy it to some location; this is great if you are running on EC2 spot instances or some other unreliable hardware ;-)
Even with such a
monitorAPI, however, I think there would be a need for an API to fit more estimators once the model has been fitted (i.e.fit_extends) - often one trains a model and does some introspection to find that its probably better to have run more iterations - existing estimators use thewarm_startparameter to implement such a functionality (e.g. seelinear_model.ElasticNet) - here is the docstring of the parameter::Personally, I'd prefer
fit_extends(orfit_more) overwarm_start- warm start is quite implicit - you have to::46 remaining items
glouppe commentedon Feb 13, 2013
After some thoughts, I think we should see the bigger picture here. In a near future, I would like to implement generic meta-ensembles that could combine any kind of estimators together. What I rather see is a "combination" mechanism that would take as input a list of (fitted) estimators and would produce a meta-estimator combining them all.
In practice, I think we can achieve that without adding any new function to our API. For example, one could simply pass such a list of fitted estimators to the constructor of the meta-ensemble.
In terms of API, one could (roughly) implement such ensembles in the following way:
a) Bagging:
base_estimator(optional),n_estimators(>=0), a listLof fitted estimators (optional).Lwithn_estimatorsnew instances ofbase_estimatorsfitted over (bootstrap copies of) the training samples. If no base estimator is given, then it is equivalent to combining the estimators inL.b) Stacking:
base_estimator(optional),n_estimators(>=0), a listLof fitted estimators (optional).Lwithn_estimatorsnew instances ofbase_estimatorsfitted of bootstrap samples, then refit a model over the predictions of the estimators.c) Forest:
base_estimator(optional),n_estimators(>=0), a listLof fitted estimators or a forest (optional).Lwithn_estimatorsnew instances ofbase_estimatorsfitted over the training samples. Here we could also check whether the estimators inLare forests or decision trees. Forests would be flattened in order to put all trees on the same level.Also, in such a framework, computation of an ensemble could easily be distributed over several machines: build your estimators; pickle them; then recombine them into one single meta-estimator. One could even wrap that interface into a MapReduce cluster, without digging into our implementation at all!
What do you think? I am aware this is only relevant to some kind of ensembles though. For instance, GBRT and AdaBoost are (in my opinion) more suited to either
warm_restartorpartial_fit.glouppe commentedon Feb 13, 2013
Just to be clear, to extend a forest, one would do something like:
amueller commentedon Feb 13, 2013
What is the motivation of that interface? I am totally with you in supporting more ensemble methods. I just feel it is quite awkward to have a different interface for GBRT and random forest. I don't really see the motivation for that.
If the main motivation is to distribute embarassingly parallel jobs, then I think we should attack this by implementing a more powerful parallelization. Doing it the way you described seems pretty manual and hacky.
Basically I feel your proposal just solves a very special case and leaves most cases unsolved.
glouppe commentedon Feb 13, 2013
Well ok... I just feel that extending boosted-like ensembles and average-like ensembles are quite different things.
amueller commentedon Feb 13, 2013
What is the use-case for your interface except parallelization? Or better: in what use cases do you need a different interface for boosted ensembles and bagging?
glouppe commentedon Feb 13, 2013
The use case is when you want to combine several estimators together. It is natural for average-like ensembles, but makes no sense in boosted ensembles. In that perspective, I see "extending an estimator" as "combining" it with more base estimators.
amueller commentedon Feb 13, 2013
So the setting is that you have trained some bagging estimators and want to combine them together, right?
In which setting do you want to do that except for parallelization? It is not so clear to me but maybe I'm overlooking something obvious.
glouppe commentedon Feb 13, 2013
In case of Stacking the estimators might be completely different (say to you want to merge forests with svms).
(Indirectly, this could also be used to implement subsampling strategies or for monitoring the fitting process.)
amueller commentedon Feb 13, 2013
I'm not sure I get the stacking example. I would have imagined that if we had a stacking interface, you could specify one estimator as the base estimator and another as the one on top.
glouppe commentedon Feb 13, 2013
As I see it, the point of stacking is to combine the predictions of estimators of different nature. The more diverse they are, often the better.
amueller commentedon Feb 13, 2013
Ok, so the base estimators would be different. But then we could also build this into the interface for stacking, right?
jwkvam commentedon Jan 9, 2014
Resolved with #2570
glouppe commentedon Jan 9, 2014
@jwkvam We recently agreed in #2570 to implement this feature using the
warm_startparameter. It is now implemented in GBRT. I'll try to update the forests with the same mechanism before the release.jwkvam commentedon Jan 9, 2014
@glouppe You're right, I forgot I had written this for any ensemble. But really I just wanted it for GBRT :) so in my haste, I decided this issue was resolved. If you like you can reopen it and close it when you are done, it doesn't matter to me.