我正在使用RandomizedSearchcv进行超参数优化。当我运行模型时,它会显示每个模型培训的分数。问题是,它训练了10多个模型,而实际上我希望它通过将n_iters指定为10来训练10个模型。为什么呢?我该怎么做才能把总数限制在10?
这是我的密码
from catboost import CatBoostRegressor
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
params = {
'iterations': randint(100, 1000),
'depth': randint(3, 10),
'learning_rate': [0.01, 0.02, 0.03, 0.04, 0.05],
'l2_leaf_reg': randint(1, 10),
'border_count': randint(32, 255),
'bagging_temperature': [0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
}
model = CatBoostRegressor(loss_function='RMSE', od_type='Iter', cat_features=[0, 1, 2, 3, 4], task_type='GPU')
search = RandomizedSearchCV(model, param_distributions=params, n_iter=10, scoring='neg_root_mean_squared_error')
# Fit the model
search.fit(X_train, y_train, eval_set=(X_val, y_val), plot=True)发布于 2023-04-01 15:28:26
n_iter参数在RandomizedSearchCV中指定要运行的用于超参数优化的迭代次数。然而,这并不一定限制将接受培训的模型的总数。
原因是RandomizedSearchCV在超参数空间上执行随机搜索。这意味着它从每次迭代的参数分布中随机抽取样本。因此,有可能在多次迭代中重复某些超参数组合,从而导致10多个模型被训练。
若要将培训的模型总数限制为10,可以在max_evals中设置CatBoostRegressor参数。此参数限制了超参数优化的最大迭代次数。您可以将其设置为10,以确保只对10种模型进行培训:
model = CatBoostRegressor(loss_function='RMSE', od_type='Iter',
cat_features=[0, 1, 2, 3, 4],
task_type='GPU', max_evals=10)
search = RandomizedSearchCV(model, param_distributions=params,
n_iter=10,scoring='neg_root_mean_squared_error')
search.fit(X_train, y_train, eval_set=(X_val, y_val), plot=True)#另一种方法是:
如果要将模型的总数限制为10,则可以设置n._CatBoostRegressor中的估值器参数设置为1,在RandomizedSearchCV中将cv参数设置为10 (或小于或等于10的任何其他数字)。这将确保每个超参数组合只适合一个模型,并且总共有10个模型适合.
model = CatBoostRegressor(
n_estimators=1, # Only fit one model per hyperparameter combination
loss_function='RMSE', od_type='Iter',
cat_features=[0, 1, 2, 3, 4], task_type='GPU'
)
search = RandomizedSearchCV(
model, param_distributions=params, n_iter=10,
scoring='neg_root_mean_squared_error', cv=10 # Limit to 10 folds
)
# Fit the model
search.fit(X_train, y_train, eval_set=(X_val, y_val), plot=True)https://datascience.stackexchange.com/questions/120612
复制相似问题