Tensorflow Queues - Switching between train and validation data
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In TensorFlow 1.x, queue-based input pipelines were a common way to feed data into a training graph. If you needed to switch between training data and validation data, the clean approach was to build separate input sources and select between them explicitly instead of trying to rewire one queue in place.
Why Switching Is Needed
Training and validation typically have different behavior:
- training input is often shuffled and repeated
- validation input is usually deterministic
- augmentation may apply only to training data
That means a single queue configuration is rarely appropriate for both. The safest design is to create one queue pipeline for training, one for validation, and switch at the dequeue point.
Minimal Queue-Based Example
The following tf.compat.v1 example shows the idea without extra file readers. Two queues are filled with different values, and a boolean placeholder selects which one to read from.
The first two reads come from the training queue. The last read switches to the validation queue because use_validation is True.
Extending The Pattern To Real Training
In a real training graph, each queue would usually contain feature tensors and labels rather than integers. You might also use RandomShuffleQueue for training and FIFOQueue for validation.
Once the selected tensors are wired into the model, the rest of the graph does not need to know whether the batch came from training or validation data.
That separation is important. It keeps data-source switching outside the model logic.
Queue Runners And Coordination
Many older TensorFlow pipelines also used queue runners. If your input pipeline reads files asynchronously, you need to start the background threads before calling sess.run on the model.
If you forget to start queue runners, your session will block while waiting for input that never arrives.
Better Architecture For Evaluation
Even in TensorFlow 1.x, it was often cleaner to keep training and validation in separate loops:
- run several training steps with
use_validation=False - run evaluation steps with
use_validation=True - aggregate metrics separately
That avoids mixing shuffling, dropout, and evaluation state in the same step. Validation should be predictable and reproducible.
Historical Context: Why Many Teams Moved Away From Queues
Queue pipelines worked, but they were difficult to debug and easy to deadlock. Later TensorFlow versions made tf.data the preferred input mechanism because it expresses switching and batching more cleanly.
For example, a modern equivalent would typically use two datasets and separate iterators or a reinitializable iterator. The conceptual lesson is unchanged: training and validation are different streams, so model them as different inputs.
Common Pitfalls
A common mistake is trying to reuse the exact same queue for both training and validation by mutating file lists or enqueue operations during a running session. That tends to create nondeterministic behavior and debugging pain.
Another issue is using shuffled validation input. That is not always wrong, but it makes evaluation harder to compare across runs and can hide data accounting bugs.
Queue starvation is also common. If the enqueue threads are not running, or the validation queue is never filled before switching, the graph can hang on a dequeue call.
Finally, remember that many queue-based examples on the internet target TensorFlow 1.x graph mode. If you are maintaining old code, the pattern above is still useful. If you are starting fresh, a dataset-based pipeline is usually the better design.
Summary
- In queue-based TensorFlow input pipelines, keep training and validation as separate queues.
- Switch between them at dequeue time with a flag such as
tf.cond. - Use shuffling for training input and deterministic ordering for validation when possible.
- Start queue runners when your pipeline depends on background enqueue threads.
- For new projects, prefer dataset-based input pipelines, but the same separation-of-streams idea still applies.
Related reading
- Tensorflow r1.0 could not a find a version that satisfies the requirement tensorflow
- Tensorflow ran out of memory trying to allocate 3.90GiB. The caller indicates that this is not a failure
- TensorFlow random_shuffle_queue is closed and has insufficient elements
- TensorFlow read a frozen model, add operations, then save to a new frozen model
- Tensorflow read images with labels
- tensorflow record with float numpy array
- Tensorflow Relu Misunderstanding
- TensorFlow Remember LSTM state for next batch stateful LSTM
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.