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

在mlr3中调优xgboost时,如何将early_stopping_rounds设为nrounds的10%?

解决mlr3中xgboost调优时early_stopping_rounds依赖nrounds的问题

要调优surv.xgboost学习器,让early_stopping_rounds自动设为nrounds的10%,但直接给early_stopping_rounds传to_tune会报错,报错信息显示参数生成的值不兼容。

错误原因

你原来的写法错误地给单个参数early_stopping_rounds绑定了独立的调优参数集,而mlr3中处理参数间依赖关系,应该把转换逻辑放在整个学习器的参数集的trafo函数里,而不是单独给该参数加to_tune。

解决方案代码

library(mlr3verse)

# 创建surv.xgboost学习器
learner = lrn("surv.xgboost")

# 给学习器的参数集添加转换逻辑:根据nrounds计算early_stopping_rounds
learner$param_set$trafo = function(x, param_set) {
  if (!is.null(x$nrounds)) {
    x$early_stopping_rounds = ceiling(0.1 * x$nrounds)
  }
  x
}

# 仅设置nrounds为调优参数,early_stopping_rounds会自动计算
learner$param_set$values = list(
  nrounds = to_tune(50, 5000)
)

# 验证参数生成效果
design = generate_design_random(learner$param_set$search_space(), 3)
print(design$transpose())

说明

这个写法的核心是利用学习器参数集的trafo函数处理参数依赖:当调优过程生成nrounds的取值后,自动计算出对应的early_stopping_rounds(nrounds的10%向上取整),不需要给early_stopping_rounds单独设置调优规则。

你之前测试的独立参数集trafo逻辑是有效的,但放到学习器参数配置时,不能直接给单个参数嵌套to_tune,必须通过整个参数集的trafo来实现参数间的动态计算。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 12:57:25