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
Precompute square norms in Passive-Aggressive #1327
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.
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.
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.
Activity
mblondel commentedon Nov 24, 2012
116b54a seems to have improved the performance. Would still be nice pre-compute the square norms though.
amueller commentedon Nov 24, 2012
+1
This should always increase the speed of
fit, right? Don't see a reason not to.ogrisel commentedon Nov 24, 2012
I had the same impression.
mblondel commentedon Nov 24, 2012
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.
ogrisel commentedon Nov 24, 2012
Indeed, like Perceptron too.
kemaleren commentedon Apr 21, 2013
I have implemented precomputation in this branch. To benchmark it I tested the classifier on datasets with increasing numbers of features:
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.mblondel commentedon Apr 22, 2013
Thanks. Please do send us a PR :)
adrinjalali commentedon Apr 17, 2024
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.