首页 文章

关于Keras分类器的精确度,召回率和FMeasure的Sklearn度量标准

提问于
浏览
0

我在尝试计算精度,召回和FMeasure时遇到问题,作为评估在Tensorflow上的Keras中实现的LSTM文本分类器的度量的一部分 . 我知道来自 Keras 2.02 metrics模块的these functions were removed .

# create the model
embedding_vector_length = 32
model = Sequential()
# load the dataset with word embedding but only keep the top n words, zero the rest
model.add(Embedding(top_words, embedding_vector_length, input_length=max_tweet_length)) 

model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, epochs=3, batch_size=64)

# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
print(scores)

# print the classification report
from sklearn.metrics import classification_report
predicted = model.predict(X_test)
report = classification_report(y_test, predicted)
print(report)

作为替代方案,我正在解析拟合模型并将输出预测为对象 sklearn.metrics.classification_report 但是我不断得到关于目标数据类型的错误 . 预测输出是 float32 格式,因为我使用的是Sigmoid激活函数,而标签是具有二进制分类级别的文本集合 . 我从Keras指标中得到了准确度评估,但是预先设置,召回,测量评估是问题所在 .

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/sklearn/metrics/classification.py", line 1261, in precision_score
    sample_weight=sample_weight)
  File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/sklearn/metrics/classification.py", line 1025, in precision_recall_fscore_support
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/sklearn/metrics/classification.py", line 81, in _check_targets
    "and {1} targets".format(type_true, type_pred))
ValueError: Classification metrics can't handle a mix of binary and continuous targets

1 回答

  • 0

    显然你没有找到 model.predict 的输出 . 事实上,对于您的情况(您使用 binary_classification ),您需要调用 model.predict_classes 以匹配您的类/标签数据 y .

相关问题