Make Predictions
The model has been trained and evaluated. Now we can give it an image it has not seen before and ask it to predict which digit the image represents.
Prediction means giving new input to the trained model.
During training, the model learned patterns from the training data. During prediction, we use those learned patterns to produce an answer for new input.
What Is a Prediction?
Suppose we have a handwritten digit image.
New Image
↓
Trained Neural Network
↓
Prediction
↓
7
The model looks at the patterns in the image and produces probabilities for each possible digit.
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 probability is for digit 7, so the model predicts 7.
The predict() Function
TensorFlow provides the predict() method for
generating predictions.
predictions = model.predict(x_test)
This sends the input data through the trained neural network.
x_test
↓
model.predict()
↓
Predictions
What Does predict() Return?
Our model has 10 output neurons because MNIST contains digits from 0 to 9.
0 1 2 3 4 5 6 7 8 9
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
Probability for each digit
For one image, the model might return:
[
0.01,
0.01,
0.02,
0.01,
0.01,
0.02,
0.01,
0.89,
0.01,
0.01
]
Each number represents how strongly the model associates the image with that class.
Find the Highest Probability
We normally select the class with the highest probability.
import numpy as np
predicted_digit = np.argmax(
predictions[0]
)
print("Predicted Digit:", predicted_digit)
np.argmax() returns the position of the
largest value.
[
0.01, 0.01, 0.02, 0.01,
0.01, 0.02, 0.01, 0.89,
0.01, 0.01
]
Largest value = 0.89
Position = 7
Prediction = 7
Prediction vs Actual Answer
Because MNIST provides the correct labels, we can compare the prediction with the actual answer.
predicted_digit = np.argmax(
predictions[0]
)
actual_digit = y_test[0]
print("Predicted:", predicted_digit)
print("Actual:", actual_digit)
Example:
Predicted: 7
Actual: 7
Correct ✓
Another example:
Predicted: 3
Actual: 7
Incorrect ✗
Predict One Image
Suppose we want to predict only the first test image.
image = x_test[0]
prediction = model.predict(
np.expand_dims(image, axis=0)
)
predicted_digit = np.argmax(
prediction[0]
)
print("Predicted:", predicted_digit)
Why do we use np.expand_dims()?
Single image
(28, 28)
↓
Add batch dimension
(1, 28, 28)
The model expects a batch of inputs, even when we want to predict only one image.
Predict Multiple Images
predictions = model.predict(
x_test[:10]
)
Here we are asking the model to predict the first 10 test images.
10 Images
↓
Neural Network
↓
10 Prediction Results
We can find the predicted class for each image.
predicted_digits = np.argmax(
predictions,
axis=1
)
print(predicted_digits)
Compare Multiple Predictions
predictions = model.predict(
x_test[:10]
)
predicted_digits = np.argmax(
predictions,
axis=1
)
print("Predicted:")
print(predicted_digits)
print("Actual:")
print(y_test[:10])
You might get something like:
Predicted:
[7 2 1 0 4 1 4 9 5 9]
Actual:
[7 2 1 0 4 1 4 9 5 9]
In this example, all ten predictions are correct.
Find Incorrect Predictions
We can compare the predicted labels with the actual labels.
predicted_digits = np.argmax(
predictions,
axis=1
)
incorrect = (
predicted_digits != y_test[:10]
)
print(incorrect)
A value of True means the prediction was
incorrect.
Predicted:
[7 2 3 0 4]
Actual:
[7 2 1 0 4]
Incorrect:
[False False True False False]
The third image was predicted incorrectly.
Prediction Confidence
The output probabilities can also show how strongly the model favors its prediction.
prediction = model.predict(
np.expand_dims(x_test[0], axis=0)
)
predicted_digit = np.argmax(
prediction[0]
)
confidence = np.max(
prediction[0]
)
print("Predicted:", predicted_digit)
print("Confidence:", confidence)
Example:
Predicted: 7
Confidence: 0.89
We can display the value as a percentage:
confidence_percentage = confidence * 100
print(
f"Confidence: {confidence_percentage:.2f}%"
)
Result:
Confidence: 89.00%
Important: Confidence Is Not Guaranteed Correctness
A prediction with 99% model confidence does not guarantee that the prediction is actually correct.
Prediction = 7
Confidence = 99%
↓
The model is very confident
↓
But the prediction
can still be wrong
The confidence is based on the model's output probabilities, not a guarantee of truth.
Predicting a Completely New Image
In a real application, we may receive an image that was not part of MNIST.
New User Image
↓
Resize to 28 × 28
↓
Convert to grayscale
↓
Normalize pixels
↓
Add batch dimension
↓
model.predict()
↓
Predicted digit
The important part is that the new image must be prepared in the same way as the training data.
Why Preprocessing Must Match
Suppose the training images were normalized:
x_train = x_train / 255.0
Then a new image should also be normalized.
new_image = new_image / 255.0
If the training data and new input are represented very differently, the model may produce poor predictions.
Complete Prediction Code
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.Input(
shape=(28, 28)
),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(
128,
activation="relu"
),
tf.keras.layers.Dropout(0.2),
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
)
# Predict one image
image = x_test[0]
prediction = model.predict(
np.expand_dims(image, axis=0)
)
# Get predicted class
predicted_digit = np.argmax(
prediction[0]
)
# Get confidence
confidence = np.max(
prediction[0]
)
# Actual label
actual_digit = y_test[0]
print("Actual:", actual_digit)
print("Predicted:", predicted_digit)
print(
f"Confidence: {confidence * 100:.2f}%"
)
Understand the Complete Prediction Flow
Image
↓
Preprocessing
↓
28 × 28 pixels
↓
Normalize
↓
Add batch dimension
↓
model.predict()
↓
10 probabilities
↓
np.argmax()
↓
Predicted digit
For example:
Input Image
↓
Handwritten "7"
↓
Model
↓
[0.01, 0.01, 0.02, 0.01,
0.01, 0.02, 0.01, 0.89,
0.01, 0.01]
↓
Highest probability
↓
7
Prediction Does Not Change the Model
This is an important difference between training and prediction.
Training
model.fit()
↓
Weights change
Prediction
model.predict()
↓
Weights do NOT change
During prediction, the trained model is simply used to produce an output.
Save the Trained Model
We do not normally want to train the model every time we need a prediction. After training, we can save it.
model.save("digit_model.keras")
Later, we can load the saved model:
model = tf.keras.models.load_model(
"digit_model.keras"
)
Then we can immediately make predictions.
prediction = model.predict(
new_image
)
Real-World Prediction Flow
User
↓
Uploads Image
↓
Application
↓
Preprocess Image
↓
Load Trained Model
↓
model.predict()
↓
Get Prediction
↓
Show Result to User
This is how a trained deep learning model can become part of a real application.
Training vs Evaluation vs Prediction
model.fit()
↓
Train the model
↓
Weights change
model.evaluate()
↓
Measure performance
↓
Weights do not change
model.predict()
↓
Generate predictions
↓
Weights do not change
Keeping these three operations separate makes the complete machine learning workflow much easier to understand.
Complete Deep Learning Workflow
1. Choose Dataset
↓
2. Prepare Data
↓
3. Build Neural Network
↓
4. Train Model
↓
5. Evaluate Model
↓
6. Improve Model
↓
7. Train Again
↓
8. Evaluate Again
↓
9. Make Predictions
↓
10. Use Model in Application
At this point, we have gone from raw data all the way to a model that can make predictions.
A trained model becomes useful when it can make predictions on new data.
The model.predict() method sends new input
through the trained network and returns the model's output
probabilities. For classification, we commonly use
np.argmax() to select the class with the
highest probability. The new input must be preprocessed in
the same way as the training data.
Quick Check
model.predict().
To find the position of the largest prediction probability and use that position as the predicted class.
No. Prediction uses the learned weights; it does not train the model.