Evaluate the Model
The model has now been trained. Next, we test it using data that the model did not use during training. This tells us whether the model has actually learned useful patterns.
Evaluation checks how well the model works on unseen data.
Training tells the model how to learn. Evaluation tells us how well that learning works on examples that were kept separate from the training process.
Training Data vs Test Data
The MNIST dataset gives us two important groups of data:
Training Data
↓
x_train
y_train
Test Data
↓
x_test
y_test
The training data is used to teach the model.
The test data is kept separate and is used later to evaluate the model.
Why Not Test Using Training Data?
Imagine you give a student the exact questions they practiced and then use the same questions for the final exam.
Training Data
↓
Model learns these examples
↓
Evaluate on same examples
↓
Very high score
That score may not tell us whether the model can recognize new examples.
This is why we use separate test data.
The evaluate() Function
test_loss, test_accuracy = model.evaluate(
x_test,
y_test
)
The evaluate() method runs the trained model
against the test dataset.
x_test
↓
Trained Model
↓
Predictions
↓
Compare with y_test
↓
Loss + Accuracy
What Is Test Accuracy?
Test accuracy tells us how many test predictions were correct.
10,000 test images
9,700 correctly classified
Accuracy = 97%
So if the model achieves 97% test accuracy, it correctly predicts approximately 97 out of every 100 test images.
What Is Test Loss?
Loss measures how far the model's predictions are from the correct answers.
Prediction
↓
Compare with correct label
↓
Calculate loss
Generally:
Lower loss
↓
Better predictions
Higher loss
↓
More prediction error
Accuracy and loss give us different views of model performance.
Complete Evaluation Code
test_loss, test_accuracy = model.evaluate(
x_test,
y_test
)
print("Test Loss:", test_loss)
print("Test Accuracy:", test_accuracy)
A possible result could look like:
Test Loss: 0.08
Test Accuracy: 0.97
These values are examples. Your actual values will depend on the model and training process.
Understand the Result
Test Accuracy = 0.97
Accuracy is returned as a decimal value between 0 and 1.
0.97 × 100 = 97%
So:
0.97
↓
97%
↓
Approximately 97 out of 100
test images were classified correctly.
Training Accuracy vs Test Accuracy
These two values should not be confused.
Training Accuracy
↓
Performance on training data
Test Accuracy
↓
Performance on unseen test data
For example:
Training Accuracy = 99%
Test Accuracy = 97%
This is much more useful than looking only at the training accuracy.
Example of Good Generalization
Training Accuracy
99%
Test Accuracy
97%
The model performs well on both training and unseen test data.
Training → 99%
Test → 97%
Small difference
↓
Generally good generalization
Example of Overfitting
Training Accuracy
99.9%
Test Accuracy
85%
This is a warning sign.
The model performs extremely well on the data it trained on but much worse on unseen data.
Training
99.9%
↓
Very high
Test
85%
↓
Much lower
Possible Overfitting
The model may have learned the training data too closely instead of learning patterns that generalize well.
Example of Underfitting
Training Accuracy
70%
Test Accuracy
68%
Here the model performs poorly even on the training data.
Training
70%
↓
Poor
Test
68%
↓
Poor
Possible Underfitting
The model may not be complex enough, may not have trained enough, or may need better features or configuration.
Evaluation Is Not Training
During training:
model.fit(
x_train,
y_train
)
The model updates its weights.
During evaluation:
model.evaluate(
x_test,
y_test
)
We measure performance. The purpose is not to train the model on the test data.
Evaluate the Model With Verbose Output
test_loss, test_accuracy = model.evaluate(
x_test,
y_test,
verbose=1
)
print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_accuracy:.4f}")
The verbose option controls how much progress
information TensorFlow displays while evaluating.
Convert Accuracy to Percentage
accuracy_percentage = test_accuracy * 100
print(
f"Test Accuracy: {accuracy_percentage:.2f}%"
)
For example:
test_accuracy = 0.975
0.975 × 100
= 97.5%
Make Predictions on Test Images
Evaluation gives us an overall score. We can also ask the model to make individual predictions.
predictions = model.predict(x_test)
The result contains probabilities for each of the 10 classes.
0 → 0.01
1 → 0.01
2 → 0.02
3 → 0.01
4 → 0.01
5 → 0.02
6 → 0.01
7 → 0.89
8 → 0.01
9 → 0.01
The largest value tells us the predicted class.
Get the Predicted Digit
import numpy as np
predicted_digit = np.argmax(
predictions[0]
)
print("Predicted Digit:", predicted_digit)
np.argmax() returns the index of the largest
probability.
Probabilities:
[0.01, 0.01, 0.02, 0.01,
0.01, 0.02, 0.01, 0.89,
0.01, 0.01]
Largest value = 0.89
Index = 7
Prediction = 7
Compare Prediction With Actual Label
actual_digit = y_test[0]
print("Actual:", actual_digit)
print("Predicted:", predicted_digit)
For example:
Actual: 7
Predicted: 7
Correct ✓
Another example:
Actual: 7
Predicted: 2
Incorrect ✗
Complete Evaluation Example
import tensorflow as tf
import numpy as np
# Load MNIST
(x_train, y_train), (x_test, y_test) = \
tf.keras.datasets.mnist.load_data()
# Normalize
x_train = x_train / 255.0
x_test = x_test / 255.0
# Build model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(
input_shape=(28, 28)
),
tf.keras.layers.Dense(
128,
activation="relu"
),
tf.keras.layers.Dense(
64,
activation="relu"
),
tf.keras.layers.Dense(
10,
activation="softmax"
)
])
# Compile
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
# Train
model.fit(
x_train,
y_train,
epochs=5,
batch_size=32
)
# Evaluate
test_loss, test_accuracy = model.evaluate(
x_test,
y_test
)
print("Test Loss:", test_loss)
print("Test Accuracy:", test_accuracy)
# Predict
predictions = model.predict(x_test)
predicted_digit = np.argmax(
predictions[0]
)
print("Actual:", y_test[0])
print("Predicted:", predicted_digit)
The Complete Flow
1. Prepare Data
↓
2. Build Neural Network
↓
3. Compile Model
↓
4. Train Model
↓
model.fit()
↓
5. Evaluate Model
↓
model.evaluate()
↓
6. Make Predictions
↓
model.predict()
This is the basic workflow of a supervised deep learning project.
What Does a Good Evaluation Tell Us?
Suppose we get:
Training Accuracy = 98.5%
Test Accuracy = 97.2%
The model performs well on both the training and unseen test data.
That gives us reasonable evidence that the model learned useful patterns for this classification task.
But accuracy alone is not always enough for every machine learning problem. For more complex classification tasks, we may also examine metrics such as precision, recall, F1-score, or a confusion matrix.
Evaluation tells us whether the model generalizes.
We train the model with training data and then use
separate test data to measure how well it performs on
unseen examples. The main tools here are
model.evaluate() for overall performance and
model.predict() for individual predictions.
Quick Check
To measure how well the trained model performs on data that was not used to update its weights.
In this model, it returns the test loss and test accuracy.
evaluate() measures overall model performance, while predict() produces predictions for specific input examples.