Skip to content

Fitting additional estimators for ensemble methods #1585

Closed

Description

@jwkvam

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

def fit_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.

Activity

  1. amueller commented on Jan 23, 2013

    @amueller
    Member

    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 ;)

  2. amueller commented on Jan 23, 2013

    @amueller
    Member

    Btw, I wouldn't hash X and y . I don't see a reason to force the user to provide the same input data.

  3. reopened this on Jan 25, 2013
  4. jwkvam commented on Jan 25, 2013

    @jwkvam
    ContributorAuthor

    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

    def fit(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.

  5. glouppe commented on Jan 25, 2013

    @glouppe
    Contributor

    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
    
  6. glouppe commented on Jan 25, 2013

    @glouppe
    Contributor

    Note that this only work for RandomForest and ExtraTrees. The same trick cannot be used with Gradient Boosting.

  7. amueller commented on Jan 26, 2013

    @amueller
    Member

    See #1626. Would early stopping be an acceptable solution to you?

  8. jwkvam commented on Jan 29, 2013

    @jwkvam
    ContributorAuthor

    @amueller I share the same opinion as @glouppe here #1626 (comment). I like early stopping but it doesn't resolve this in my opinion.

  9. amueller commented on Jan 29, 2013

    @amueller
    Member

    Ok. Then we should look for a solution that allows for early stopping and adding additional estimators.

  10. amueller commented on Jan 29, 2013

    @amueller
    Member

    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.

  11. GaelVaroquaux commented on Jan 29, 2013

    @GaelVaroquaux
    Member

    Thinking about it a bit more, I think the partial_fit method would be the right
    interface.

    I like this suggestion. What do other people think?

  12. glouppe commented on Jan 30, 2013

    @glouppe
    Contributor

    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?)

  13. amueller commented on Jan 30, 2013

    @amueller
    Member

    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 :-/

  14. pprett commented on Jan 30, 2013

    @pprett
    Member

    sorry for joining the discussion so late.

    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)
    
  15. 46 remaining items

  16. glouppe commented on Feb 13, 2013

    @glouppe
    Contributor

    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.

  17. glouppe commented on Feb 13, 2013

    @glouppe
    Contributor

    Just to be clear, to extend a forest, one would do something like:

    forest = RandomForestClassifier(n_estimators=100)
    forest.fit()
    forest_extended = RandomForestClassifier(n_estimators=100, L=forest)
    forest_extended.fit() # now counts 200 trees
    
  18. amueller commented on Feb 13, 2013

    @amueller
    Member

    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.

  19. glouppe commented on Feb 13, 2013

    @glouppe
    Contributor

    Well ok... I just feel that extending boosted-like ensembles and average-like ensembles are quite different things.

  20. amueller commented on Feb 13, 2013

    @amueller
    Member

    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?

  21. glouppe commented on Feb 13, 2013

    @glouppe
    Contributor

    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.

  22. amueller commented on Feb 13, 2013

    @amueller
    Member

    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.

  23. glouppe commented on Feb 13, 2013

    @glouppe
    Contributor

    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.)

  24. amueller commented on Feb 13, 2013

    @amueller
    Member

    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.

  25. glouppe commented on Feb 13, 2013

    @glouppe
    Contributor

    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.

  26. amueller commented on Feb 13, 2013

    @amueller
    Member

    Ok, so the base estimators would be different. But then we could also build this into the interface for stacking, right?

  27. jwkvam commented on Jan 9, 2014

    @jwkvam
    ContributorAuthor

    Resolved with #2570

  28. glouppe commented on Jan 9, 2014

    @glouppe
    Contributor

    @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.

  29. jwkvam commented on Jan 9, 2014

    @jwkvam
    ContributorAuthor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions