Hiro
Hiro

Reputation: 103

Different accuracy by fit() and evaluate() in Keras with the same dataset

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)

Training Result

Testing Result

Upvotes: 6

Views: 1664

Answers (1)

Justice_Lords
Justice_Lords

Reputation: 969

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.

Reference:-

  1. Keras_issues_thread
  2. Keras_official_doc

Upvotes: 4

Related Questions