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


当前回答

我找到了最简单、最全面的方法。只要设置tf.debug .set_log_device_placement(True),你就可以看到ops是否真的在GPU上运行,例如在device /job:localhost/replica:0/task:0/device:GPU:0中执行op _EagerConst

更多文档:https://www.tensorflow.org/guide/gpu#logging_device_placement

其他回答

这应该会给出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)

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

把这个放在jupyter笔记本的最上面。注释掉你不需要的东西。

# confirm TensorFlow sees the GPU
from tensorflow.python.client import device_lib
assert 'GPU' in str(device_lib.list_local_devices())

# confirm Keras sees the GPU (for TensorFlow 1.X + Keras)
from keras import backend
assert len(backend.tensorflow_backend._get_available_gpus()) > 0

# confirm PyTorch sees the GPU
from torch import cuda
assert cuda.is_available()
assert cuda.device_count() > 0
print(cuda.get_device_name(cuda.current_device()))

注意:随着TensorFlow 2.0的发布,Keras现在被包含在TF API中。

原来在这里回答。

你有一些选项来测试你的TensorFlow安装是否正在使用GPU加速。

您可以在三种不同的平台上输入以下命令。

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Jupyter Notebook -检查运行Jupyter Notebook的控制台。你将能够看到GPU正在被使用。 Python Shell -你将能够直接看到输出。(注意-不要将第二个命令的输出分配给变量'sess';如果这有帮助的话)。 Spyder -在控制台中输入以下命令。 将tensorflow导入为tf tf.test.is_gpu_available ()

随着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()。 从这里获取更多细节