Patric
Patric

Reputation: 21

Error: range must have an upper and lower bound. Inf and unknown() are acceptable values. Run rlang::last_error() to see where the error occurred

I want to tune the model using bayesian optimization by tidymodels but when defining the range of parameter values ​​there is a problem. Anyone can help me??

set.seed(123)
splitting <- initial_split(datafix, strata = Y)
data.training <- training(splitting)
data.testing <- testing(splitting)


xgboost_recipe <- recipe(formula = Y ~ ., data = data.training) %>% 
  step_novel(all_nominal(), -all_outcomes()) %>%
  step_dummy(all_nominal(), -all_outcomes(), one_hot = TRUE) %>% 
  step_zv(all_predictors()) 

xgboost_spec <- 
  boost_tree(trees = tune(), min_n = tune(), tree_depth = tune(), learn_rate = tune(), 
    loss_reduction = tune(), sample_size = tune(), mtry = tune()) %>% 
  set_mode("regression") %>% 
  set_engine("xgboost") 

params_grid <- parameters(trees(range= seq(100, 2000, by=100 )), min_n(range=seq(10,40, by=5)), tree_depth(range = c(6:10)), learn_rate(range = seq(0.1,1, by=0.1)), loss_reduction(range = seq(0.1,1, by=0.1)),
sample_prop(range = seq(0.1,1, by=0.2)), finalize(mtry(range=seq(1,8,by=1)), data.training))

Error: range must have an upper and lower bound. Inf and unknown() are acceptable values. Run rlang::last_error() to see where the error occurred.

Upvotes: 0

Views: 469

Answers (1)

EmilHvitfeldt
EmilHvitfeldt

Reputation: 3185

The error you are getting comes from the dials functions you called inside parameters(). These functions need a vector of length 2 to define the upper and lower of the range.

library(tidymodels)

data.training <- mtcars

params_grid <- parameters(
  trees(range = c(100, 2000)),
  min_n(range = c(10, 40)),
  tree_depth(range = c(6, 10)),
  learn_rate(range = c(0.1, 1)),
  loss_reduction(range = c(0.1, 1)),
  sample_prop(range = c(0.1, 1)),
  finalize(mtry(range = c(1, 8)), data.training)
)

params_grid
#> Collection of 7 parameters for tuning
#> 
#>      identifier           type    object
#>           trees          trees nparam[+]
#>           min_n          min_n nparam[+]
#>      tree_depth     tree_depth nparam[+]
#>      learn_rate     learn_rate nparam[+]
#>  loss_reduction loss_reduction nparam[+]
#>     sample_size    sample_size nparam[+]
#>            mtry           mtry nparam[+]

params_grid %>%
  grid_latin_hypercube(size = 25)
#> # A tibble: 25 × 7
#>    trees min_n tree_depth learn_rate loss_reduction sample_size  mtry
#>    <int> <int>      <int>      <dbl>          <dbl>       <dbl> <int>
#>  1  1456    17          9       3.50           1.67       0.566     1
#>  2   835    31          9       2.58           7.26       0.282     3
#>  3   647    25          8       2.87           2.48       0.788     7
#>  4   147    18          8       3.07           8.10       0.860     3
#>  5   396    15         10       1.31           3.69       0.219     2
#>  6  1984    11          7       7.59           4.61       0.688     2
#>  7  1560    21          7       3.27           3.10       0.781     3
#>  8  1532    29          7       8.47           2.34       0.625     7
#>  9  1330    20          7       3.72           9.59       0.529     6
#> 10  1029    16          6       9.95           3.90       0.415     4
#> # … with 15 more rows

Created on 2022-06-14 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions