How to define max_queue_size, workers and use_multiprocessing in keras fit_generator?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Keras, a widely-used high-level neural networks API running on top of TensorFlow, provides several methods for training models, including fit_generator(). This article delves into the parameters max_queue_size, workers, and use_multiprocessing within fit_generator(), explaining their significance and providing guidance on how to set them effectively.
Understanding fit_generator()
fit_generator() is a method used to train models in Keras with data that's generated in real-time by a Python generator. This is particularly useful for large datasets that do not fit into memory, as fit_generator() allows them to be loaded and processed in batches.
Here's how the method looks in practice:
Key Parameters
1. max_queue_size
max_queue_size sets the maximum size of the generator queue. The generator queue stores batch data ready for training. By correctly defining max_queue_size, you can balance memory consumption and performance:
- Purpose: Controls how many batches the generator can queue up at a time.
- Default Value: 10.
- Performance: A larger queue size can improve data throughput and model training time by preloading data.
- Memory Usage: A higher
max_queue_sizemay increase memory usage since more data is being held in memory.
Scenario: If your data generation is slow (e.g., involving complex image preprocessing or augmentation), increasing max_queue_size can be beneficial. However, ensure that your system has enough memory to accommodate this.
2. workers
workers defines how many processor threads should be used to fetch data. More workers can lead to faster data fetching but at the potential cost of increased processor usage.
- Purpose: Sets the number of processes to spin up for data preparation.
- Default Value: 1.
- Performance: Introducing more workers can significantly accelerate data loading times, especially on multi-core systems.
- Considerations: Adjust the number of workers based on CPU capabilities, as each worker runs on a separate CPU core.
Scenario: If your data fetching is the bottleneck (e.g., due to slow disk access or intensive preprocessing), increasing the number of workers can enhance performance, assuming you have CPU cores to spare.
3. use_multiprocessing
use_multiprocessing determines whether to use process-based threading or thread-based threading for data generation. Multiprocessing can be more efficient due to the Global Interpreter Lock (GIL) in Python.
- Purpose: Switches between using threading (
False) and multiprocessing (True). - Default Value: False.
- Performance: Multiprocessing can be faster because it spawns separate memory processes, avoiding Python’s GIL; however, it generally has more overhead than threading due to inter-process communication costs.
Scenario: Use use_multiprocessing=True if your processing logic is CPU-bound. This is especially effective for complex data augmentation solutions but may require adjustments to ensure thread-safety and avoid conflicts when accessing shared resources.
Example Setup
Here is an example of how to set these parameters:
Table Summary
| Parameter | Purpose | Default Value | Key Considerations |
max_queue_size | Max size of the generator queue | 10 | Impact on memory and throughput |
workers | Number of processor threads for data fetching | 1 | Benefit from multi-core CPUs; consider processor limitations |
use_multiprocessing | Choose multiprocessing instead of multithreading | False | Good for CPU-bound tasks; consider inter-process communication overhead |
Additional Considerations
When configuring these parameters, it’s essential to conduct experiments and tailor settings to your specific hardware and data processing requirements. Environmental factors such as CPU count, memory capacity, disk I/O performance, and the complexity of data preprocessing tasks all influence optimal settings.
Additionally, consider debugging techniques such as profiling your data generation pipeline to identify bottlenecks. This understanding can guide you in fine-tuning max_queue_size, workers, and use_multiprocessing efficiently.
By leveraging fit_generator() appropriately, you can significantly enhance the efficiency and speed of model training, particularly when handling real-time data pipelines and large-scale datasets.
Related reading
- How to define weight decay for individual layers in TensorFlow?
- How to deploy and serve prediction using TensorFlow from API?
- How To Determine the 'filter' Parameter in the Keras Conv2D Function
- How to disable dropout while prediction in keras?
- How to define object in array in Mongoose schema correctly with 2d geo index
- How to delete a queue in rabbit mq
- How to disable GPU in keras with tensorflow?
- How to disable keras warnings?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.