device_lib.list_local_devices()只有一个cpu

原因是: 我的虚拟环境同时安装了tensorflow和tensorflow-gpu,此时默认使用的是cpu版本的tensorflow,所以检测出来的只有cpu。

查看系统是否存在可用gpu cmd下面运行命令:nvidia-smi,结果如下,证明存在可用gpu 切换gpu版本 程序默认使用最后安装的tensorflow版本,我先安装的tensorflow-gpu,后安装的tensorflow,所以默认使用的是tensorflow,卸载tensorflow-gpu: pip uninstall tensorflow 重新安装 pip install tensorflow-gpu==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple (安装要指定版本,不然会可cuda和cudnn不兼容) 运行 from tensorflow.python.client import device_lib print(device_lib.list_local_devices()) 显示:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 8036499544802769569
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 5041356800
locality {
  bus_id: 1
}
incarnation: 8658293469486625633
physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1660 Ti with Max-Q Design, pci bus id: 0000:01:00.0, compute capability: 7.5"
]

成功

经验分享 程序员 微信小程序 职场和发展