[英]ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets
I want to apply KerasCLassifier
to solve multi-class classification problem.我想应用
KerasCLassifier
来解决多类分类问题。 The value of y
is one-hot-encoded, eg: y
的值是单热编码的,例如:
0 1 0
1 0 0
1 0 0
This is my code:这是我的代码:
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)
When I run the last line of code, it throws the following error after 10 epochs:当我运行最后一行代码时,它在 10 个时期后抛出以下错误:
/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight) 174 175 # Compute accuracy for each possible representation --> 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 inaccuracy_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 in _check_targets(y_true, y_pred) 79 if len(y_type) > 1: 80 raise ValueError("Classification metrics can't handle a mix of {0} " ---> 81 "and {1} targets".format(type_true, type_pred)) 82 83 # We can't have more than one value on y_type => The set is no more needed
/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py in _check_targets(y_true, y_pred) 79 if len(y_type) > 1: 80 raise ValueError("Classification metrics can't handle a {0} " ---> 81 " 和 {1} 个目标".format(type_true, type_pred)) 82 83 # y_type 上的值不能超过一个 => 不再需要该集合
ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets
ValueError:分类指标无法处理多标签指标和二进制目标的混合
When I write categorical_accuracy
or balanced_accuracy
instead of accuracy
, I cannot compile a model.当我编写
categorical_accuracy
或balanced_accuracy
而不是accuracy
,我无法编译模型。
Here is a working demo:这是一个工作演示:
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 please pay attention at the usage of sparse_categorical_*
loss function and metrics. PS 请注意
sparse_categorical_*
损失函数和指标的使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.