导入TensorFlow时cudaGetDevice()失败及Keras导入报错求助
cudaGetDevice() Failed Error with TensorFlow GPU & Keras Hey there, sorry to hear you're stuck with this frustrating error when importing TensorFlow GPU and Keras—let's walk through the most common fixes, even without the full error log (this issue almost always traces back to a handful of configuration mismatches or setup oversights):
Double-check version compatibility between TensorFlow, CUDA, and cuDNN
This is the #1 culprit. TensorFlow GPU has strict, non-negotiable version requirements for CUDA and cuDNN. For example, TensorFlow 2.15 needs CUDA 12.2 and cuDNN 8.9, while TensorFlow 2.10 relies on CUDA 11.2. Even a tiny mismatch (like using CUDA 12.3 with TF 2.15) can break GPU detection entirely. Confirm your installed versions line up perfectly with TensorFlow's official compatibility guidelines.Validate your GPU is visible to your system
Open a command prompt/terminal and runnvidia-smi. If this command fails or doesn't list your GPU, your NVIDIA drivers are either missing, outdated, or incompatible. Install the latest Game Ready Driver (for consumer GPUs) or Data Center Driver (for enterprise GPUs) that matches your CUDA version, then restart your machine.Ensure environment variables are properly configured
TensorFlow needs explicit paths to find CUDA and cuDNN files:- Add
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.X\bin(replace X.X with your CUDA version number) to your systemPATH - Set the
CUDA_PATHenvironment variable toC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.X - For cuDNN, copy its
bin,include, andlibfolders into the corresponding directories in your CUDA installation (or add the cuDNNbinpath directly to yourPATH)
Don't forget to restart your terminal/IDE after updating variables—this is a super easy step to skip that causes tons of headaches.
- Add
Test GPU detection with a minimal script
Before messing with Keras, run this simple code to isolate the issue to TensorFlow itself:import tensorflow as tf print("TensorFlow version:", tf.__version__) print("Available GPUs:", tf.config.list_physical_devices('GPU'))If the GPU list is empty or you get the same
cudaGetDevice()error, you know the problem is with TensorFlow's GPU setup, not Keras.Clean up conflicting installations
If you have both CPU and GPU versions of TensorFlow installed, or multiple CUDA versions on your system, conflicts are inevitable. Uninstall all TensorFlow instances first (pip uninstall tensorflow tensorflow-gpu), then reinstall the exact GPU version you need. Also, make sure only one CUDA version's path is active in your systemPATH.Confirm your GPU supports TensorFlow GPU
TensorFlow GPU requires an NVIDIA GPU with compute capability 3.5 or higher. You can look up your GPU's compute capability on NVIDIA's official site—if your GPU is older than that, it won't work with TensorFlow GPU, and you'll need to switch to the CPU version.
If you can share the full error log later, we can narrow this down even further, but these steps should resolve most cases of the cudaGetDevice() failure.
内容的提问来源于stack exchange,提问作者Ahmed Lahlou Mimi

