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


不,我不认为“开放CUDA库”足以说明问题,因为图的不同节点可能在不同的设备上。

当使用tensorflow2时:

print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

对于tensorflow1,要找出使用了哪个设备,您可以像这样启用日志设备放置:

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

检查控制台中是否有这种类型的输出。

这将证实张量流使用GPU同时训练也?

Code

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

输出

I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: 
name: GeForce GT 730
major: 3 minor: 5 memoryClockRate (GHz) 0.9015
pciBusID 0000:01:00.0
Total memory: 1.98GiB
Free memory: 1.72GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0
I tensorflow/core/common_runtime/direct_session.cc:255] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0

除了使用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)

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

在Jupyter上运行以下命令,

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

如果你已经正确地设置了环境,你将在运行“jupyter notebook”的终端中得到以下输出,

2017-10-05 14:51:46.335323: I c:\tf_jenkins\home\workspace\release-win\m\windows-gpu\py\35\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Quadro K620, pci bus id: 0000:02:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Quadro K620, pci bus id: 0000:02:00.0
2017-10-05 14:51:46.337418: I c:\tf_jenkins\home\workspace\release-win\m\windows-gpu\py\35\tensorflow\core\common_runtime\direct_session.cc:265] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Quadro K620, pci bus id: 0000:02:00.0

你可以看到我在使用Nvidia Quodro K620的TensorFlow。

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

我发现从命令行查询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

下面的代码段应该给出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" ]

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

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或以上的东西,所以请记住。

好的,首先从终端启动一个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版本包含GPU支持。

import tensorflow as tf
print(tf.test.is_built_with_cuda())

你可以通过运行下面的代码来检查你是否正在使用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()

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

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

把这个放在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的最新更新,你可以检查它如下:

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

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

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

这是我用来列出tf可用设备的行。会话直接从bash:

python -c "import os; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'; import tensorflow as tf; sess = tf.Session(); [print(x) for x in sess.list_devices()]; print(tf.__version__);"

它将打印可用的设备和tensorflow版本,例如:

_DeviceAttributes(/job:localhost/replica:0/task:0/device:CPU:0, CPU, 268435456, 10588614393916958794)
_DeviceAttributes(/job:localhost/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 12320120782636586575)
_DeviceAttributes(/job:localhost/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 13378821206986992411)
_DeviceAttributes(/job:localhost/replica:0/task:0/device:GPU:0, GPU, 32039954023, 12481654498215526877)
1.14.0

你有一些选项来测试你的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 ()

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

import tensorflow as tf
tf.test.gpu_device_name()

我发现下面的代码片段非常方便测试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))

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

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

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

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

对于Tensorflow 2.0

import tensorflow as tf

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

源在这里

其他选择是:

tf.config.experimental.list_physical_devices('GPU')

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

更新为tensorflow >= 2.1

检查TensorFlow是否使用GPU的推荐方法如下:

tf.config.list_physical_devices('GPU') 

从TensorFlow 2.1开始,tf.test.gpu_device_name()已经被弃用,取而代之的是前面提到的。

然后,在终端中,您可以使用nvidia-smi检查有多少GPU内存已分配;同时,使用watch -n K nvidia-smi会告诉你,例如每K秒你使用了多少内存(你可能想使用K = 1实时)

如果你有多个GPU,你想使用多个网络,每个网络都在一个独立的GPU上,你可以使用:

 with tf.device('/GPU:0'):
      neural_network_1 = initialize_network_1()
 with tf.device('/GPU:1'):
      neural_network_2 = initialize_network_2()

与tensorflow 2.0 >=

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

Tensorflow 2.1

一个简单的计算,可以用nvidia-smi验证GPU上的内存使用情况。

import tensorflow as tf 

c1 = []
n = 10

def matpow(M, n):
    if n < 1: #Abstract cases where n < 1
        return M
    else:
        return tf.matmul(M, matpow(M, n-1))

with tf.device('/gpu:0'):
    a = tf.Variable(tf.random.uniform(shape=(10000, 10000)), name="a")
    b = tf.Variable(tf.random.uniform(shape=(10000, 10000)), name="b")
    c1.append(matpow(a, n))
    c1.append(matpow(b, n))
>>> 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')]

对于TF2.4+, tensorflow网站上列出的“官方”方法来检查TF是否使用GPU

>>> import tensorflow as tf
>>> print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
Num GPUs Available:  2

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

tf.config.list_physical_devices('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

也许可以试试这个:

print(tf.reduce_sum(tf.random. sum);正常((1000、1000))))

看系统是否返回张量

根据网站