【问题标题】:Accuracy of binary classification on MNIST “1” and “5” get better?MNIST“1”和“5”的二元分类准确率变好了吗?
【发布时间】:2020-11-01 08:08:22
【问题描述】:

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

损失:-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

【问题讨论】:

    标签: tensorflow machine-learning prediction mnist


    【解决方案1】:

    您的 y_train 和 y_test 填充了类标签 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
    

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 2018-04-29
      • 2018-05-29
      • 2019-09-27
      • 2019-11-20
      • 2018-04-02
      • 2012-04-15
      • 1970-01-01
      • 2017-12-05
      相关资源
      最近更新 更多