MLJ中XGBoost回归器调参报错:Unsupported range #2 求助
错误原因与解决办法
错误原因
报错Unsupported range #2指向第二个超参数max_depth的范围定义问题:你使用了原生Julia的2:10(UnitRange类型),但MLJTuning要求所有超参数范围必须是MLJ提供的Range类型对象,而非原生范围类型。
修正后的代码
将max_depth的范围定义替换为MLJ的range函数生成的对象即可,修改后的完整可运行脚本如下:
using MLJ, MLJXGBoostInterface, MLJTuning, Random, XGBoost # Load the XGBoost Regressor model XGBoostRegressor = @load XGBoostRegressor # Generate synthetic regression dataset X, y = MLJ.make_regression(200, 5; noise=0.3, rng=1234) train, test = partition(eachindex(y), 0.8, shuffle=true, rng=1234) # Define the base model with placeholders for hyperparameters model = XGBoostRegressor( objective="reg:squarederror", eta=0.1, # Learning rate max_depth=6, # Tree depth num_round=100, # Boosting rounds ) # Define the hyperparameter tuning ranges tuned_model = TunedModel( model=model, tuning=RandomSearch(), resampling=CV(nfolds=5, shuffle=true, rng=1234), range=[ :eta => range(0.01, 0.3, length=5), :max_depth => range(2, 10, step=1), # 替换为MLJ规范的range对象 :num_round => range(50, 200, length=4) ], measure=rms, n=10, acceleration=CPUThreads() ); # Wrap the model into a machine mach = machine(tuned_model, X, y); # Train the tuned model fit!(mach, rows=train);
补充说明
MLJTuning的range参数仅接受MLJ定义的范围对象,无论超参数是连续型还是离散型,都需要用range()函数构造。对于整数离散范围,通过step=1可以确保生成的是整数取值。
内容的提问来源于stack exchange,提问作者Andrea
相关产品推荐
相关产品推荐

