Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

Keras model.fit vs model.evaluate - Explain the difference.

Asked by David Edmunds Feb 16, 2023 1.3K views 1 answer
Share

About this question

The following is a small snippet of the code, but I'm trying to understand the results of model.fit with train and test dataset vs the model.evaluate results. I'm not sure if they do not match up or if I'm not understanding how to read the results?

batch_size = 16
img_height = 127
img_width = 127
channel = 3 #RGB
train_dataset = image_dataset_from_directory(Train_data_dir,
                                             shuffle=True,
                                             batch_size=batch_size,
                                             image_size=(img_height, img_width),
                                             class_names = class_names)
##Transfer learning code from mobilenetV2/imagenet here to create model
initial_epochs = 10
history = model.fit(train_dataset, validation_data=test_dataset, epochs=initial_epochs)


train accuracy: 0.8030709732662548 from history.history['accuracy']
train loss: 0.26204305738210676 from history.history['loss']
test accuracy: 0.6920163252136924 from history.history['val_accuracy']
test loss: 0.6397175192832947 from history.history['val_loss']
The below are the shapes and datatypes of the datasets and how I'm passing them to the model.fit:
train_dataset: Found 5216 files belonging to 2 classes.
test_dataset: Found 624 files belonging to 2 classes.
initial_epochs = 10
history = model.fit(train_dataset, validation_data=test_dataset, epochs=initial_epochs)
Then I tried to check the evaluate method on the test_dataset and I'm not sure if what I'm doing is correct, but it doesn't seem to match what the fit method results give:
score = model.evaluate(test_dataset, verbose=0)
print(score) print(f'Test loss: {score[0]} / Test accuracy: {score[1]}')
Test loss: 0.6872978806495667 / Test accuracy: 0.7644230723381042

Are the results supposed to match for the fit method and the evaluate method?

Your answer

1 Answer

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.