Skip to content

partial_fit support for Pipeline #3299

Closed

Description

@linkenneth

Would it be possible to support partial_fit for Pipelines in which components do support partial fits?

Activity

  1. jnothman commented on Jun 19, 2014

    @jnothman
    Member

    What does partial_fit mean for a transformer? Do we just re-fit_transform
    with each new bit of training data? Or do we fit_transform the first time,
    and transform the rest? I assume in most cases where this is useful, the
    first bit of training data is adequate to fit the transformer, or else the
    transformer has a null fit.

    There may be benefit to saying "roll your own" and making the user think
    about it, rather than implement arbitrary semantics.

    On 19 June 2014 11:12, Kenneth Lin [email protected] wrote:

    Would it be possible to support partial_fit for Pipelines in which
    components do support partial fits?

    —
    Reply to this email directly or view it on GitHub
    #3299.

  2. linkenneth commented on Jun 19, 2014

    @linkenneth
    Author

    What about if we provide a way to assign functions as the "fit" or "transform" functions in each step? Or would it be easier to just implement custom classes every time that would be necessary?

  3. jnothman commented on Jun 19, 2014

    @jnothman
    Member

    The pipeline supports two main functions (as well as reducing a small
    amount of repeated code that one would have if they used a mere list):

    • to ensure correct use of transformers: only acquire sufficient statistics
      from the training data, then apply to test
    • to apply in a cross-validation context, particularly with grid
      search-assigned parameters (note that the pipeline is useful for this
      application even when the transformer doesn't fit any model)

    It is unclear how to apply online training to either of these ends, given
    that scikit-learn doesn't directly support cross-validation, let alone grid
    search, in an online setting. So yes, I think ad hoc implementation for
    particular purposes might make sense unless one can identify substantial
    regularity in usage patterns.

    On 19 June 2014 11:46, Kenneth Lin [email protected] wrote:

    What about if we provide a way to assign functions as the "fit" or
    "transform" functions in each step? Or would it be easier to just implement
    custom classes every time that would be necessary?

    —
    Reply to this email directly or view it on GitHub
    #3299 (comment)
    .

  4. linkenneth commented on Jun 19, 2014

    @linkenneth
    Author

    I see. Thanks a lot then! Marking as closed then

  5. miraculixx commented on Mar 17, 2018

    @miraculixx

    Here's what worked for me:

    import numpy as np
    from sklearn.pipeline import Pipeline
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import MiniBatchKMeans
    
    class OnlinePipeline(Pipeline):
        def partial_fit(self, X, y=None):
            for i, step in enumerate(self.steps):
                name, est = step
                est.partial_fit(X, y)
                if i < len(self.steps) - 1:
                   X = est.transform(X)
            return self
        
    piple = OnlinePipeline([
        ('scale', StandardScaler()),
        ('clf', MiniBatchKMeans()),
    ])
    
    X = np.random.beta(.1, .2, (100, 20))
    
    piple.partial_fit(X[0:50])
    piple.partial_fit(X[50:])
    piple.predict(X)
  6. jnothman commented on Mar 18, 2018

    @jnothman
    Member

    That will work for some needs. It depends entirely on how your partially trained components fit together.

  7. stsievert commented on Jun 14, 2018

    @stsievert
    Contributor

    @miraculixx in your example, all the est.partial_fit calls are "embarrassingly parallel" or completely independent. Shouldn't there be some interdependence between partial_fit calls? i.e.,

    class OnlinePipeline(Pipeline):
        def partial_fit(self, X, y=None):
            args = [X, y]
            for name, est in self.steps:
                est.partial_fit(*args)
                X_transformed = est.transform(args[0])
                args = [X_transformed, y]
            return self
  8. miraculixx commented on Jun 16, 2018

    @miraculixx

    @stsievert indeed yes, I noticed that in further testing and changed accordingly in my code base but unfortunately did not post here. Thanks for pointing this out. Updated above

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions