Keras
test_on_batch
predict_on_batch
machine learning
neural networks

Keras difference between test_on_batch and predict_on_batch

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

test_on_batch and predict_on_batch both run a forward pass on one batch, but they answer different questions. predict_on_batch returns model outputs, while test_on_batch evaluates the batch against ground-truth labels and returns loss and any configured metrics. If you remember that one is for inference and the other is for evaluation, the distinction becomes straightforward.

What predict_on_batch Does

predict_on_batch(x) takes input features and returns the model's predictions for that batch.

It does not require labels, and it does not compute loss or metrics.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(1, input_shape=(2,))
5])
6
7x = tf.constant([[1.0, 2.0], [3.0, 4.0]])
8preds = model.predict_on_batch(x)
9print(preds)

This is useful when you just want outputs such as probabilities, logits, or regression values.

What test_on_batch Does

test_on_batch(x, y) evaluates the model on one batch using the loss and metrics defined when the model was compiled.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(1, input_shape=(2,))
5])
6model.compile(optimizer="adam", loss="mse", metrics=["mae"])
7
8x = tf.constant([[1.0, 2.0], [3.0, 4.0]])
9y = tf.constant([[1.0], [2.0]])
10
11result = model.test_on_batch(x, y)
12print(result)

If metrics are configured, the returned value usually contains:

  • loss
  • metric values such as accuracy or MAE

So test_on_batch is not mainly about predictions. It is about evaluating how good the predictions are on that batch.

Side-by-Side Mental Model

A practical way to think about them:

  • 'predict_on_batch: "What did the model output for this input?"'
  • 'test_on_batch: "How well did the model perform on this input-label pair?"'

Both do a forward pass. The difference is what happens after the forward pass.

predict_on_batch stops at the raw outputs.

test_on_batch continues by comparing the outputs to labels and computing loss and metrics.

Inputs Required

Another simple distinction is the function signature.

For prediction:

  • inputs only

For testing:

  • inputs plus targets

If you do not have labels, test_on_batch is the wrong tool.

test_on_batch Is Not Training

People sometimes confuse test_on_batch with train_on_batch. They are not the same.

  • 'train_on_batch updates weights'
  • 'test_on_batch does not update weights'
  • 'predict_on_batch does not update weights either'

So if the question is whether the model learns from test_on_batch, the answer is no. It measures; it does not optimize.

Example with Classification Output

A classification example makes the distinction more concrete.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(2, activation="softmax", input_shape=(3,))
5])
6model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
7
8x = tf.constant([[0.1, 0.2, 0.3], [0.8, 0.1, 0.1]])
9y = tf.constant([1, 0])
10
11predictions = model.predict_on_batch(x)
12evaluation = model.test_on_batch(x, y)
13
14print("predictions:", predictions)
15print("evaluation:", evaluation)

Here predictions is a matrix of class probabilities, while evaluation is a compact numeric summary such as loss and accuracy.

When to Use Each One

Use predict_on_batch when:

  • you need outputs for serving or inspection
  • you want per-batch inference in a custom loop
  • you do not have labels available

Use test_on_batch when:

  • you want quick batch-level evaluation
  • labels are available
  • you care about loss and metrics, not just raw outputs

Common Pitfalls

A common mistake is expecting predict_on_batch to report accuracy or loss. It does not.

Another mistake is calling test_on_batch without compiling the model with a loss function and any desired metrics first.

People also often confuse test_on_batch with train_on_batch and assume it updates weights. It does not.

Finally, if you want dataset-level evaluation rather than one-batch evaluation, model.evaluate(...) is usually the higher-level API.

Summary

  • 'predict_on_batch returns model outputs for one batch and does not require labels'
  • 'test_on_batch evaluates one batch against labels and returns loss and metrics'
  • Both run a forward pass, but only test_on_batch compares predictions with targets
  • Neither predict_on_batch nor test_on_batch updates model weights
  • Use predict_on_batch for inference and test_on_batch for evaluation
  • If you need whole-dataset evaluation, prefer evaluate over repeated manual batch calls when practical

Course illustration
Course illustration

All Rights Reserved.