python - 为什么 fit() 期间的训练集准确度与对相同数据使用预测后计算出的准确度不同?

标签 python tensorflow machine-learning keras deep-learning

使用 Tensorflow - Keras 编写了基本的深度学习模型。

为什么训练结束时报告的训练集准确度 (0.4097) 与使用预测函数(或使用评估,给出相同数字)对相同训练数据进行直接计算而直接报告的结果不同= 0.6463?

MWE如下;之后直接输出。

from extra_keras_datasets import kmnist
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
import numpy as np


# Model configuration
no_classes = 10


# Load KMNIST dataset
(input_train, target_train), (input_test, target_test) = kmnist.load_data(type='kmnist')

# Shape of the input sets
input_train_shape = input_train.shape
input_test_shape = input_test.shape 

# Keras layer input shape
input_shape = (input_train_shape[1], input_train_shape[2], 1)



# Reshape the training data to include channels
input_train = input_train.reshape(input_train_shape[0], input_train_shape[1], input_train_shape[2], 1)
input_test = input_test.reshape(input_test_shape[0], input_test_shape[1], input_test_shape[2], 1)


# Parse numbers as floats
input_train = input_train.astype('float32')
input_test = input_test.astype('float32')

# Normalize input data
input_train = input_train / 255
input_test = input_test / 255


# Create the model
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(no_classes, activation='softmax'))


# Compile the model
model.compile(loss=tensorflow.keras.losses.sparse_categorical_crossentropy,
              optimizer=tensorflow.keras.optimizers.Adam(),
              metrics=['accuracy'])


# Fit data to model
history = model.fit(input_train, target_train,
            batch_size=2000,
            epochs=1,
            verbose=1)

prediction = model.predict(input_train)
print("Prediction accuracy = ", np.mean( np.argmax(prediction, axis=1) == target_train))

model.evaluate(input_train, target_train, verbose=2)

最后几行输出:

30/30 [==============================] - 0s 3ms/step - loss: 1.8336 - accuracy: 0.4097
Prediction accuracy =  0.6463166666666667
1875/1875 - 1s - loss: 1.3406 - accuracy: 0.6463

编辑

下面的初步答案解决了我的第一个问题,指出当您只运行 1 个周期时,批量大小很重要。当运行小批量(或批量大小= 1)或更多时期时,您可以将拟合后的预测精度推得非常接近拟合本身所抛出的最终精度。这很好!

我最初问这个问题是因为我在处理更复杂的模型时遇到了麻烦。

我仍然无法理解这种情况下发生的情况(是的,它涉及批量标准化)。要获得我的 MWE,请将上面“创建模型”下面的所有内容替换为下面的代码,以通过批量归一化实现一些完全连接的层。

当您运行两个 epoch 时,您会看到所有 30 个小批量的准确率非常稳定(30 个小批量是因为训练集中的 60,000 个小批量除以每批中的 2000 个)。我发现在整个第二个训练周期中,准确率始终保持在 83%。

但是执行此操作后,拟合后的预测结果只有 10% 左右。谁能解释一下吗?

model = Sequential()
model.add(Dense(50, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
model.add(Dense(20, activation='relu'))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(no_classes, activation='softmax'))


# Compile the model
model.compile(loss=tensorflow.keras.losses.sparse_categorical_crossentropy,
              optimizer=tensorflow.keras.optimizers.Adam(),
              metrics=['accuracy'])


# Fit data to model
history = model.fit(input_train, target_train,
            batch_size=2000,
            epochs=2,
            verbose=1)

prediction = model.predict(input_train)

print("Prediction accuracy = ", np.mean( np.argmax(prediction, axis=1) == target_train))

model.evaluate(input_train, target_train, verbose=2, batch_size = batch_size)
30/30 [==============================] - 46s 2s/step - loss: 0.5567 - accuracy: 0.8345
Prediction accuracy =  0.10098333333333333

最佳答案

发生这种情况的一个原因是,最后报告的准确性考虑了整个时期,其参数非恒定,并且仍在优化。

评估模型时,参数停止变化,它们保持最终(希望是最优化的)状态。与上一个纪元不同的是,上一个纪元的参数处于各种(希望是不太优化的)状态,在纪元开始时更是如此。


已删除,因为我现在发现您在这种情况下没有使用批量归一化。


我假设这是由于BatchNormalization造成的。

例如参见 here

在训练期间,使用移动平均值。

在推理过程中,我们已经有了归一化参数

这可能是造成差异的原因。

请尝试不使用它,看看是否仍然存在如此巨大的差异。

关于python - 为什么 fit() 期间的训练集准确度与对相同数据使用预测后计算出的准确度不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70786197/

相关文章:

python - 在 tensorflow 中沿任何轴归一化意味着什么?

machine-learning - 深度学习中可能/也许的类别

python - Pandas 重命名操作更改两列而不是一列上的列名称

python - 如何用python获取网格和平面之间的横截面周长?

python - 将 pandas 数据框作为 csv 保存到 gcloud 存储桶

python - cx_Freeze "no module named google"错误

tensorflow - 如何为新类别重新训练Inception的最后一层

python - 如何在 python 中为 t-SNE 添加标签

machine-learning - α-LMS算法挫折问题

python - Exif阅读库