简体   繁体   English

Tensorflow model.evaluate 给出与训练获得的结果不同的结果

[英]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!您可以通过这种方式查看它们!


  1. Extract the history of model提取模型历史

    history_dict = history.history history_dict.keys()
  2. 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:也许,你得到的结果是这样的:

  • In training process, acc and loss changed with epochs在训练过程中,acc和loss随着epoch的变化而变化
  • But in validation, acc and loss seem to reached a peak after 20 epochs但在验证中,acc 和 loss 似乎在 20 epochs 后达到了峰值

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.

相关问题 在运行模型时使用 tensorflow 进行训练时出现 valueError.Evaluate() - valueError while training with tensorflow while running model.Evaluate() 为什么在Keras训练期间,由model.evaluate()计算的指标与跟踪的指标不同? - Why differ metrics calculated by model.evaluate() from tracked metrics during training in Keras? Keras:model.evaluate() 在训练和 val 集上与上次训练时期后的 acc 和 val_acc 不同 - Keras: model.evaluate() on training and val set differ from the acc and val_acc after last training epoch 当训练具有高精度但 model.evaluate() 精度低时,如何改进 model? - How do I improve the model when training has high accuracy but model.evaluate() gives low accuracy? 为什么 model.predict() 手动计算的准确度与 model.evaluate() 的准确度不同 - Why accuracy manually calculated by model.predict() is different from model.evaluate()'s accuracy 从 Keras model.evaluate 和 model.predict 获得不同的结果 - Getting different results from Keras model.evaluate and model.predict model.evaluate() 如何在 tensorflow 中工作? - How does model.evaluate() work in tensorflow? Keras 中的 model.evaluate() 返回什么值? - What values are returned from model.evaluate() in Keras? 问题 Tensorflow 2.2 与 model.fit 和 model.evaluate - Issue Tensorflow 2.2 with model.fit and model.evaluate 为什么 Keras 在 model.evaluate、model.predicts 和 Z20F35E630DAF44DB8CZF. - Why does Keras gives me different results between model.evaluate, model.predicts & model.fit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM