GridSearch for MultiClass KerasClassifier

时间:2017-10-17 22:23:21

标签: keras grid-search hyperparameters multiclass-classification

我正在尝试使用Keras进行多类分类的网格搜索。以下是代码的一部分:

数据的一些属性如下:

y_
array(['fast', 'immobile', 'immobile', ..., 'slow',
       'immobile', 'slow'],
      dtype='<U17')

y_onehot = pd.get_dummies(y_).values

y_onehot
array([[1, 0, 0],
       [0, 0, 1],
       [0, 0, 1],
    ...
       [0, 1, 0],
       [0, 0, 1],
       [0, 1, 0]], dtype=uint8)

#Do train-test split

y_train.shape
(1904,)

y_train_onehot.shape
(1904, 3)

模特......

# Function to create model, required for KerasClassifier
def create_model(optimizer='rmsprop', init='glorot_uniform'):
    # create model
    model = Sequential()
    model.add(Dense(2048, input_dim=X_train.shape[1], kernel_initializer=init, activation='relu'))
    model.add(Dense(512, kernel_initializer=init, activation='relu'))
    model.add(Dense(y_train_onehot.shape[1], kernel_initializer=init, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model

# create model
model = KerasClassifier(build_fn=create_model, verbose=0)

# grid search epochs, batch size and optimizer
optimizers = ['rmsprop', 'adam']
init = ['glorot_uniform', 'normal', 'uniform']
epochs = [50, 100, 150]
batches = [5, 10, 20]

param_grid = dict(optimizer=optimizers, epochs=epochs, batch_size=batches, init=init)
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy')
grid_result = grid.fit(X_train, y_train_onehot)

这是错误:

--> grid_result = grid.fit(X_train, y_train_onehot)
ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets

代码用于二进制模型,但我希望为多类数据集修改它。请帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

错误发生在softmax层。

我认为您的意思是y_train_onehot.shape[1]而不是y_train_onehot[1]

更新1:这很奇怪,但你的第二个问题似乎是y_train_onehot,你介意尝试两件事:

  1. 在y_train上尝试相同的模型不带 onehot编码。
  2. 如果仅此一项无效,请将损失更改为sparse_categorical_crossentropy
  3. 还要确保将y_train_onehot.shape[1]更改为softmax图层中的类数