usemethod中的错误(grid_latin_hypercube&quot):没有适用的方法' grid_latin_hypercube'应用于“ data.frame”类的对象。

发布于 2025-02-08 00:50:05 字数 2973 浏览 3 评论 0原文

我想使用tidymodels优化贝叶斯优化的XGBoost模型,但是在定义高参数值的范围时,存在问题。有人可以帮我吗?

我想要特定的超参数组合组合,因此我使用explent.grid()函数来定义超参数值

library(visdat)
library(tidyverse)
library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
#> Warning: package 'dials' was built under R version 4.1.3
#> Warning: package 'parsnip' was built under R version 4.1.3
library(patchwork)
library(readxl)
library(ranger)
library(reprex)
#> Warning: package 'reprex' was built under R version 4.1.3

data <- mtcars

data$cyl <- as.factor(data$cyl)
data$vs <- as.factor(data$vs)
data$am <- as.factor(data$am)
data$gear <- as.factor(data$gear)
data$carb <- as.factor(data$carb)

set.seed(123)
data.training <- data

xgboost_recipe <- 
  recipe(formula = mpg ~ ., 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") 

# define hyperparameter range

params <- expand.grid(
  trees = seq(500,1000,by=200),
  min_n = seq(10,40, by=10),
  tree_depth = seq(6L, 10L, by=2L),
  learn_rate = seq(0.1,1, by=0.5),
  loss_reduction = seq(0.1,1, by=0.5),
  sample_prop = seq(0.1,1, by=0.5), mtry=seq(2,7, by=1))

xgboost_workflow <- 
  workflow() %>% 
  add_recipe(xgboost_recipe) %>% 
  add_model(xgboost_spec) 

# resampling

set.seed(123)
resampling <- vfold_cv(data = data.training, v = 10, strata = mpg)
#> Warning: The number of observations in each quantile is below the recommended threshold of 20.
#> * Stratification will use 1 breaks instead.
#> Warning: Too little data to stratify.
#> * Resampling will be unstratified.


# Tune use bayesian optimization

doParallel::registerDoParallel()

set.seed(456) 
res <-
  tune_bayes(xgboost_workflow,
             iter = 6,
             resamples = resampling, 
             param_info = params, 
             metrics = metric_set(mae),
             control = control_bayes(verbose = TRUE, 
                                     save_pred = TRUE)
  )
#> ! There are 1728 tuning parameters and 5 grid points were requested. This is
#>   likely to cause numerical issues in the first few search iterations.
#> Error in UseMethod("grid_latin_hypercube"): no applicable method for 'grid_latin_hypercube' applied to an object of class "data.frame"

在2022-06-16创建的 reprex软件包(v2.0.1)

我有错误:

usemethod中的错误(“ grid_latin_hypercube”):没有适用于'grid_latin_hypercube'的适用方法

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

I want specific hyperparameter combinations so i use expand.grid() function to define hyperparameter values

library(visdat)
library(tidyverse)
library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
#> Warning: package 'dials' was built under R version 4.1.3
#> Warning: package 'parsnip' was built under R version 4.1.3
library(patchwork)
library(readxl)
library(ranger)
library(reprex)
#> Warning: package 'reprex' was built under R version 4.1.3

data <- mtcars

data$cyl <- as.factor(data$cyl)
data$vs <- as.factor(data$vs)
data$am <- as.factor(data$am)
data$gear <- as.factor(data$gear)
data$carb <- as.factor(data$carb)

set.seed(123)
data.training <- data

xgboost_recipe <- 
  recipe(formula = mpg ~ ., 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") 

# define hyperparameter range

params <- expand.grid(
  trees = seq(500,1000,by=200),
  min_n = seq(10,40, by=10),
  tree_depth = seq(6L, 10L, by=2L),
  learn_rate = seq(0.1,1, by=0.5),
  loss_reduction = seq(0.1,1, by=0.5),
  sample_prop = seq(0.1,1, by=0.5), mtry=seq(2,7, by=1))

xgboost_workflow <- 
  workflow() %>% 
  add_recipe(xgboost_recipe) %>% 
  add_model(xgboost_spec) 

# resampling

set.seed(123)
resampling <- vfold_cv(data = data.training, v = 10, strata = mpg)
#> Warning: The number of observations in each quantile is below the recommended threshold of 20.
#> * Stratification will use 1 breaks instead.
#> Warning: Too little data to stratify.
#> * Resampling will be unstratified.


# Tune use bayesian optimization

doParallel::registerDoParallel()

set.seed(456) 
res <-
  tune_bayes(xgboost_workflow,
             iter = 6,
             resamples = resampling, 
             param_info = params, 
             metrics = metric_set(mae),
             control = control_bayes(verbose = TRUE, 
                                     save_pred = TRUE)
  )
#> ! There are 1728 tuning parameters and 5 grid points were requested. This is
#>   likely to cause numerical issues in the first few search iterations.
#> Error in UseMethod("grid_latin_hypercube"): no applicable method for 'grid_latin_hypercube' applied to an object of class "data.frame"

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

I get error:

Error in UseMethod("grid_latin_hypercube"): no applicable method for 'grid_latin_hypercube' applied to an object of class "data.frame"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文