Skip to content

ENH: optimize StringEncoder#1248

Merged
Vincent-Maladiere merged 7 commits intoskrub-data:mainfrom
GaelVaroquaux:string_encoder_optim
Feb 27, 2025
Merged

ENH: optimize StringEncoder#1248
Vincent-Maladiere merged 7 commits intoskrub-data:mainfrom
GaelVaroquaux:string_encoder_optim

Conversation

@GaelVaroquaux
Copy link
Copy Markdown
Member

For memory and speed:

  • Significant memory improvement (I did not measure)
  • 1.5 speedup on one task

For memory and speed
Force a doc build, and also fix some failing examples (still more to do)
@GaelVaroquaux
Copy link
Copy Markdown
Member Author

GaelVaroquaux commented Feb 26, 2025

FYI, the examples can be seen here: https://output.circle-artifacts.com/output/job/f25f287d-9bba-4153-977a-bbd5fbc2f3ca/artifacts/0/doc/auto_examples/02_text_with_string_encoders.html#sphx-glr-auto-examples-02-text-with-string-encoders-py
And I'm quite happy that with this PR StringEncoder is now faster than MinHashEncoder on this example ✌️
image

Copy link
Copy Markdown
Member

@jeromedockes jeromedockes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but it would be nice to cover the shape[1] == n_components case in a test, LMK if you don't have time now I can push a small commit for it

Comment thread skrub/_string_encoder.py
Comment on lines +145 to +146
elif X_out.shape[1] == self.n_components:
result = X_out.toarray()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the 'arpack' algorithm not like it when p == n_components? or is skipping the tsvd in that case an optimization? if the latter, I guess the case where the number of discovered ngrams is exactly equal to n_components might be too rare to warrant it? and we might want a test for that branch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess when n_components matches the number of dimensions of the vector, running the SVD doesn't make sense? We could coalesce this unlikely condition with the else statement below, though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's both, actually.

I think that needed this for the tests to pass. At least my first implementation hit a corner case in the tests (good tests!)

Comment thread skrub/_string_encoder.py Outdated
# Therefore, self.n_components_ below stores the resulting
# number of dimensions of result.
result = X_out[:, : self.n_components].toarray()
result = result.copy()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the copy() is that otherwise the reference to the slice would prevent X_out from being garbage collected; might be worth a short comment

@GaelVaroquaux
Copy link
Copy Markdown
Member Author

LGTM but it would be nice to cover the shape[1] == n_components case in a test, LMK if you don't have time now I can push a small commit for it

It would be helpful. Thanks!

Let's also wait for a systematic evaluation before merging

Copy link
Copy Markdown
Member

@jeromedockes jeromedockes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Copy Markdown
Member

@Vincent-Maladiere Vincent-Maladiere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@Vincent-Maladiere Vincent-Maladiere merged commit 97011bd into skrub-data:main Feb 27, 2025
@rcap107
Copy link
Copy Markdown
Member

rcap107 commented Mar 13, 2025

I ran some experiments with the new and old version of the StringEncoder to see how they would perform on different datasets:
image
image

It looks like the new version of the StringEncoder (the one that was merged here) is consistently faster (the margin depends on the dataset) and has comparable performance to that of the old version.

At the moment I have both the notebook I used to prepare the figures, and the code for running the experiments in my scratch folder, should I add the code for running the experiments to the benchmark folder? I was thinking of turning the notebook into a blog post with some additional analysis. Let me know!

@GaelVaroquaux
Copy link
Copy Markdown
Member Author

@rcap107 : super useful. Thanks!!!

rcap107 pushed a commit to rcap107/skrub that referenced this pull request Apr 2, 2025
Co-authored-by: Jerome Dockes <jerome@dockes.org>
rcap107 pushed a commit that referenced this pull request Apr 2, 2025
Co-authored-by: Jerome Dockes <jerome@dockes.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants