tidymodels / tune

Tools for tidy parameter tuning
https://tune.tidymodels.org
Other
276 stars 42 forks source link

glmnet fails with one penalty value #28

Open topepo opened 5 years ago

topepo commented 5 years ago
library(tidymodels)
#> ── Attaching packages ─────────────────────────────────────────────────────────────── tidymodels 0.0.2 ──
#> ✔ broom     0.5.2          ✔ purrr     0.3.2     
#> ✔ dials     0.0.2.9001     ✔ recipes   0.1.6.9000
#> ✔ dplyr     0.8.3          ✔ rsample   0.0.5     
#> ✔ ggplot2   3.2.1          ✔ tibble    2.1.3     
#> ✔ infer     0.4.0.1        ✔ yardstick 0.0.3.9000
#> ✔ parsnip   0.0.3.9001
#> ── Conflicts ────────────────────────────────────────────────────────────────── tidymodels_conflicts() ──
#> ✖ purrr::discard()  masks scales::discard()
#> ✖ dplyr::filter()   masks stats::filter()
#> ✖ dplyr::lag()      masks stats::lag()
#> ✖ ggplot2::margin() masks dials::margin()
#> ✖ dials::offset()   masks stats::offset()
#> ✖ recipes::step()   masks stats::step()
library(tune)
library(workflows)
#> 
#> Attaching package: 'workflows'
#> The following object is masked from 'package:purrr':
#> 
#>     has_element
library(glmnet)
#> Loading required package: Matrix
#> 
#> Attaching package: 'Matrix'
#> The following object is masked from 'package:tidyr':
#> 
#>     expand
#> Loading required package: foreach
#> 
#> Attaching package: 'foreach'
#> The following objects are masked from 'package:purrr':
#> 
#>     accumulate, when
#> Loaded glmnet 2.0-16
library(dplyr)

rec_no_tune_1 <-
  recipe(mpg ~ ., data = mtcars) %>%
  step_normalize(all_predictors())

glmn_mod <- linear_reg(mixture = tune(), penalty = .1) %>% set_engine("glmnet")

set.seed(363)
mt_folds <- vfold_cv(mtcars, v = 5)

workflow() %>%
  add_recipe(rec_no_tune_1) %>%
  add_model(glmn_mod) %>%
  tune_grid(mt_folds)
#> 
#> Attaching package: 'crayon'
#> The following object is masked from 'package:ggplot2':
#> 
#>     %+%
#> 
#> Attaching package: 'rlang'
#> The following object is masked from 'package:crayon':
#> 
#>     chr
#> The following objects are masked from 'package:purrr':
#> 
#>     %@%, as_function, flatten, flatten_chr, flatten_dbl,
#>     flatten_int, flatten_lgl, flatten_raw, invoke, list_along,
#>     modify, prepend, splice
#> `by` can't contain join column `id` which is missing from RHS

Created on 2019-09-04 by the reprex package (v0.2.1)

EmilHvitfeldt commented 2 years ago

We are catching this error differently now using the no_penalty() function. The error pops up in submod_and_others() if the no_penalty() check is ignored.

library(tidymodels)

rec_no_tune_1 <-
  recipe(mpg ~ ., data = mtcars) %>%
  step_normalize(all_predictors())

glmn_mod <- linear_reg(mixture = tune(), penalty = .1) %>% set_engine("glmnet")

set.seed(363)
mt_folds <- vfold_cv(mtcars, v = 5)

workflow() %>%
  add_recipe(rec_no_tune_1) %>%
  add_model(glmn_mod) %>%
  tune_grid(mt_folds)
#> Error: At least one penalty value is required for glmnet.

Created on 2022-02-18 by the reprex package (v2.0.1)