Skip to content

Precompute square norms in Passive-Aggressive #1327

Closed as not planned
Closed as not planned

Description

@mblondel

PassiveAggressiveClassifier is currently 5 times slower than Perceptron on the document classification example. This is because the current implementation recomputes the square norm (which is needed to compute the step size) for each example every time.

The solution would be to add a void precompute_norms(squared=True) and double get_norm(int i) to SequentialDataset. Doing so, I expect the performance to be inline with Perceptron.

CC @pprett

Activity

  1. mblondel commented on Nov 24, 2012

    @mblondel
    MemberAuthor

    116b54a seems to have improved the performance. Would still be nice pre-compute the square norms though.

  2. amueller commented on Nov 24, 2012

    @amueller
    Member

    +1
    This should always increase the speed of fit, right? Don't see a reason not to.

  3. ogrisel commented on Nov 24, 2012

    @ogrisel
    Member

    116b54a seems to have improved the performance.

    I had the same impression.

  4. mblondel commented on Nov 24, 2012

    @mblondel
    MemberAuthor

    Thinking about it, it makes sense. PA is similar to SGD + Hinge loss in that it updates only when the prediction is not good enough. Since sqnorm was incorrect, the step size at each iteration was incorrect, leading to too many updates.

  5. ogrisel commented on Nov 24, 2012

    @ogrisel
    Member

    Indeed, like Perceptron too.

  6. kemaleren commented on Apr 21, 2013

    @kemaleren
    Contributor

    I have implemented precomputation in this branch. To benchmark it I tested the classifier on datasets with increasing numbers of features:

    from sklearn.linear_model import PassiveAggressiveClassifier
    from sklearn.datasets import make_classification
    
    learner = PassiveAggressiveClassifier(n_iter=50, random_state=0)
    for n_feats in range(100, 501, 100):
        x, y = make_classification(1000, n_feats, random_state=0)
        learner.fit(x, y)
    

    The speedups for n_feats=[100, 200, 300, 400, 500] were [1.03, 1.18, 1.21, 1.23, 1.29]. If that improvement is good enough, I can clean up the code and issue a pull request.

  7. mblondel commented on Apr 22, 2013

    @mblondel
    MemberAuthor

    Thanks. Please do send us a PR :)

  8. adrinjalali commented on Apr 17, 2024

    @adrinjalali
    Member

    Since we're not sure if we want to keep the estimator in the first place, I don't think we'd be improving the implementation.

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

    EnhancementModeratemodule:linear_model

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions