ValueError:分类指标无法处理多标签指标和二元目标的混合
Posted
技术标签:
【中文标题】ValueError:分类指标无法处理多标签指标和二元目标的混合【英文标题】:ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets 【发布时间】:2019-06-29 06:53:35 【问题描述】:我想申请KerasCLassifier
解决多类分类问题。 y
的值是 one-hot-encoded,例如:
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 个 epoch 后抛出以下错误:
/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py 在 accuracy_score(y_true, y_pred, normalize, sample_weight) 174 175 # 计算每个可能表示的准确度 --> 176 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 第177章 178 如果 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 raise ValueError("Classification metrics can't handle a mix of 0" ---> 81 "和 1 个目标".format(type_true, type_pred)) 82 83 # y_type 上的值不能超过一个 => 不再需要该集合
ValueError:分类指标无法处理混合 多标签指标和二元目标
当我写categorical_accuracy
或balanced_accuracy
而不是accuracy
时,我无法编译模型。
【问题讨论】:
【参考方案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_*
损失函数和指标的使用。
【讨论】:
谢谢你,@MaxU。我对这个任务还有另一个问题。如果您有时间查看,请访问此线程:***.com/questions/54537304/…以上是关于ValueError:分类指标无法处理多标签指标和二元目标的混合的主要内容,如果未能解决你的问题,请参考以下文章
f-score:ValueError:分类指标无法处理多标签指标和连续多输出目标的混合
如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合
Logistic 回归 - ValueError:分类指标无法处理连续多输出和二进制目标的混合