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

使用BayesSearchCV优化MLPRegressor超参数遇两类错误求助

解决MLPRegressor贝叶斯优化中的两个参数错误

错误1:Invalid parameter regressor for estimator MLPRegressor

问题原因

  1. 参数前缀冗余:直接将MLPRegressor作为BayesSearchCV的估计器时,参数不需要加regressor__前缀,该前缀仅适用于Pipeline或嵌套模型的参数声明。
  2. 参数混淆:MLPRegressor的learning_rate参数用于控制学习率策略(取值为constant/invscaling/adaptive),而学习率的具体数值对应的参数是learning_rate_init,你之前的代码错误地将数值型学习率赋值给了learning_rate参数。

修正代码

from skopt import BayesSearchCV
from sklearn.neural_network import MLPRegressor

n_iter = 100
mlpr = MLPRegressor()

param_grid = {
    "activation": ["logistic", "tanh", "relu"],
    "solver": ["lbfgs", "sgd", "adam"],
    # 修正参数名:移除冗余前缀,改用对应数值型学习率的参数
    "learning_rate_init": (0.0001, 0.01)  # 贝叶斯优化支持区间形式,自动在范围内采样
}

reg_bay = BayesSearchCV(estimator=mlpr,
                        search_spaces=param_grid,
                        n_iter=n_iter,
                        cv=5,
                        n_jobs=8,
                        scoring='neg_mean_squared_error',
                        random_state=123)

model_bay = reg_bay.fit(X_train, Y_train)

错误2:Not all points are within the bounds of the space

问题原因

BayesSearchCV对分类参数(离散选项)的格式要求严格,直接传入列表在部分版本中会被误判为连续空间,导致搜索时生成的点超出边界。此外你代码中存在变量名笔误(fit时用了X_tr, y_tr,但实际训练集变量是X_train, Y_train)。

修正后的Pipeline版本代码

from skopt import BayesSearchCV
from sklearn.neural_network import MLPRegressor
from sklearn.pipeline import Pipeline
from skopt.space import Categorical

n_iter = 100

pipe = Pipeline(steps=[
    ('mlpr', MLPRegressor())  # 步骤名建议用小写,保持命名规范
])

param_grid = {
    # 用Categorical显式声明分类参数空间
    "mlpr__activation": Categorical(["logistic", "tanh", "relu"]),
    "mlpr__solver": Categorical(["lbfgs", "sgd", "adam"]),
    "mlpr__learning_rate": Categorical(["constant", "invscaling", "adaptive"]),
    # 若需优化学习率数值,可添加该参数
    "mlpr__learning_rate_init": (0.0001, 0.01)
}

reg_bay = BayesSearchCV(estimator=pipe,
                        search_spaces=param_grid,
                        n_iter=n_iter,
                        cv=5,
                        n_jobs=8,
                        scoring='neg_mean_squared_error',
                        random_state=123)

# 修正变量名,与训练集变量保持一致
model_bay = reg_bay.fit(X_train, Y_train)

额外说明

  • 数值型参数直接传入(最小值, 最大值)区间即可,贝叶斯优化会自动在区间内采样搜索;
  • 分类参数必须用Categorical包装,确保BayesSearchCV正确识别为离散参数空间;
  • 使用Pipeline时,参数名格式为步骤名__参数名,需与Pipeline中定义的步骤名完全匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 23:40:42