Keras拟合分类器具有2个或更多输出类,必须指定度量标签

时间:2018-09-06 10:06:21

标签: scikit-learn keras

尝试拟合分类器时(使用sklearn的KerasWrapper,即from keras.wrappers.scikit_learn import KerasClassifier),出现以下错误:

Traceback (most recent call last):
  File "extArgExtraction.py", line 915, in <module>
a, p, r, f = evaluatePositionOnlyNeural(matrix, numIterations)
  File "extArgExtraction.py", line 367, in evaluatePositionOnlyNeural
classifier.fit(X, Y, class_weight=class_weights)
  File "/usr/local/lib/python3.5/dist-packages/keras/wrappers/scikit_learn.py", line 210, in fit
return super(KerasClassifier, self).fit(x, y, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/keras/wrappers/scikit_learn.py", line 141, in fit
self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
  File "extArgExtraction.py", line 493, in create_baseline_model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy', keras_metrics.precision(), keras_metrics.recall(), keras_metrics.f1_score()]) # perhaps this should not be binary (but categorical instead)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 440, in compile
handle_metrics(output_metrics)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 409, in handle_metrics
mask=masks[i])
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/training_utils.py", line 403, in weighted
score_array = fn(y_true, y_pred)
  File "/usr/local/lib/python3.5/dist-packages/keras_metrics/metrics.py", line 213, in __call__
tp = self.tp(y_true, y_pred)
  File "/usr/local/lib/python3.5/dist-packages/keras_metrics/metrics.py", line 70, in __call__
y_true, y_pred = self.cast(y_true, y_pred)
  File "/usr/local/lib/python3.5/dist-packages/keras_metrics/metrics.py", line 24, in cast
return self.cast_strategy(y_true, y_pred, dtype=dtype)
  File "/usr/local/lib/python3.5/dist-packages/keras_metrics/metrics.py", line 46, in _categorical
raise ValueError("With 2 and more output classes a "

ValueError: With 2 and more output classes a metric label must be specified

相关代码段:

df = pandas.DataFrame(numpy.array(nmatrix), columns=None)
ds = df.values
X = ds[:,0:numpy.shape(df)[1]-1].astype(float)
Y = to_categorical(numpy.array(labels))#numpy.reshape(numpy.array(labels), len(labels), 1))

classifier.fit(X, Y)#, class_weight=class_weights)

我在任何地方(在Google上)都找不到此错误。有人对如何指定指标标签有任何想法吗?

1 个答案:

答案 0 :(得分:1)

最相关的一行是这样:

model.compile(loss='categorical_crossentropy', 
              optimizer='adam', 
              metrics=['categorical_accuracy', 
                       keras_metrics.precision(), 
                       keras_metrics.recall(), 
                       keras_metrics.f1_score()]) 

您会看到'precision''recall',因此'f1_score'是根据真阳性,真阴性,假阳性,假阴性计算得出的。

因此,在多类别设置中,如何计算指定的预测为真阳性或假阳性或其他?为此,您需要指定肯定标签(您要视为肯定的类)。

您需要将代码更改为:

model.compile(loss='categorical_crossentropy', 
              optimizer='adam', 
              metrics=['categorical_accuracy', 
                       keras_metrics.precision(label=class_value), 
                       keras_metrics.recall(label=class_value), 
                       keras_metrics.f1_score(label=class_value)]) 

其中class_value是您希望被视为肯定的班级。所有其他类别都将被视为否定语。