Repository navigation
Conversation
Contributor
Author
|
https://github.com/tidymodels /recipes/issues/1543 The good news is that the issue I found was definitely real and led to massive speedup, the bad news is that I was using an old version of recipes and this was implemented in the newest version. So closing this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
perf(fitting): scale features in one step instead of column by column
What changes
R/recipe-scale.R(new).stepScale(), a recipe step with the same statistics asrecipes::step_scale()andstep_normalize(). It applies them to all columns at once.R/fit-models.R.buildRecipe()uses.stepScale()in place ofstep_scale()(sparse) andstep_normalize()(dense). Two lines.NAMESPACEprep,bakeandprintmethods (from roxygen).tests/testthat/test-recipe-scale.R(new).stepScale()bakesidentical()tostep_scale()(sparse) andstep_normalize()(dense), with the same RNG state afterwards.recipes scaling steps is really slow on very wide tables
recipes' scaling steps assign each scaled column back into the tibble one at a time. Every
assignment costs time proportional to the number of columns. This is extremely problematic for struct in particular given its size. The step is quadratic:
step_scaleprep + bakesdarithmetic itselfrecipes' step_scale() and step_normalize() write each scaled column back into the tibble one at a time, and each write copies and re-checks the tibble's full list of columns. With n columns that's n writes of cost ~n each, so time grows with n²; .stepScale() writes all columns back in one assignment.
A task runs the recipe 6 times (5 folds plus the final fit). So a struct task spent minutes
scaling
Results don't change
The step computes exactly what recipes computes:
sqrt(stats::var(x));.Machine$double.epsis set to 1;colMeans();It draws the same random step ID as
step_scale()/step_normalize(), so the folds drawnafter the recipe are unchanged.
Checked against
amrml-rewriteidentical(), same RNG state afterwardstasks,performance,importanceandpredictionsallidentical()identical()(exceptdate)NOT_CRAN=truehpc-parallelization), NAMESPACE regeneratedSpeed
Each pair ran side by side on the same machine:
I did not do a full benchmark but I am hoping for pretty meaningful speedup here for larger bugs.
Merging with the stack
The R and test changes apply cleanly to #53 (
8fdef4d), #57 and #59. There will be merging conflicts with NAMESPACE across the stack thoughOther speedup possibilties that are less easy and can be done if this isn't enough
These are more speedups for wide tables. Each one changes results or is a larger refactor:
among 31,643 columns. This changes elastic-net coefficients and importance attribution.