Keras with TensorFlow backend not using GPU
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
When Keras with a TensorFlow backend runs on CPU instead of GPU, the cause is almost always a missing or misconfigured component in the CUDA/cuDNN stack. TensorFlow requires a specific combination of NVIDIA drivers, CUDA toolkit, and cuDNN library versions to detect and use the GPU. The most common fixes are installing tensorflow-gpu (for TensorFlow 1.x), installing the correct CUDA/cuDNN versions, and verifying the GPU is visible with tf.config.list_physical_devices('GPU').
Checking GPU Availability
If this prints no GPU devices, TensorFlow is not detecting your GPU. The issue is in the driver/CUDA/cuDNN stack.
Fix 1: Install the Correct TensorFlow Package
Starting with TensorFlow 2.1, the tensorflow pip package includes GPU support by default. For TensorFlow 1.x, you must install tensorflow-gpu separately. If you have the CPU-only tensorflow package installed alongside tensorflow-gpu, remove the CPU-only version to avoid conflicts.
Fix 2: Match CUDA and cuDNN Versions
TensorFlow requires specific CUDA and cuDNN versions. Mismatched versions are the most common cause of GPU detection failure.
| TensorFlow | CUDA | cuDNN |
| 2.15 | 12.2 | 8.9 |
| 2.14 | 11.8 | 8.7 |
| 2.13 | 11.8 | 8.6 |
| 2.12 | 11.8 | 8.6 |
| 2.10-2.11 | 11.2 | 8.1 |
| 1.15 | 10.0 | 7.4 |
Check the official TensorFlow build configurations page for the exact version matrix.
Fix 3: Set CUDA Environment Variables
TensorFlow searches LD_LIBRARY_PATH for CUDA and cuDNN shared libraries. If the paths are wrong, it falls back to CPU silently.
Fix 4: Use conda for Automatic CUDA Management
conda installs the correct CUDA and cuDNN libraries into the environment, avoiding system-wide version conflicts. This is the easiest path for most users.
Fix 5: GPU Memory Configuration
By default, TensorFlow allocates all available GPU memory. If another process already holds the GPU memory, TensorFlow may fail to initialize the GPU. Enabling memory growth allocates memory incrementally.
Fix 6: Verify GPU Usage During Training
tf.debugging.set_log_device_placement(True) prints which device (CPU or GPU) each operation runs on. This confirms whether your model is actually using the GPU during training.
Docker with GPU Support
Docker containers need --gpus all and the nvidia-container-toolkit to pass GPU access into the container. The official TensorFlow GPU image includes pre-configured CUDA and cuDNN.
Windows-Specific Issues
On Windows, CUDA DLLs must be on the PATH. The CUDA installer usually adds them, but cuDNN files (cudnn64_8.dll) must be manually copied to the CUDA bin directory or added to PATH.
Common Pitfalls
- Installing tensorflow instead of tensorflow-gpu for TF 1.x: TensorFlow 1.x has separate CPU and GPU packages. The CPU-only
tensorflowpackage cannot use the GPU regardless of your CUDA setup. Installtensorflow-gpufor TF 1.x. - CUDA/cuDNN version mismatch: TensorFlow 2.14 needs CUDA 11.8, not CUDA 12.x. Installing the latest CUDA does not work — you must match the exact version from the compatibility matrix.
- Missing cuDNN: CUDA alone is not enough. TensorFlow requires cuDNN (the deep neural network library) in addition to the CUDA toolkit. Install cuDNN from the NVIDIA developer site and place the files in the CUDA directory.
- Another process holding GPU memory: If PyTorch, another TensorFlow session, or a game is using all GPU memory, TensorFlow cannot allocate and falls back to CPU. Use
nvidia-smito check GPU memory usage and kill competing processes. - Virtual environment not seeing system CUDA: Python virtual environments may not inherit
LD_LIBRARY_PATH. Set the environment variable inside the activated environment or use conda which bundles its own CUDA libraries.
Summary
- Check GPU visibility with
tf.config.list_physical_devices('GPU')first - For TensorFlow 1.x, install
tensorflow-gpu(TF 2.x includes GPU support in the main package) - Match CUDA and cuDNN versions exactly to your TensorFlow version
- Set
LD_LIBRARY_PATH(Linux) orPATH(Windows) to include CUDA directories - Use conda for automatic CUDA/cuDNN management without version conflicts
- Enable
tf.debugging.set_log_device_placement(True)to verify operations run on GPU - Use
tf.config.experimental.set_memory_growth(gpu, True)to prevent memory allocation failures
Related reading
- Keras with Tensorflow Use memory as it's needed ResourceExhaustedError
- Keras/Tensorflow Combined \`Loss\` function for single output
- Keras/TF Time Distributed CNNLSTM for visual recognition
- K.gradientsloss, input_img0 return None. Keras CNN visualization with tensorflow backend
- Keras,models.add missing 1 required positional argument ''layer''
- KerasRegressor Coefficient of Determination R2 `Score`
- KL Divergence for two probability distributions in PyTorch
- Kubernetes GPU support how to enable?
.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.