【问题标题】:Why does normalizing MNIST images reduce accuracy?为什么归一化 MNIST 图像会降低准确性?
【发布时间】:2020-06-22 13:37:28
【问题描述】:

我正在使用一个基本的 NN 来训练和测试 MNIST 数据集的准确性。

系统:i5 第 8 代,GPU - Nvidia 1050Ti

这是我的代码:

from __future__ import print_function,absolute_import,unicode_literals,division
import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train,y_train) , (x_test,y_test) = mnist.load_data()
#x_train , y_train = x_train/255.0 , y_train/255.0

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(312,activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(10,activation='softmax')
])

model.compile(
optimizer='Adamax',
loss="sparse_categorical_crossentropy",
metrics=['accuracy']
)
model.fit(x_train,y_train,epochs=5)
model.evaluate(x_test,y_test)

当我像第 5 行那样对图像进行归一化时,准确度会急剧下降:

loss: 10392.0626 - accuracy: 0.0980

但是,当我不规范化它们时,它会给出:

- loss: 0.2409 - accuracy: 0.9420

一般来说,归一化数据有助于梯度下降更快地收敛。为什么会有这么大的差异? 我错过了什么?

【问题讨论】:

  • 为什么要将标签除以 255?
  • 正如GoodDeeds所说,您不能将标签除以255。标签是0、1、2等。那是错误的。此外,您忘记标准化 x_test。

标签: python tensorflow deep-learning neural-network mnist


【解决方案1】:

使用这个:

(x_train, y_train) , (x_test,y_test) = mnist.load_data()
x_train , x_test = x_train/255.0 , x_test/255.0

您将标签除以 255,因此您没有正确规范化。

【讨论】:

  • @user9999114 如果你得到你的答案,你介意接受这个解决方案吗?
【解决方案2】:

您需要对训练集和测试集进行相同的归一化。 如果您使用预训练的网络,您应该进行与培训师相同的标准化;在这里,您缺少对测试集的规范化。

x_train = x_train/255.0
x_test = x_test/255.0 

不客气。

【讨论】:

  • 其实这并不是解决作者疑惑的办法。作者还没有对测试集做评价,显示的准确率和损失都是在训练集上的。问题是他将标签除以 255,这是他不应该做的。
猜你喜欢
  • 2012-01-18
  • 2020-03-22
  • 2020-11-19
  • 1970-01-01
  • 2023-04-10
  • 2018-12-24
  • 2013-12-08
  • 1970-01-01
  • 2016-08-08
相关资源
最近更新 更多