1、首先检查当前环境的cpu,gpu设备信息
from tensorflow.python.client import device_lib as _device_lib
local_device_protos = _device_lib.list_local_devices()
devices = [x.name for x in local_device_protos]
for d in devices:
print(d)
/device:CPU:0
博主当前的环境只install了tensorflow,并没有install tensorflow-gpu。以为2.x之后的版本不区分cpu和gpu了,就没有单独再install tensorflow-gpu了。结果根本找不到gpu信息!
2、install tensorflow-gpu:
原本的tensorflow要保留,不能直接uninstall tensorflow!!会直接报错没有tensorflow模块:No module named 'tensorflow'
此处要注意tensorflow-gpu版本!博主原来的tensorflow是2.1.0版本,然后就install了一个2.1.0版本的tensorflow-gpu。如下图,
检查当前gpu是否available
import tensorflow as tf
print(tf.test.is_gpu_available())
False
实测不行,tf.test.is_gpu_available()
输出是False。
tensorflow-gpu版本需要比tensorflow高!且要注意和cuda对应。
3、pip uninstall tensorflow-gpu==2.5.0
同时update 了numpypip install --upgrade numpy
再次输出cpu,gpu设备信息以及gpu是否available:
from tensorflow.python.client import device_lib as _device_lib
local_device_protos = _device_lib.list_local_devices()
devices = [x.name for x in local_device_protos]
for d in devices:
print(d)
print(tensorflow.test.is_gpu_available())
/device:CPU:0
/device:GPU:0
/device:GPU:1
True
大功告成!
标签:lib,install,tensorflow,device,gpu,local,cpu From: https://www.cnblogs.com/juneyiiii/p/17249737.html