TensorFlow无法调用GPU求助:已装CUDA但仅识别CPU
问题背景
已安装CUDA编译器nvcc,执行nvcc -V输出:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Mon_Jan__9_17:32:33_CST_2017
Cuda compilation tools, release 8.0, V8.0.60
运行TensorFlow代码:
from tensorflow.python.client import device_lib device_lib.list_local_devices()
出现报错:
2023-02-20 17:16:03.556026: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 16176753119491980911
xla_global_id: -1
]
PyTorch中执行print(torch.cuda.is_available())返回True,使用Windows系统,TensorFlow版本为2.7.0(tensorflow-gpu),需解决TensorFlow无法使用GPU的问题。
解决步骤
1. 明确版本兼容要求
TensorFlow 2.7.0(Windows平台)必须搭配CUDA 11.2和cuDNN 8.1.0,当前安装的CUDA 8.0版本过低,不满足依赖条件,这是核心问题。PyTorch对旧CUDA版本兼容范围更广,所以能正常识别GPU。
2. 安装对应版本的CUDA和cuDNN
- 卸载现有CUDA 8.0:在Windows「设置-应用」中找到
NVIDIA CUDA Toolkit 8.0完成卸载。 - 安装CUDA Toolkit 11.2:下载对应版本安装包,按默认路径完成安装。
- 安装cuDNN 8.1.0:下载对应版本压缩包,解压后将
bin、include、lib三个文件夹复制到CUDA安装目录(默认路径为C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2)的对应文件夹下。
3. 配置系统环境变量
确保以下路径添加到系统环境变量Path中:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\binC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\libnvvp
4. 验证GPU可用性
重启终端或IDE后,运行以下代码验证:
import tensorflow as tf print(tf.test.is_gpu_available()) print(tf.config.list_physical_devices('GPU'))
若返回True并列出GPU设备,说明配置成功。
内容的提问来源于stack exchange,提问作者ponda

