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


当前回答

随着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可用的所有设备。

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

样例输出 (名字:“/ cpu: 0” device_type:“CPU” memory_limit: 268435456 位置{ } 化身:4402277519343584096, 名称:“/ gpu: 0” device_type:“GPU” memory_limit: 6772842168 位置{ bus_id: 1 } 化身:7471795903849088328 physical_device_desc: "设备:0,名称:GeForce GTX 1070, pci总线id: 0000:05:00.0" ]

在Jupyter或你的IDE中运行这个命令,检查Tensorflow是否使用GPU:

在新版本的TF(>2.1)中,检查TF是否使用GPU的建议方法是:

tf.config.list_physical_devices('GPU')

我发现从命令行查询gpu是最简单的:

nvidia-smi

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.98                 Driver Version: 384.98                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 980 Ti  Off  | 00000000:02:00.0  On |                  N/A |
| 22%   33C    P8    13W / 250W |   5817MiB /  6075MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1060      G   /usr/lib/xorg/Xorg                            53MiB |
|    0     25177      C   python                                      5751MiB |
+-----------------------------------------------------------------------------+

如果你的学习是一个后台过程,pid从 Jobs -p应该匹配nvidia-smi的pid

我发现下面的代码片段非常方便测试gpu ..

Tensorflow 2.0测试

import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
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))

张量流测试

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