You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

mlr3两种嵌套调参方法结果差异及置信区间求解

在mlr3嵌套调参场景下计算模型性能置信区间

针对因随机种子导致性能波动、需要计算置信区间的问题,核心解决方案是通过重复多次嵌套调参实验获取多组独立性能数据,再基于统计方法计算置信区间。以下是具体实现步骤:

1. 采用重复交叉验证作为外层重采样

不管使用auto_tuner + benchmark_grid还是tune_nested,都将外层重采样设置为重复交叉验证(Repeated CV),这样能直接得到多组独立的性能评估结果(每组对应一次重复的外层验证)。

# 定义20次重复的5折交叉验证(可根据资源调整重复次数)
outer_resampling = rsmp("repeated_cv", repeats = 20, folds = 5)
outer_resampling$instantiate(your_task) # 绑定任务,固定外层划分(可选,用于复现)

2. 执行嵌套调参并收集性能数据

方法一:auto_tuner + benchmark_grid

# 定义内层调参配置
inner_resampling = rsmp("cv", folds = 3)
tuner = tnr("grid_search", resolution = 10) # 替换为你的调参器
measure = msr("classif.acc") # 替换为你的评估指标(如regr.mse)

# 创建AutoTuner
auto_tuner = auto_tuner(
  tuner = tuner,
  learner = lrn("classif.rpart"), # 替换为你的目标模型
  resampling = inner_resampling,
  measure = measure
)

# 构建并运行基准实验
bench_grid = benchmark_grid(
  tasks = your_task,
  learners = list(auto_tuner),
  resamplings = outer_resampling
)
bmr_result = benchmark(bench_grid, store_models = FALSE)

# 提取所有重复实验的性能值
perf_values = bmr_result$aggregate(measure)[[measure$id]]

方法二:tune_nested

# 直接执行嵌套调参
tune_result = tune_nested(
  learner = lrn("classif.rpart"), # 替换为你的目标模型
  task = your_task,
  inner_resampling = inner_resampling,
  outer_resampling = outer_resampling,
  tuner = tuner,
  measure = measure
)

# 提取所有重复实验的性能值
perf_values = tune_result$score()[[measure$id]]

3. 计算置信区间

基于收集到的多组性能数据,使用t分布计算95%置信区间(最常用的置信水平):

library(stats)

# 计算核心统计量
mean_perf = mean(perf_values)
std_error = sd(perf_values) / sqrt(length(perf_values))
confidence_interval = t.test(perf_values)$conf.int

# 输出结果
cat(sprintf("平均性能: %.4f\n", mean_perf))
cat(sprintf("95%%置信区间: [%.4f, %.4f]\n", confidence_interval[1], confidence_interval[2]))

关键注意事项

  • 种子控制:若需复现实验,运行前设置全局种子set.seed(123),同时可给内层调参、外层重采样的组件单独指定seed参数,确保两种嵌套调参方法的结果一致。
  • 重复次数:建议重复次数不少于20次,资源允许的话50次以上能得到更稳定的置信区间估计。
  • 指标适配:上述流程适用于分类、回归等各类任务,只需替换对应的评估指标measure即可。

内容的提问来源于stack exchange,提问作者NDe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 14:05:32