为什么混淆Maxtrix和Model ACC不同

时间:2019-01-12 06:10:31

标签: keras deep-learning conv-neural-network confusion-matrix

当前,我们正在使用Confusion Matrix构建CNN,这与Model Acc不同。

使用Model ACC计算出 51%的准确性时,

Confusion Matrix 92.3 %。

在这种情况下,Model ACCConfusion Matrix的以下哪项正确?

该如何解决该问题?

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
['accuracy'])
hist = model.fit_generator(train_generator, steps_per_epoch=10, epochs= 20, 
validation_data=test_generator, validation_steps=10 )
#5. Model Evaluate
print("-- Evaluate --")
scores = model.evaluate_generator(test_generator)
print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))

# 6.  Make Confusion Matrix
print("-- Predict --")
output = model.predict_generator(test_generator)
print('Counfusion Matrix')
output1 = np.argmax(output,axis=1)
cm1=confusion_matrix(test_generator.classes, output1)
print(cm1)

1 个答案:

答案 0 :(得分:0)

您必须做错什么,我对mnsit数据集进行了一个简单的实验,结果还是一样。

# my model loss and metric
model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
# fit model and train
# evaluate model
score = model.evaluate(X_train, Y_train1, verbose=1)
print('Val accuracy by model:', score[1])

from sklearn.metrics import confusion_matrix as cm
pred = model.predict(X_train)
pred_label = [np.argmax(i) for i in pred]
actual_label = [np.argmax(i) for i in Y_train1]

cm1 = cm(pred_label, actual_label)
print('Val accuracy by confusion-metrics:', np.trace(cm1)/Y_train1.shape[0])

查看结果:

Val accuracy by model: 0.9872380952380952
Val accuracy by confusion-metrics: 0.9872380952380952

请确保您传递的是正确的指标和损失函数。