【发布时间】:2018-11-17 14:04:47
【问题描述】:
我尝试获取mnist数据集的混淆矩阵。
这是我的代码:
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation=tf.nn.tanh),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, callbacks=[history])
test_predictions = model.predict(x_test)
# Compute confusion matrix
confusion = tf.confusion_matrix(y_test, test_predictions)
问题在于test_prediction 是一个 10000 x 10 矩阵,而 y_test 是 10000 x 1 矩阵。事实上,神经网络不会为每个测试样本提供输出。如何计算这种情况下的混淆矩阵?
然后我怎样才能呈现混淆矩阵?我可以为此目的使用 sci-kit 库吗?
【问题讨论】:
标签: python tensorflow keras confusion-matrix