-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lightgbm tuned with parallel processing unable to predict #52
Comments
Thanks for the issue! Do you see this issue when using LightGBM without the bonsai wrapper? Does the issue persist if you use a different parallel backend? Some notes on parellel processing with tune here, though you seem to have applied those notes. :) |
I have tried registerDoParallel and registerDoFuture in my reprex above, and both resulted in the same error. Are there any other parallel backends I should try? And no, I have not tried LightGBM without bonsai wrapper. Taking a look at related open issues in its GitHub though, I don't see this issue being raised. |
Thanks for the additional info! This one was fun to track down. Just spent a bit more time with this. It makes sense that these backends would lead to downstream failures with LightGBM, as they, by default, spin up new R sessions with each core, fit the models there, and then pass the models back to the calling session. LightGBM objects can't be passed between R sessions without proper serialization (see our work on bundle and This should be fine, as we don't need any of the resampled fits to have access to that instance anymore once we have library(tidymodels)
library(bonsai)
library(palmerpenguins)
#>
#> Attaching package: 'palmerpenguins'
#> The following object is masked from 'package:modeldata':
#>
#> penguins
library(doParallel)
#> Loading required package: foreach
#>
#> Attaching package: 'foreach'
#> The following objects are masked from 'package:purrr':
#>
#> accumulate, when
#> Loading required package: iterators
#> Loading required package: parallel
library(parallel)
split <- penguins |>
initial_split(strata = species)
penguins_train <- training(split)
penguins_test <- testing(split)
folds <- vfold_cv(penguins_train, strata = species, 3)
recipe_basic <- penguins_train |>
recipe(species ~ .)
lightgbm_spec <- boost_tree(trees = tune()) |>
set_engine(
"lightgbm",
objective = "multiclass",
metric = "multi_error",
num_class = !!length(unique(penguins_train$species))
) |>
set_mode("classification")
lightgbm_wflow <- workflow(preprocessor = recipe_basic,
spec = lightgbm_spec)
all_cores <- detectCores(logical = FALSE)
cl <- makePSOCKcluster(all_cores - 1)
registerDoParallel(cl)
training_grid_results <- lightgbm_wflow |>
tune_grid(resamples = folds,
grid = 5)
ctrl <- control_last_fit()
ctrl$allow_par <- FALSE
last_fit <- lightgbm_wflow |>
finalize_workflow(select_best(training_grid_results, "roc_auc")) |>
last_fit(split, control = ctrl)
last_fit |>
extract_workflow() |>
predict(head(penguins_test))
#> # A tibble: 6 × 1
#> .pred_class
#> <fct>
#> 1 Adelie
#> 2 Adelie
#> 3 Adelie
#> 4 Adelie
#> 5 Adelie
#> 6 Adelie
last_fit |>
extract_fit_engine() |>
lightgbm::lgb.importance() |>
lightgbm::lgb.plot.importance() Created on 2022-09-14 by the reprex package (v2.0.1) Unfortunately, So, that's a patch for now, though I think we ought to be able to handle this without that workaround. I'll open a linked issue here in a moment! |
Going to close in favor of tidymodels/tune#539. Thanks for documenting this! |
This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue. |
Lightgbm model (after using
last_fit()
) isn't able to predict when parallel processing is used.I tried using both
registerDoParallel
andregisterDoFuture
and both gave me the same error.I tried it on both multiclass and binary classification problems, but both cases with parallel processing still gave me the error.
Error messages are mentioned below in the reprexes:
Multiclass tuning without parallel processing
No issues. Able to predict, and extract feature importance.
Created on 2022-09-07 with reprex v2.0.2
Session info
Multiclass tuning with parallel processing (registerDoParallel)
Unable to predict nor extract feature importance.
Created on 2022-09-07 with reprex v2.0.2
Session info
Multiclass tuning with parallel processing (registerDoFuture)
Unable to predict nor extract feature importance.
Created on 2022-09-07 with reprex v2.0.2
Session info
Binary class tuning with parallel processing (registerDoFuture)
Unable to predict nor extract feature importance.
Created on 2022-09-07 with reprex v2.0.2
Session info
The text was updated successfully, but these errors were encountered: