我如何知道我是否正在导入tensorflow或tensorflow-gpu?

时间:2018-06-02 18:58:58

标签: python tensorflow

我开始使用tensorflow cpu版本,但后来我安装了tensorflow-gpu以便更快地处理,但我不确定哪一个当前正在工作。如何确认当前运行的版本确实是tensorflow-gpu?

2 个答案:

答案 0 :(得分:1)

AFAIK你不能直接从python告诉你是从tensorflow(仅限CPU)还是tensorflow-gpu包导入tensorflow。

但是,您可以询问有关它所知道的设备的张量流:

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

如果有device_type类型GPU的设备,那么您肯定是从tensorflow-gpu导入的。

答案 1 :(得分:0)

在上面的P-Gns代码的基础上,使某些东西更自动化:

from tensorflow.python.client.device_lib import list_local_devices
ss = "\n".join([str(s) for s in list_local_devices()]
numGpus = ss.count("device:GPU")
print("There are {} GPUs present".format(numGpus))

它获取本地设备列表,将其属性转换为大字符串,并计算其中的GPU数量。因此,将以上文本复制并粘贴到我的python中会得到:

>>> from tensorflow.python.client.device_lib import list_local_devices
>>> ss = "\n".join([str(s) for s in list_local_devices()])
2018-06-27 09:30:43.309703: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1423] Adding visible gpu devices: 0
2018-06-27 09:30:43.309796: I tensorflow/core/common_runtime/gpu/gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-06-27 09:30:43.309836: I tensorflow/core/common_runtime/gpu/gpu_device.cc:917]      0
2018-06-27 09:30:43.309869: I tensorflow/core/common_runtime/gpu/gpu_device.cc:930] 0:   N
2018-06-27 09:30:43.310186: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Created TensorFlow device (/device:GPU:0 with 5185 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1)
>>> print(ss)
name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 4228992220605196898

name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 5436997632
locality {
  bus_id: 1
  links {
  }
}
incarnation: 6685330293620217138
physical_device_desc: "device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1"

>>> numGpus = ss.count("device:GPU")
>>> print("There are {} GPUs present".format(numGpus))
There are 1 GPUs present
>>>
相关问题