C.W.K.
Stream
Lesson 07 of 08 · published

model.evaluate() and model.predict()

~8 min · training

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Two methods, two questions

Training is done; now you ask two different things of the model. model.evaluate(x_test, y_test) asks how good is it — it runs the same loss and metrics you compiled with over labeled data and returns the numbers. model.predict(x) asks what does it think — it takes unlabeled input and returns raw output. You evaluate when you have ground truth to score against; you predict when you're actually using the model in the wild.

Both run in inference mode — dropout is off, batch-norm uses its running statistics, and nothing updates the weights. That's why a model can look worse during training (dropout is sabotaging it on purpose) yet score higher at evaluate time.

From probabilities to a decision

predict() hands back a probability (or logit) per class, shaped (num_samples, num_classes) — not a label. To get the predicted class you take the argmax along the class axis, as the Code section shows. Note keras.ops.argmax, not NumPy's: it's the backend-agnostic op, so the same line works whether you're on TensorFlow, PyTorch, or JAX. The old predict_classes shortcut is gone — argmax is the explicit, portable replacement. And because predict() batches internally, you can throw a dataset far larger than memory at it without blowing up.

Code

evaluate(), predict(), and argmax to class labels·python
# Evaluate on test data
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")

# Get predictions (probabilities)
predictions = model.predict(x_test)
# predictions.shape = (num_samples, num_classes)

# Convert probabilities to class labels
predicted_classes = keras.ops.argmax(predictions, axis=1)

External links

Exercise

Train MNIST. Run model.evaluate on test set. Then model.predict on 5 random test images, argmax the output, compare to true labels. Verify accuracy matches evaluate's number on those 5.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.