[英]Tensorflow model.evaluate gives different result from that obtained from training
I am using tensorflow to do a multi-class classification我正在使用 tensorflow 进行多类分类
I load the training dataset and validation dataset in the following way我通过以下方式加载训练数据集和验证数据集
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
Then when I train the model using model.fit()然后当我使用 model.fit() 训练模型时
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs,
shuffle=True
)
I get validation accuracy around 95%.我得到大约 95% 的验证准确率。
But when I load the same validation set and use model.evaluate()但是当我加载相同的验证集并使用 model.evaluate()
model.evaluate(val_ds)
I get very low accuracy (around 10%).我得到的准确度非常低(大约 10%)。
Why am I getting such different results?为什么我得到如此不同的结果? Am I using the model.evaluate function incorrectly?我是否错误地使用了 model.evaluate 函数?
Note : In the model.compile() I am specifying the following, Optimizer - Adam, Loss - SparseCategoricalCrossentropy, Metric - Accuracy注意:在 model.compile() 中,我指定了以下内容,Optimizer - Adam,Loss - SparseCategoricalCrossentropy,Metric - Accuracy
Model.evaluate() output Model.evaluate() 输出
41/41 [==============================] - 5s 118ms/step - loss: 0.3037 - accuracy: 0.1032
Test Loss - 0.3036555051803589
Test Acc - 0.10315627604722977
Model.fit() output for last three epochs最近三个时期的 Model.fit() 输出
Epoch 8/10
41/41 [==============================] - 3s 80ms/step - loss: 0.6094 - accuracy: 0.8861 - val_loss: 0.4489 - val_accuracy: 0.9483
Epoch 9/10
41/41 [==============================] - 3s 80ms/step - loss: 0.5377 - accuracy: 0.8953 - val_loss: 0.3868 - val_accuracy: 0.9554
Epoch 10/10
41/41 [==============================] - 3s 80ms/step - loss: 0.4663 - accuracy: 0.9092 - val_loss: 0.3404 - val_accuracy: 0.9590
I am using tensorflow to do a multi-class classification我正在使用tensorflow进行多类分类
I load the training dataset and validation dataset in the following way我以以下方式加载训练数据集和验证数据集
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
Then when I train the model using model.fit()然后当我使用model.fit()训练模型时
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs,
shuffle=True
)
I get validation accuracy around 95%.我得到了约95%的验证准确性。
But when I load the same validation set and use model.evaluate()但是当我加载相同的验证集并使用model.evaluate()时
model.evaluate(val_ds)
I get very low accuracy (around 10%).我得到的准确率很低(大约10%)。
Why am I getting such different results?为什么我会得到如此不同的结果? Am I using the model.evaluate function incorrectly?我使用了model.evaluate函数不正确吗?
Note : In the model.compile() I am specifying the following, Optimizer - Adam, Loss - SparseCategoricalCrossentropy, Metric - Accuracy注意:在model.compile()中,我指定以下内容:优化器-亚当,损耗-SparseCategoricalCrossentropy,度量-准确性
Model.evaluate() output Model.evaluate()输出
41/41 [==============================] - 5s 118ms/step - loss: 0.3037 - accuracy: 0.1032
Test Loss - 0.3036555051803589
Test Acc - 0.10315627604722977
Model.fit() output for last three epochs最后三个时期的Model.fit()输出
Epoch 8/10
41/41 [==============================] - 3s 80ms/step - loss: 0.6094 - accuracy: 0.8861 - val_loss: 0.4489 - val_accuracy: 0.9483
Epoch 9/10
41/41 [==============================] - 3s 80ms/step - loss: 0.5377 - accuracy: 0.8953 - val_loss: 0.3868 - val_accuracy: 0.9554
Epoch 10/10
41/41 [==============================] - 3s 80ms/step - loss: 0.4663 - accuracy: 0.9092 - val_loss: 0.3404 - val_accuracy: 0.9590
Why am I getting such different results?为什么我得到如此不同的结果? Am I using the model.evaluate function incorrectly?我是否错误地使用了 model.evaluate 函数?
I suppose that it is the over fitting that cause this issue.我想是过度拟合导致了这个问题。 You can check them out in this way!您可以通过这种方式查看它们!
Extract the history of model提取模型历史
history_dict = history.history history_dict.keys()
Visualize the history可视化历史
import matplotlib.pyplot as plt acc=history_dict['accuracy'] val_acc=history_dict['val_accuracy'] loss=history_dict['loss'] val_loss=history_dict['val_loss'] epochs=range(1,len(acc)+1) plt.figure(figsize=(10,10)) ax1=plt.subplot(221) ax1.plot(epochs,loss,'bo',label='Training loss') ax1.plot(epochs,acc,'ro',label='Training acc') ax1.set_title('loss and acc of Training') ax1.set_xlabel('Epochs') ax1.set_ylabel('Loss') ax1.legend() ax2=plt.subplot(222) ax2.plot(epochs,val_acc,'r',label='Validation acc') ax2.plot(epochs,val_loss,'b',label='Validation loss') ax2.set_title('loss and acc of Training') ax2.set_xlabel('Epochs') ax2.set_ylabel('Acc') ax2.legend()
Maybe, the results you get are like these:也许,你得到的结果是这样的:
Solution解决方案
It turns out that, when overfitting occurs, fewer epochs can be set to avoid this problem!事实证明,当发生过拟合时,可以设置更少的 epoch 来避免这个问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.