Keras not using full CPU cores for training
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 powerful and widely-used open-source neural network library, is part of the TensorFlow ecosystem and provides an easy-to-use interface for deep learning. However, for those working on performance-critical tasks, one might notice that Keras does not always utilize full CPU cores for training, which could be a bottleneck for speed-intensive operations. This article explores the reasons behind this behavior and offers insights into how to potentially optimize CPU usage during training.
Understanding Keras and Backend Configurations
Keras can run on multiple backends, including TensorFlow, Theano, and CNTK. Each of these backends has different ways of managing resources, and TensorFlow, being the default backend, employs its own strategies regarding CPU utilization.
TensorFlow and Threading
TensorFlow is designed to handle computations efficiently across different hardware setups. It uses a thread pool architecture to manage computations on CPUs but does not always maximize CPU usage due to the following reasons:
- Default Thread Configuration: TensorFlow does not, by default, use all available CPU cores. This design choice avoids overwhelming the system resources, which can be critical when other processes need to run concurrently.
- Global Interpreter Lock (GIL): For Python-based computations, the GIL can impact thread performance. Since Keras is a high-level API running on top of Python, this may result in underutilization of CPU resources when executing Python-bound operations.
- Non-parallel Code: Certain parts of the pipeline, such as data preprocessing or loading, might not be parallelized well by default. The computation could be CPU-bound in such scenarios, causing a bottleneck.
Configuring Keras to Maximize CPU Usage
To maximize CPU usage, you can optimize TensorFlow settings and Keras code implementations:
- Setting Number of Threads: Override the default TensorFlow thread configuration by setting environmental variables such as `OMP_NUM_THREADS`. Example:
- Increasing the number of threads improves CPU usage and decreases training time significantly.
- Data pipeline optimization plays a significant role in achieving high CPU utilization.
- CPU vs. GPU: While CPU optimization is essential, GPUs offer more significant speed-ups for deep learning tasks due to their specialized architecture for parallel processing.
- Overhead Management: Running too many threads might introduce overhead and potentially degrade performance if cores are over-saturated, emphasizing the need for balanced configuration.
- Monitoring and Profiling: Tools like TensorBoard and NVDIA's Nsight Systems can help in profiling how resources are utilized, guiding necessary adjustments.
Related reading
- Keras not using full CPU cores for training
- Keras occupies an indefinitely increasing amount of memory for each epoch
- Keras or Tensorflow function to draw a 3D diagram of a neural network structure?
- Keras real amount of GPU memory used
- keras predict always output same value in multi-classification
- Keras predict getting incorrect shape?
- Keras predict not returning inside celery task
- Keras Realtime Augmentation adding Noise and Contrast

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 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.