ValueError:分类指标无法处理多标签指标和二进制目标的混合

时间:2019-02-05 12:40:35

标签: python tensorflow machine-learning keras scikit-learn

我想应用KerasCLassifier解决多类分类问题。 y的值是单次热编码的,例如:

0 1 0
1 0 0
1 0 0

这是我的代码:

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier

# 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, class_weight="balanced", verbose=2)

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

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

grid_result = grid.fit(X_train], y_train_onehot)

当我运行最后一行代码时,它在10个时间段后引发以下错误:

  

/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py   以precision_score(y_true,y_pred,normalize,sample_weight)       174       175#计算每种可能表示的准确性   -> 176 y_type,y_true,y_pred = _check_targets(y_true,y_pred)       177 check_consistent_length(y_true,y_pred,sample_weight)       178 if y_type.startswith('multilabel'):

     

/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py   在_check_targets(y_true,y_pred)中        79如果len(y_type)> 1:        80提高ValueError(“分类指标不能处理{0}的混合”   ---> 81个“和{1}个目标”。format(type_true,type_pred))        82        83#y_type =>上不能有多个值

     

ValueError:分类指标无法处理以下各项的混合问题:   多标签指标和二进制目标

当我写categorical_accuracybalanced_accuracy而不是accuracy时,我无法编译模型。

1 个答案:

答案 0 :(得分:1)

这是一个工作示例:

import numpy as np
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier

N = 100
X_train = np.random.rand(N, 4)
Y_train = np.random.choice([0,1,2], N, p=[.5, .3, .2])

# 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(len(np.unique(Y_train)), kernel_initializer=init, activation='softmax'))
    # Compile model
    model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['sparse_categorical_accuracy'])
    return model

# create model
model = KerasClassifier(build_fn=create_model, class_weight="balanced", verbose=2)

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

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)

PS请注意sparse_categorical_*损失函数和指标的使用。