在具有相同数据集的Keras中,fit()和valuate()的准确性不同 [英] Different accuracy by fit() and evaluate() in Keras with the same dataset

查看:124
本文介绍了在具有相同数据集的Keras中,fit()和valuate()的准确性不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了Keras的代码来训练GoogleNet.但是,从fit()获得的准确性是100%,但是在使用与validate()相同的训练数据集的情况下,准确性仅保留25%,这是如此巨大的差异!!!而且,不像fit()那样,由validate()提供的准确性不会因进行更多次训练而得到提高,这意味着它几乎保持在25%的水平.

I program Keras's code to train GoogleNet. However, accuracy gotten from fit() is 100% yet with the same training dataset used for evaluate(), accuracy remains 25% only, which has such huge discrepancy!!! Also, accuracy by evaluate(), which is not like fit(), won't get improved for training more times, which means it almost stays in 25%.

有人知道这种情况有什么问题吗?

Does anyone has idea of what is wrong with this situation?

# Training Dataset and labels r given. Here load GoogleNet model
from keras.models import load_model
model = load_model('FT_InceptionV3.h5')
# Training Phase
model.fit(x=X_train, 
              y=y_train, 
              batch_size=5, 
              epochs=20, 
              validation_split=0,
              #callbacks=[tensorboard]
             )

#Testing Phase
train_loss , train_acc=model.evaluate(X_train, y_train, verbose=1)
print("Train loss=",train_loss,"Train accuracy",train_acc)

训练结果

测试结果

推荐答案

深入研究Keras问题后,我发现了这一点.

After some digging into Keras issues, I found this.

这样做的原因是,当您使用健身时,在每批训练数据中,权重都会更新.拟合方法返回的损失值不是最终模型损失的平均值,而是每批使用的所有略有不同的模型的损失平均值.

The reason for this is that when you use fit, At each batch of the training data the weights are updated. The loss value returned by the fit method is not the mean of the loss of the final model, but the mean of the loss of all slightly different models used on each batch.

另一方面,当您用于评估时,在整个数据集上使用相同的模型.而且该模型实际上甚至没有出现在拟合方法的损失中,因为即使在最后一批训练中,所计算的损失也将用于更新模型的权重.

On the other hand, when you use to evaluate, the same model is used on the whole dataset. And this model actually doesn't even appear in the loss of the fit method since even at the last batch of training, the loss computed is used to update the model's weights.

总结起来,拟合和评估有两种完全不同的行为.

To sum everything up, fit and evaluate have two completely different behaviours.

参考:-

  1. Keras_issues_thread
  1. Keras_issues_thread
  2. Keras_official_doc

这篇关于在具有相同数据集的Keras中,fit()和valuate()的准确性不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆