MNIST“1”和“5”的二元分类准确率变好了吗?

Accuracy of binary classification on MNIST “1” and “5” get better?

我尝试使用 MNIST 仅使用数字“1”和“5”进行二进制分类。 但是准确率不太好。。下面的程序有什么问题吗? 如果你找到了什么,请给我一些建议。

损失:-9.9190e+04

准确度:0.5599

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
train_filter = np.where((y_train == 1) | (y_train == 5))
test_filter = np.where((y_test == 1) | (y_test == 5))
x_train, y_train = x_train[train_filter], y_train[train_filter]
x_test, y_test = x_test[test_filter], y_test[test_filter]
print("x_train", x_train.shape)
print("x_test", x_test.shape)

# x_train (12163, 28, 28)
# x_test (2027, 28, 28)

model = keras.Sequential(
    [
        keras.layers.Flatten(input_shape=(28, 28)),
        keras.layers.Dense(128, activation="relu"),
        keras.layers.Dense(1, activation="sigmoid"),
    ]
)

model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5)

loss, acc = model.evaluate(x_test, y_test, verbose=2)
print("accuracy:", acc)

# 2027/1 - 0s - loss: -9.9190e+04 - accuracy: 0.5599
# accuracy: 0.5599408

你的 y_train 和 y_test 填充了 class 标签 1 和 5,但是你最后一层的 sigmoid 激活将输出压缩在 0 和 1 之间。

如果你将 y 中的 5 改为 0,你将获得非常高的准确度:

y_train = np.where(y_train == 5, 0, y_train)
y_test = np.where(y_test == 5, 0, y_test)

结果:

64/64 - 0s - loss: 0.0087 - accuracy: 0.9990