网格搜索Keras时出错

时间:2018-08-09 07:12:28

标签: python keras deep-learning

我正在尝试使用网格搜索优化技术来提高使用Keras的Python中的深度学习模型的准确性。 我正在使用的脚本下面

# encode class values as integers
encoder = LabelEncoder()
encoder.fit(train["Group"])
encoder_name_mapping = dict(zip(encoder.classes_, encoder.transform(encoder.classes_)))
print(encoder_name_mapping)
encoded_Y = encoder.transform(train["Group"])

# convert integers to dummy variables (i.e. one hot encoded)
train_y = np_utils.to_categorical(encoded_Y)

    def create_model():
        model = Sequential()
        model.add(Dense(10, input_dim=train_data_features.shape[1], activation='relu'))
        model.add(Dense(len(list(set(train["Group"]))), activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model

    # fix random seed for reproducibility
    seed = 7
    numpy.random.seed(seed)

    # create model
    model = KerasClassifier(build_fn=create_model)

    # define the grid search parameters
    batch_size = [10, 20, 40, 60, 80, 100]
    epochs = [10, 50, 100]
    param_grid = dict(batch_size=batch_size, epochs=epochs)
    grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)

    grid_result = grid.fit(train_data_features, train_y)

但是,我遇到了错误。有人可以帮我吗?

Using TensorFlow backend.
Process SpawnPoolWorker-2:
Traceback (most recent call last):
  File "C:\AppData\Local\Continuum\Anaconda3\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()
  File "C:\AppData\Local\Continuum\Anaconda3\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "C:\AppData\Local\Continuum\Anaconda3\lib\multiprocessing\pool.py", line 108, in worker
    task = get()
  File "C:\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\externals\joblib\pool.py", line 362, in get
    return recv()
  File "C:\AppData\Local\Continuum\Anaconda3\lib\multiprocessing\connection.py", line 251, in recv
    return ForkingPickler.loads(buf.getbuffer())
AttributeError: Can't get attribute 'create_model' on <module '__main__' (built-in)>

2 个答案:

答案 0 :(得分:0)

这里是一个如何将KerasClassifier与GridSearchCV一起使用的示例。 我认为这很清楚,您如何适应它。

def create_model(optimizer='adam'):
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model

optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
param_grid = dict(optimizer=optimizer)
model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=10, verbose=0)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)

答案 1 :(得分:0)

如果您使用GPU进行神经网络训练,请在GridSearchCV中设置n_jobs = 1。您可能只有1个GPU,该参数用于CPU线程。