TensorFlow
machine learning
validation
training
data queues

TensorFlow How can I evaluate a validation data queue multiple times during training?

Master System Design with Codemia

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

Introduction

Validation during training is not a special side channel. It is just another pass over data, and the key requirement is that the validation input can be consumed repeatedly. In modern TensorFlow, the clean solution is to use tf.data.Dataset with model.fit, then control how often validation runs with validation_freq and how many batches are consumed with validation_steps.

Older TensorFlow code often talked about queues. The modern replacement is tf.data. The same principle still applies: if the validation input is finite and you want to evaluate it multiple times, it must either be recreated automatically by Keras each time or defined in a way that supports repeated iteration.

Use validation_data In model.fit

For standard training, pass a validation dataset directly to fit:

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5x_train = tf.random.normal((256, 8))
6y_train = tf.cast(tf.reduce_sum(x_train, axis=1) > 0, tf.float32)
7
8x_val = tf.random.normal((64, 8))
9y_val = tf.cast(tf.reduce_sum(x_val, axis=1) > 0, tf.float32)
10
11train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)
12val_ds = tf.data.Dataset.from_tensor_slices((x_val, y_val)).batch(32)
13
14model = keras.Sequential([
15    layers.Input(shape=(8,)),
16    layers.Dense(16, activation="relu"),
17    layers.Dense(1, activation="sigmoid"),
18])
19
20model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
21model.fit(train_ds, validation_data=val_ds, epochs=5)

Keras will iterate over val_ds at the end of each epoch. You do not need to manually rewind it in the normal case.

Control How Often Validation Runs

If you do not want validation after every epoch, use validation_freq:

python
1model.fit(
2    train_ds,
3    validation_data=val_ds,
4    epochs=10,
5    validation_freq=2,
6)

That evaluates validation data every second epoch. This is useful when validation is expensive or the dataset is large.

Use validation_steps For Repeating Or Infinite Datasets

If your validation dataset uses .repeat() or is otherwise unbounded, Keras needs to know how many batches to evaluate each time:

python
1val_ds = (
2    tf.data.Dataset.from_tensor_slices((x_val, y_val))
3    .batch(16)
4    .repeat()
5)
6
7model.fit(
8    train_ds,
9    validation_data=val_ds,
10    validation_steps=4,
11    epochs=5,
12)

Without validation_steps, validation on an infinite dataset never finishes. That is one of the easiest ways to create a training loop that appears to hang.

When You Need Mid-Epoch Validation

If the real requirement is "evaluate several times during a single epoch," then epoch-level validation is not enough. In that case, write a callback that calls model.evaluate at a chosen batch interval.

python
1class PeriodicValidation(keras.callbacks.Callback):
2    def __init__(self, val_data, every_n_batches=10):
3        super().__init__()
4        self.val_data = val_data
5        self.every_n_batches = every_n_batches
6
7    def on_train_batch_end(self, batch, logs=None):
8        if (batch + 1) % self.every_n_batches == 0:
9            results = self.model.evaluate(self.val_data, verbose=0)
10            print("validation:", results)

That pattern is more flexible, but it is also more expensive, so it should be used deliberately.

Finite Datasets Versus Repeating Datasets

A finite validation dataset is usually simplest because Keras can consume it cleanly at each validation point. Repeating validation datasets are useful when the pipeline is shared or streaming, but they require more explicit control. If the validation loop feels unpredictable, simplify the input pipeline before you optimize its frequency.

Common Pitfalls

  • Treating validation input like a one-shot iterator when it needs to be reusable.
  • Using .repeat() on validation data without setting validation_steps.
  • Expecting validation_freq to mean batches when it actually counts epochs.
  • Calling model.evaluate too often during training and turning validation into the bottleneck.
  • Carrying forward older queue-runner mental models instead of using tf.data.

Summary

  • In modern TensorFlow, repeated validation is usually handled through validation_data in model.fit.
  • Use validation_freq to control how often validation runs across epochs.
  • Use validation_steps when the validation dataset repeats or is unbounded.
  • For mid-epoch validation, use a callback that calls model.evaluate intentionally.
  • Think in terms of reusable datasets, not one-shot queues.

Course illustration
Course illustration

All Rights Reserved.