基于mlrMBO/mlr3的物种分布模型贝叶斯调参问题求助
贝叶斯优化在物种分布模型调优中的问题
一、mlrMBO包调优问题
问题背景与矛盾
构建物种分布模型(SDMs),利用机器学习分析二元存在/缺失变量(1代表物种存在,0代表缺失)与环境变量的关系。使用mlrMBO开展贝叶斯超参数调优时,神经网络模型预测值范围仅为0.8-1(预期应为0-1),怀疑调优流程存在问题。
核心矛盾点:
- 优化函数中将响应变量
presence转为因子(明确为分类任务),但makeLearner却指定了回归模型regr.nnet - 尝试改用分类模型
classif.nnet作为代理模型时,触发报错:
Error in checkStuff(fun, design, learner, control) : mbo requires regression learner!
不清楚如何及在何处添加代码test <- makeClassifTask(data = train_data, target="presence")来对齐任务类型。
核心代码片段
## Create an optimization function create_optimization_function <- function(x){ ## Create a list containing the performance tables for each model replication performance_tables_by_replication <- list() ## Loop over the model replications for(replication_ID in 1:nb_replications){ ## Split the data into training and testing sets ... train_data <- train_test_split$train test_data <- train_test_split$test ## Create a list containing the performance tables for each data partition performance_tables_by_partition <- list() ## Loop over the data partitions for(partition_ID in 1:nb_partitions){ ## Run the model using the training data set.seed(1) train_data <- train_data %>% dplyr::mutate_at(response, as.factor) mod <- nnet::nnet(formula = model_formula, data = train_data, size = x["size"], rang = x["rang"], decay = x["decay"], MaxNWts = 1*(10*(length(c(nb_predictors)) + 1) + 10 + 1), trace = FALSE) ## Compute spatial predictions using the testing data ... ## Create a performance table ... ## Add the performance table into the list "performance_tables_by_partition" ... } ## Combine all performance tables from the list "performance_tables_by_partition" ... ## Add the performance tables into the list "performance_tables_by_replication" ... } ## Combine all performance tables from the list "performance_tables_by_replication" ## Compute the mean of performance metric values ## Retrieve the maximum value for the hyperparameter selection metric (TSS) return(max_TSS) } ## Build hyperparameter combinations hyperparameter_combinations <- ParamHelpers::makeParamSet(ParamHelpers::makeIntegerParam( "size", lower = 1, upper = 10), ParamHelpers::makeNumericParam("rang", lower = 0.1, upper = 1), ParamHelpers::makeNumericParam("decay", lower = 0, upper = 2)) ## Create an objective function objective_function <- smoof::makeSingleObjectiveFunction(name = "model_tuning", fn = create_optimization_function, par.set = hyperparameter_combinations, minimize = FALSE) ## Generate a random Latin hypercube design random_LH_design <- ParamHelpers::generateDesign(n = 100, par.set = ParamHelpers::getParamSet(objective_function), fun = randomLHS) ## Define the control parameters for Bayesian optimization MBO_control_parameters <- makeMBOControl() MBO_control_parameters <- setMBOControlTermination(MBO_control_parameters, iters = 10) MBO_control_parameters <- setMBOControlInfill(MBO_control_parameters, crit = makeMBOInfillCritMeanResponse()) ## Define the surrogate model surrogate_model <- mlr::makeLearner("regr.nnet", predict.type = "response") ## Start the optimization process MBO_results <- mlrMBO::mbo(fun = objective_function, design = random_LH_design, learner = surrogate_model, control = MBO_control_parameters)
二、mlr3包调优问题
问题背景与报错
改用mlr3包进行超参数调优,目标是调优多个机器学习模型的超参数以找到最优组合并生成空间预测。已对原始数据去除空间自相关,采用非空间嵌套重采样降低性能估计偏差,但调用auto_tuner时触发报错:
Error in UseMethod("as_search_space") : no applicable method for 'as_search_space' applied to an object of class c('Design', 'R6')
可复现代码
tsk_sonar = tsk("sonar") search_space <- paradox::ps(cost = paradox::p_dbl(lower = 1e-1, upper = 1e5), gamma = paradox::p_dbl(lower = 1e-1, upper = 1)) search_space <- paradox::generate_design_lhs(search_space, n = 100) at = mlr3tuning::auto_tuner(tuner = mlr3tuning::tnr("mbo"), learner = mlr3::lrn("classif.svm", predict_type = "prob", kernel = "radial", type = "C-classification"), resampling = mlr3::rsmp("cv", folds = 4), measure = mlr3::msr("classif.auc"), search_space = search_space, terminator = mlr3tuning::trm("evals", n_evals = 20)) rr = mlr3::resample(tsk_sonar, at, rsmp("cv", folds = 3), store_models = TRUE)
我的代码片段
## Build a spatio-temporal classification task sp_task = mlr3spatial::as_task_classif_st(x = dat, target = "presence", positive = "1", coordinate_names = c("decimalLongitude", "decimalLatitude"), crs = "+proj=longlat +datum=WGS84 +no_defs +type=crs") ## summary(sp_task) ## NB: dat is the data frame containing raw data ## Split the task into training and testing sets partitioned_data = partition(sp_task) ## print(partitioned_data) ## Define the arguments for the tuning process ## Define search spaces for each algorithm type search_space_nnet <- paradox::ps(size = p_int(lower = 1, upper = 10), rang = p_dbl(lower = 0.1, upper = 1), decay = p_dbl(lower = 0, upper = 2)) search_space_nnet <- paradox::generate_design_lhs(search_space_nnet, n = 100) ## search_space_nnet$print() ## Create learners learner_nnet = lrn("classif.nnet", predict_type = "prob") ## Define a measure measure <- msr("classif.auc") ## Create a terminator terminator <- trm("evals", n_evals = 20) ## trm("none"), trm("run_time", secs = 900) ## Define a tuner tuner <- tnr("mbo") ## Define a resampling used for the inner loop inner_resampling <- rsmp ("cv", folds = 2) ## Define a resampling used for the outer loop outer_resampling <- rsmp ("cv", folds = 2) ## Run the tuning process set.seed(1234) at = mlr3tuning::auto_tuner(tuner = tuner, learner = learner_nnet, resampling = inner_resampling, measure = measure, terminator = terminator, search_space = search_space_nnet) rr = resample(sp_task, at, outer_resampling, store_models = TRUE) ## extract_inner_tuning_archives(rr) ## extract_inner_tuning_results(rr) ## rr$aggregate()
内容的提问来源于stack exchange,提问作者Marine
相关产品推荐
相关产品推荐

