sklearn RandomizedSearchCV with Pipelined KerasClassifier

时间:2017-10-13 14:06:57

标签: python scikit-learn keras pipeline

我正在Keras模型上使用sklearn执行超参数调优优化任务。我正在尝试优化管道中的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应该是一个实现' fit'   方法,已通过"。

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

提前致谢

1 个答案:

答案 0 :(得分:2)

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

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)

希望这可以解决问题。