Reputation: 11
I was trying to use scikit-learn package 0.24.2 with python-3.8.5 to do a grid search but I get the following error:
Error : Invalid parameter activation for estimator Pipeline(steps=[('scaler', StandardScaler()), ('MLPRegressor', MLPRegressor())]). Check the list of available parameters with
estimator.get_params().keys()
.
My code is the following:
mpl_pipe = Pipeline(steps=[
('scaler', StandardScaler()),
('MLPRegressor', MLPRegressor())
])
parameters = {
'hidden_layer_sizes': [(900,700,500,300,150,) ,(700,500,300,150, ),(500,300,150, ),(300,150, )],
'max_iter': [20000],
'activation' : ['identity', 'logistic', 'tanh', 'relu'],
'solver' : ['lbfgs', 'sgd', 'adam']
}
mlp_grid = GridSearchCV(mpl_pipe,
parameters,
cv = 5,
n_jobs = -1,
verbose=True)
mlp_grid.fit(x_train, y_train)
print(mlp_grid.best_score_)
print(mlp_grid.best_params_)
Upvotes: 1
Views: 1222
Reputation: 8663
The pipeline parameters can be accessed as pipeline step name + __ + parameter name
, which means that you will need to add MLPRegressor__
before each of the parameter names in your search grid.
from sklearn.pipeline import Pipeline
from sklearn.datasets import make_regression
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import GridSearchCV
X, y = make_regression(n_samples=200, n_targets=1, n_features=5, n_informative=5)
pipe = Pipeline(steps=[
('scaler', StandardScaler()),
('MLPRegressor', MLPRegressor())
])
param_grid = {
'MLPRegressor__hidden_layer_sizes': [(900, 700, 500, 300, 150,), (700, 500, 300, 150, ), (500, 300, 150,), (300, 150,)],
'MLPRegressor__max_iter': [1000],
'MLPRegressor__activation': ['identity', 'tanh', 'relu'],
'MLPRegressor__solver': ['adam']
}
search = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1, verbose=True)
search.fit(X, y)
print(search.best_score_)
print(search.best_params_)
Upvotes: 2
Reputation: 640
I think the problem is that the parameters e.g. activation
are only for MLPRegressor()
.
You may specify which estimator the parameters are for, with say {'MLPRegressor__activation': ['identity', 'logistic', 'tanh', 'relu'], ...}
Reference: https://scikit-learn.org/stable/modules/compose.html#nested-parameters
Upvotes: 1