检查方法
import tensorflow as tf gpus = tf.config.experimental.list_physical_devices(device_type='GPU') cpus = tf.config.experimental.list_physical_devices(device_type='CPU') print(gpus, cpus)
如果运行结果为
[] [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
那么恭喜,说明你的tf仅仅是检测不到GPU设备,CPU设备还是能够检查到的。
但是,好像从某个版本开始,tf就不区分CPU版和GPU版了,所以这个问题通过 install tensorflow-gpu 解决的方法已经过时了。
解决方法
整个解决思路也是看了别的大佬的文章整理的1 2,这里我直接给出结论:大概率是tensorflow的版本安装得太高了,将版本降到2.10.0,并且将CUDA和cudnn的版本对齐(高了的话记得先卸载干净),我将我对齐的版本贴上。
版本列表
这里我只给出对应版本和查看方法,具体安装教程直接找有很多,我就不赘述了。
tensorflow==2.10.0
用命令行指令查看版本
pip show tensorflow
CUDA==11.2
nvcc -V
cudnn==8.1.1
到CUDA安装路径下的include文件夹中(以我的举例,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\include),找到cudnn_version.h文件,右键 -> 编辑 找到以下三行查看版本
结果
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
import os os.environ["CUDA_VISIBLE_DEVICES"] = "0"
训练速度直接翻了三倍