我试图复制Lenet-5神经网络,我想显示我的结果的混淆矩阵来评价我的结果。
这就是我所做的:
# Create the model
model = models.Sequential()
model.add(layers.Conv2D(filters=6, kernel_size=(5,5), activation='relu', input_shape=(28,28,1)))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Conv2D(filters=16, kernel_size=(5,5), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(120,activation='relu'))
model.add(layers.Dense(84,activation='relu'))
model.add(layers.Dense(10,activation='softmax'))
# I categorize the data because I use categorical crossentropy
train_labels = to_categorical(train_labels)
val_labels = to_categorical(val_labels)
test_labels = to_categorical(test_labels)
# Compile the model
model.compile(optimizer=SGD(learning_rate=0.1),
loss='categorical_crossentropy',
metrics=['accuracy'])
# Fit the model
history = model.fit(train_images, train_labels,
epochs=10, batch_size=128,
validation_data=(val_images, val_labels),
verbose=2)既然在这里,我想(希望)一切都好。我想评估我的模型的性能。
首先,我绘制了准确性图。
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epochs'); plt.ylabel('Accuracy')
plt.ylim([0.85, 1])
plt.legend(loc='best')

然后,我评估准确性和损失。
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
>>> OUT: test_acc: 0.9909999966621399, test_loss: 0.03354883939027786现在我想看看混乱矩阵,
from sklearn.metrics import confusion_matrix
predictions = model.predict(test_images)
confusion = confusion_matrix(test_labels, predictions.round())但我有个错误:
ValueError:不支持多标签指示器
我认为问题可能是分类测试数据,但不是。有人能帮我吗?我现在的目标是尽可能最好地评估我的模型(我是新手),我认为混乱矩阵是个好主意。
非常感谢!
发布于 2021-06-10 12:25:40
model.predict返回模型输出的向量表示(例如[0.1, 0.05, 0.0, 0.85],但是confusion_matrix需要输出的标签/类(例如3)。
您可以使用np.argmax函数从向量到预测的标签:
predictedLabels = np.argmax(predictions, axis=1)如果test_labels是一个热编码的,则可能必须在它们上使用相同的方法。
P.S.:查看ConfusionMatrixDisplay以获得混乱矩阵的良好显示。
https://stackoverflow.com/questions/67920914
复制相似问题