DEEP LEARNING • LESSON 15

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.

SIMPLE IDEA

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.

01

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.

02

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.

03

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
04

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.

05

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.

06

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.

07

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.
08

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.

09

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
10

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.

11

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.

12

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.

13

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.

14

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%
15

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.

16

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
17

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 ✗
18

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)
19

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.

20

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.

KEY TAKEAWAY

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

Why do we use test data?

To measure how well the trained model performs on data that was not used to update its weights.

What does model.evaluate() return?

In this model, it returns the test loss and test accuracy.

What is the difference between evaluate() and predict()?

evaluate() measures overall model performance, while predict() produces predictions for specific input examples.