Closed lymanmark closed 4 years ago
I arrived at the same error through a different path (tune_grid
rather than fit_resamples
), and have encountered for both discrim_linear
and naive_Bayes
.
library(tidyverse)
library(tidymodels)
library(discrim)
library(doParallel)
model_spec <- naive_Bayes(smoothness = tune(), Laplace = tune()) %>%
set_engine("naivebayes") %>%
set_mode("classification")
grid = expand.grid(
Laplace = 0, # :5,
smoothness = seq(0, 5, by = 1)
)
data(iris)
iris_recipe <-
recipe(Species ~ ., data = iris)
set.seed(45)
repeated_cv <- vfold_cv(iris, v = 5, repeats = 5)
wflow <- workflow() %>%
add_model(model_spec) %>%
add_recipe(iris_recipe)
cl <- makePSOCKcluster(7)
registerDoParallel(cl)
tune_grid(
wflow,
repeated_cv,
grid = grid,
metrics = metric_set(accuracy, kap),
control = control_grid(save_pred = TRUE)
)
stopCluster(cl)
Temporary solution in tidymodels/tune#265 if you use a cluster object:
library(tidyverse)
library(tidymodels)
#> ── Attaching packages ────────────────────────────────────── tidymodels 0.1.1 ──
#> ✔ broom 0.7.0 ✔ recipes 0.1.13
#> ✔ dials 0.0.8.9000 ✔ rsample 0.0.7
#> ✔ infer 0.5.2 ✔ tune 0.1.1.9000
#> ✔ modeldata 0.0.2 ✔ workflows 0.1.3
#> ✔ parsnip 0.1.3 ✔ yardstick 0.0.7
#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
#> ✖ scales::discard() masks purrr::discard()
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ recipes::fixed() masks stringr::fixed()
#> ✖ dplyr::lag() masks stats::lag()
#> ✖ yardstick::spec() masks readr::spec()
#> ✖ recipes::step() masks stats::step()
library(discrim)
#>
#> Attaching package: 'discrim'
#> The following object is masked from 'package:dials':
#>
#> smoothness
library(doParallel)
#>
#> Attaching package: 'foreach'
#> The following objects are masked from 'package:purrr':
#>
#> accumulate, when
#> Loading required package: iterators
#> Loading required package: parallel
model_spec <- naive_Bayes(smoothness = tune(), Laplace = tune()) %>%
set_engine("naivebayes") %>%
set_mode("classification")
grid = expand.grid(
Laplace = 0, # :5,
smoothness = seq(0, 5, by = 1)
)
data(iris)
iris_recipe <-
recipe(Species ~ ., data = iris)
set.seed(45)
repeated_cv <- vfold_cv(iris, v = 5, repeats = 5)
wflow <- workflow() %>%
add_model(model_spec) %>%
add_recipe(iris_recipe)
cl <- makePSOCKcluster(7)
registerDoParallel(cl)
parallel::clusterEvalQ(cl, {library(discrim)})
#> [[1]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
#>
#> [[2]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
#>
#> [[3]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
#>
#> [[4]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
#>
#> [[5]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
#>
#> [[6]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
#>
#> [[7]]
#> [1] "discrim" "parsnip" "stats" "graphics" "grDevices" "utils"
#> [7] "datasets" "methods" "base"
tune_grid(
wflow,
repeated_cv,
grid = grid,
metrics = metric_set(accuracy, kap),
control = control_grid(save_pred = TRUE)
)
#> Warning: This tuning result has notes. Example notes on model fitting include:
#> model 1/6: Error in density.default(x, na.rm = TRUE, ...): 'bw' is not positive.
#> model 1/6: Error in density.default(x, na.rm = TRUE, ...): 'bw' is not positive.
#> model 1/6: Error in density.default(x, na.rm = TRUE, ...): 'bw' is not positive.
#> # Tuning results
#> # 5-fold cross-validation repeated 5 times
#> # A tibble: 25 x 6
#> splits id id2 .metrics .notes .predictions
#> <list> <chr> <chr> <list> <list> <list>
#> 1 <split [120/30… Repeat1 Fold1 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 2 <split [120/30… Repeat1 Fold2 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 3 <split [120/30… Repeat1 Fold3 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 4 <split [120/30… Repeat1 Fold4 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 5 <split [120/30… Repeat1 Fold5 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 6 <split [120/30… Repeat2 Fold1 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 7 <split [120/30… Repeat2 Fold2 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 8 <split [120/30… Repeat2 Fold3 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 9 <split [120/30… Repeat2 Fold4 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> 10 <split [120/30… Repeat2 Fold5 <tibble [10 × … <tibble [1 × … <tibble [150 × …
#> # … with 15 more rows
stopCluster(cl)
Created on 2020-08-20 by the reprex package (v0.3.0)
Confirming that this worked for me. Thanks for sharing!
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.
Trying to fit_resamples in parallel fails for discrim_linear.