带有流水线 KerasClassifier 的 sklearn RandomizedSearchCV

sklearn RandomizedSearchCV with Pipelined KerasClassifier

我正在使用 sklearn 在 Keras 模型上执行超参数调整优化任务。我正在尝试优化管道中的 KerasClassifiers ... 代码如下:

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score, StratifiedKFold,RandomizedSearchCV
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.pipeline import Pipeline

my_seed=7

dataframe = pd.read_csv("z:/sonar.all-data.txt", header=None)

dataset = dataframe.values
# split into input and output variables
X = dataset[:,:60].astype(float)
Y = dataset[:,60]

encoder = LabelEncoder()
Y_encoded=encoder.fit_transform(Y)
myScaler = StandardScaler()
X_scaled = myScaler.fit_transform(X)

def create_keras_model(hidden=60):
    model = Sequential()
    model.add(Dense(units=hidden, input_dim=60, kernel_initializer="normal", activation="relu"))
    model.add(Dense(1,  kernel_initializer="normal", activation="sigmoid"))
    #compile model
    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
    return model

def create_pipeline(hidden=60):
    steps = []
    steps.append(('scaler', StandardScaler()))
    steps.append(('dl', KerasClassifier(build_fn=create_keras_model,hidden=hidden, verbose=0)))
    pipeline = Pipeline(steps)
    return pipeline

my_neurons = [15, 30, 60]
my_epochs= [50, 100, 150]
my_batch_size = [5,10]
my_param_grid = dict(hidden=my_neurons, epochs=my_epochs, batch_size=my_batch_size)

model2Tune = KerasClassifier(build_fn=create_keras_model, verbose=0)
model2Tune2 = create_pipeline()

griglia = RandomizedSearchCV(estimator=model2Tune, param_distributions = my_param_grid, n_iter=8 )
griglia.fit(X_scaled, Y_encoded) #this works

griglia2 = RandomizedSearchCV(estimator=create_pipeline, param_distributions = my_param_grid, n_iter=8 )
griglia2.fit(X, Y_encoded) #this does not

我们看到 RandomizedSearchCV 适用于 griglia,但不适用于 griglia2,返回

"TypeError: estimator should be an estimator implementing 'fit' method, was passed".

是否可以修改代码使其运行在管道对象下?

提前致谢

估算器参数需要一个对象,而不是指针。当前,您正在传递一个指向生成管道对象的方法的指针。尝试添加 () 来解决这个问题:

griglia2 = RandomizedSearchCV(estimator=create_pipeline(), param_distributions = my_param_grid, n_iter=8 )

现在是关于无效参数错误的第二条评论。需要在实际参数后面加上创建管道时定义的名称,才能传递成功。

查看 Pipeline usage here 的描述。

使用这个:

my_param_grid = dict(dl__hidden=my_neurons, dl__epochs=my_epochs,
                     dl__batch_size=my_batch_size)

注意 dl__(带有两个下划线)。当您想要调整管道内多个对象的参数时,这很有用。

例如,除了上述参数外,您还想调整或指定 StandardScaler 的参数。

那么你的参数网格就变成了:

my_param_grid = dict(dl__hidden=my_neurons, dl__epochs=my_epochs,
                     dl__batch_size=my_batch_size,
                     scaler__with_mean=False)

希望这能解决问题。