我已经在我的ubuntu 16.04中安装了tensorflow,使用的是ubuntu内置的apt cuda安装。

现在我的问题是,我如何测试tensorflow是否真的使用gpu?我有一个gtx 960m gpu。当我导入tensorflow时,这是输出

I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally

这个输出是否足够检查tensorflow是否使用gpu ?


当前回答

除了使用sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)),这是在其他答案和官方TensorFlow文档中列出的,你可以尝试给gpu分配一个计算,看看你是否有错误。

import tensorflow as tf
with tf.device('/gpu:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)

with tf.Session() as sess:
    print (sess.run(c))

在这里

"/cpu:0":您机器的cpu。 "/gpu:0":你机器的gpu,如果你有的话。

如果你有一个gpu并且可以使用它,你会看到结果。否则,您将看到一个带有很长的堆栈跟踪的错误。最后你会得到这样的结果:

无法将设备分配给节点“MatMul”:无法满足显式要求 设备规格'/device:GPU:0'因为没有设备与之匹配 规范是在这个过程中注册的


最近在TF中出现了几个有用的函数:

tf.test。Is_gpu_available表示gpu是否可用 tf.test。Gpu_device_name返回gpu设备名称

你也可以检查会话中可用的设备:

with tf.Session() as sess:
  devices = sess.list_devices()

设备会给你一些类似的东西

[_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:CPU:0, CPU, -1, 4670268618893924978),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 6127825144471676437),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 16148453971365832732),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 10003582050679337480),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 5678397037036584928)

其他回答

如果你正在使用TensorFlow 2.0,你可以使用这个for循环来显示设备:

with tf.compat.v1.Session() as sess:
  devices = sess.list_devices()
devices

除了使用sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)),这是在其他答案和官方TensorFlow文档中列出的,你可以尝试给gpu分配一个计算,看看你是否有错误。

import tensorflow as tf
with tf.device('/gpu:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)

with tf.Session() as sess:
    print (sess.run(c))

在这里

"/cpu:0":您机器的cpu。 "/gpu:0":你机器的gpu,如果你有的话。

如果你有一个gpu并且可以使用它,你会看到结果。否则,您将看到一个带有很长的堆栈跟踪的错误。最后你会得到这样的结果:

无法将设备分配给节点“MatMul”:无法满足显式要求 设备规格'/device:GPU:0'因为没有设备与之匹配 规范是在这个过程中注册的


最近在TF中出现了几个有用的函数:

tf.test。Is_gpu_available表示gpu是否可用 tf.test。Gpu_device_name返回gpu设备名称

你也可以检查会话中可用的设备:

with tf.Session() as sess:
  devices = sess.list_devices()

设备会给你一些类似的东西

[_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:CPU:0, CPU, -1, 4670268618893924978),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 6127825144471676437),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 16148453971365832732),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 10003582050679337480),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 5678397037036584928)

如果你用的是张量流2。x使用:

sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

Tensorflow 2.0

会话在2.0中不再使用。相反,可以使用tf.test.is_gpu_available:

import tensorflow as tf

assert tf.test.is_gpu_available()
assert tf.test.is_built_with_cuda()

如果出现错误,则需要检查安装。

下面还将返回GPU设备的名称。

import tensorflow as tf
tf.test.gpu_device_name()