我已经在我的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 ?


当前回答

好的,首先从终端启动一个ipython shell,然后导入TensorFlow:

$ ipython --pylab
Python 3.6.5 |Anaconda custom (64-bit)| (default, Apr 29 2018, 16:14:56) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
Using matplotlib backend: Qt5Agg

In [1]: import tensorflow as tf

现在,我们可以在控制台中使用以下命令查看GPU内存的使用情况:

# realtime update for every 2s
$ watch -n 2 nvidia-smi

因为我们只导入了TensorFlow,但还没有使用任何GPU,所以使用统计数据将是:

注意GPU内存使用非常少(~ 700MB);有时GPU内存使用甚至可能低至0 MB。


现在,让我们在代码中加载GPU。如tf文档所示,请执行:

In [2]: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

现在,手表的统计数据应该显示一个更新的GPU使用内存如下:

现在观察一下我们在ipython shell中的Python进程是如何使用大约7 GB的GPU内存的。


附注:你可以在代码运行时继续观察这些统计数据,看看随着时间的推移GPU的使用有多激烈。

其他回答

我更喜欢使用nvidia-smi来监控GPU使用情况。如果当你开始你的程序时,它显著上升,这是一个强烈的迹象,表明你的张量流正在使用GPU。

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()

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

除了使用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的最新更新,你可以检查它如下:

tf.test.is_gpu_available( cuda_only=False, min_cuda_compute_capability=None)

如果GPU正在被Tensorflow使用,返回True,否则返回False。

如果你想要设备device_name,可以输入:tf.test.gpu_device_name()。 从这里获取更多细节

这应该会给出Tensorflow可用的设备列表(Py-3.6下):

tf = tf.Session(config=tf.ConfigProto(log_device_placement=True))
tf.list_devices()
# _DeviceAttributes(/job:localhost/replica:0/task:0/device:CPU:0, CPU, 268435456)