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


当前回答

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

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

其他回答

你可以通过运行下面的代码来检查你是否正在使用GPU:

import tensorflow as tf
tf.test.gpu_device_name()

如果输出是“,这意味着你只使用CPU; 如果输出类似于/device:GPU:0,这意味着GPU工作。


并使用下面的代码来检查您使用的GPU:

from tensorflow.python.client import device_lib 
device_lib.list_local_devices()
>>> import tensorflow as tf 
>>> tf.config.list_physical_devices('GPU')

2020-05-10 14:58:16.243814: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-05-10 14:58:16.262675: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-10 14:58:16.263119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1060 6GB computeCapability: 6.1
coreClock: 1.7715GHz coreCount: 10 deviceMemorySize: 5.93GiB deviceMemoryBandwidth: 178.99GiB/s
2020-05-10 14:58:16.263143: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-05-10 14:58:16.263188: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-05-10 14:58:16.264289: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-05-10 14:58:16.264495: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-05-10 14:58:16.265644: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-05-10 14:58:16.266329: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-05-10 14:58:16.266357: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-05-10 14:58:16.266478: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-10 14:58:16.266823: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-10 14:58:16.267107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

正如@AmitaiIrron所建议的:

这个部分表示找到了一个gpu

2020-05-10 14:58:16.263119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:

pciBusID: 0000:01:00.0 name: GeForce GTX 1060 6GB computeCapability: 6.1
coreClock: 1.7715GHz coreCount: 10 deviceMemorySize: 5.93GiB deviceMemoryBandwidth: 178.99GiB/s

这里它被添加为一个可用的物理设备

2020-05-10 14:58:16.267107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0

[PhysicalDevice(name='/physical_device:GPU:0', device_type='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的使用有多激烈。

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

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

我认为有一种更简单的方法来实现这一点。

import tensorflow as tf
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

它通常是这样的

Default GPU Device: /device:GPU:0

对我来说,这似乎比那些冗长的日志更容易。

编辑: 对TF 1进行了测试。x版本。我从来没有机会做TF 2.0或以上的东西,所以请记住。