Can Keras with Tensorflow backend be forced to use CPU or GPU at will?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Keras, a popular high-level neural networks API, is often used with TensorFlow as its backend due to TensorFlow's extensive capabilities for deep learning. One of the fantastic features of this combination is the ability to leverage computational resources effectively—whether it be a powerful GPU or a CPU. Sometimes, developers may explicitly want to control which computational device Keras should use, based on various constraints or optimization strategies. This article provides a detailed exploration of how one can force Keras to use either a CPU or a GPU.
Why Control CPU/GPU Usage?
Before diving into how you can control the device usage, it is essential to understand why you may need to:
- Resource Availability: Not all hardware configurations have a dedicated GPU. Forcing operations to run on a CPU is necessary for such environments.
- Debugging: Debugging on CPU might be easier due to more predictable execution compared to parallel operations on GPUs.
- Comparison: Running models on both CPU and GPU can help compare performance metrics and manage resource allocation in a shared environment.
- Cost Management: In cloud environments, GPUs can be more expensive than CPUs. Efficiently managing resources can significantly reduce computing costs.
Enabling and Disabling GPU Usage
Controlling Devices with Environment Variables
You can control hardware usage by setting environment variables before importing any TensorFlow or Keras modules.
- Force CPU: Set the
CUDA_VISIBLE_DEVICESenvironment variable to an empty string.Example (in Python):
- Force Specific GPU: If you have multiple GPUs and want to use a specific one, set the variable to the GPU's device ID.Example:
TensorFlow Device Context
For more granular control within the code, use TensorFlow's tf.device context manager to specify CPU or GPU for specific operations.
Example:
Configuring Keras Policy
Keras and TensorFlow also provide global settings that affect device placement.
- Keras Policy: With TensorFlow 2.x and above, you can set global mixed precision policies that determine whether computations are performed with float32 or mixed precision, which can affect device selection implicitly, as GPUs often provide better hardware acceleration for mixed precision.
Key Points Summary
| Action | Environment Variable | Python Context Settings | Use Case Example |
| Force CPU | os.environ['CUDA_VISIBLE_DEVICES'] = '' | tf.device('/CPU:0') | Debugging, no GPU available |
| Force specific GPU | os.environ['CUDA_VISIBLE_DEVICES'] = '0' | tf.device('/GPU:0') | Forcing use of a specific GPU |
| Mixed precision VS32/VS16 choice | N/A | mixed_precision.set_global_policy() | Optimizing GPU performance |
| Disable GPU acceleration warnings | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' | N/A | Cleaner console output |
Additional Considerations
Installation Dependencies
- Ensure the compatible CUDA and cuDNN versions are installed. TensorFlow requires these for GPU acceleration.
- Verify that your TensorFlow installation includes GPU support. You can check this with:
Monitoring and Debugging
Monitor usage to ensure the execution is happening on the desired device:
- TensorFlow Profiler can give insights into which device is being used for which operation.
- The
nvidia-smitool helps monitor GPU usage externally.
Conclusion
Forcing Keras with TensorFlow backend to use CPU or GPU gives you precise control over your model's execution environment. This can lead to more efficient resource utilization, effective debugging, and better performance optimization. Understanding how to manipulate device placement is an essential skill for any deep learning practitioner looking to harness the full power of their available computational resources.

