Keras
TensorFlow
GPU
Machine Learning
Deep Learning

How to disable GPU in keras with tensorflow?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In this article, we'll explore the process of disabling the use of GPU in Keras when using TensorFlow as the backend. This might be particularly useful in environments where GPU resources are either not available, unnecessarily powerful for the task at hand, or you want to ensure that computations are run on the CPU instead for consistency or other reasons.

Understanding GPU Usage in Keras with TensorFlow

Keras, when using TensorFlow as its backend, automatically attempts to utilize available GPUs to accelerate computation. GPUs are incredibly efficient for handling large-scale data processing and machine learning models due to their parallel processing capabilities. However, there are scenarios where you might want to restrict operations to the CPU, such as:

  • Debugging purposes, where reproducing results consistently across different machines is crucial.
  • Running in an environment without GPU support.
  • Reducing costs associated with cloud-based GPU resources.

Steps to Disable GPU in Keras with TensorFlow

Method 1: Setting Environment Variables

The simplest way to instruct TensorFlow to ignore GPUs is by setting environment variables before you even import TensorFlow. This method involves:

  • Step 1: Setting the environment variable `CUDA_VISIBLE_DEVICES` to an empty string. This tells TensorFlow to ignore all available GPUs.
  • Step 2: Import TensorFlow and Keras after setting the environment variable.
  • Computation Performance: Running deep learning models on a CPU instead of a GPU typically results in slower training and inference times. It's important to consider this tradeoff when performing significant data processing and modeling tasks.
  • Verify Device Utilization: Once configuration changes are applied, verify the desired setup using TensorFlow utilities such as `tf.config.list_physical_devices()` and `tf.test.is_gpu_available()`.
  • Environment Persistence: Remember that setting the environment variable `CUDA_VISIBLE_DEVICES` is effective only during the lifetime of the application. Resetting this is just a matter of rerunning the script without setting the variable, or setting it to a specific device identifier.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.