tensorflow获取可用运算设备(CPU,GPU)

正常来说,运行下面两句会打印tensorflow能用的CPU和GPU

1
2
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

结果发现只有CPU可用,查阅资料后发现可能跟tensorflow版本有关,在终端运行pip3 list查看安装的包

1
pip3 list

发现有两个tensorflow,一个是tensorflow,还有一个是tensorflow-gpu.卸载无gpu版本的tensorflow

1
pip3 uninstall tensorflow

重新运行最开始两句代码,结果报错:

1
ImportError: No module named 'tensorflow.python'

卸载tensorflow-gpu后重新安装

1
2
pip3 uninstall tensorflow-gpu
pip3 install tensorflow-gpu==1.4.0

重新运行最开始两句代码,成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 13177083330855175469
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 10968950375
locality {
bus_id: 1
}
incarnation: 6161624703599064583
physical_device_desc: "device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:00:08.0, compute capability: 6.1"
]

总结

最开始存在两个tensorflow包:无gpu版本的tensorflow和gpu版本的tensorflow-gpu,可能是默认使用了无gpu版的tensorflow,所以可用设备中没有GPU

0%