如何判断tensorflow是否在python shell中使用gpu加速?

Tam*_*ari 246 python ubuntu tensorflow

我已经在我的ubuntu 16.04中使用第二个答案 ubuntu的内置apt cuda安装中安装了tensorflow .

现在我的问题是如何测试tensorflow是否真的使用gpu?我有一个gtx 960m gpu.当我import 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
Run Code Online (Sandbox Code Playgroud)

这个输出是否足以检查tensorflow是否正在使用gpu?

Yao*_*ang 260

不,我不认为"开放CUDA库"足以说明,因为图表的不同节点可能在不同的设备上.

要找出使用的设备,您可以启用日志设备放置,如下所示:

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Run Code Online (Sandbox Code Playgroud)

检查控制台是否有此类输出.

  • 可以在运行Jupyter Notebook的控制台上生成输出. (26认同)
  • @iyop45对于tensorflow V2,命令进行了一些修改:`sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))` (25认同)
  • 我尝试了这个,它打印绝对没有.知道为什么会这样吗? (17认同)
  • 你是在一个jupyter笔记本上做的吗? (7认同)
  • 我们能否获得Tensorflow V2的更新答案(不支持tf.Sessions)。 (6认同)
  • 与@Qubix相同,它不会打印任何内容.我正在Jupyter笔记本中执行它.我试图打印sess但我没有任何相关性. (4认同)
  • Qubix和richar8086,它打印在您启动jupyter笔记本的终端上 (2认同)
  • AttributeError: 模块“tensorflow”没有属性“Session” (2认同)

Sal*_*ali 256

除了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))
Run Code Online (Sandbox Code Playgroud)

这里

  • "/ cpu:0":您机器的CPU.
  • "/ gpu:0":你的机器的GPU,如果你有的话.

如果您有一个gpu并且可以使用它,您将看到结果.否则,您将看到长堆栈跟踪的错误.最后你会得到这样的东西:

无法将设备分配给节点'MatMul':无法满足显式设备规范'/ device:GPU:0',因为在此过程中未注册与该规范匹配的设备


最近在TF中出现了一些有用的功能:

您还可以在会话中检查可用的设备:

with tf.Session() as sess:
  devices = sess.list_devices()
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

  • 结果:[[22. 28.] [49. 64.]] (16认同)
  • @GeorgePligor结果在这里并不重要.要么您有结果并且使用了GPU,要么您有错误,这意味着它没有被使用 (4认同)
  • 即使在用“tf.compat.v1.Session()”替换“Session”之后,似乎根本不适用于tensorflow 2.1 (2认同)

She*_*raz 134

下面的代码应该为您提供所有可用于tensorflow的设备.

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
Run Code Online (Sandbox Code Playgroud)

样本输出

[name:"/ cpu:0"device_type:"CPU"memory_limit:268435456 locality {} incarnation:4402277519343584096,

name:"/ gpu:0"device_type:"GPU"memory_limit:6772842168 locality {bus_id:1} incarnation:7471795903849088328 physical_device_desc:"device:0,name:GeForce GTX 1070,pci bus id:0000:05:00.0"]

  • 这是最好的答案。 (5认同)
  • 我不同意,这 ** 不** 回答问题:这与设备 _available_ 无关,而是设计 ** 使用 **。这可能是一个完全不同的故事!(例如,TF 默认只使用 1 个 GPU。 (5认同)
  • 如果此命令没有返回任何带有"GPU"的条目,是否意味着我的机器只是拥有GPU,或者tensorflow无法找到它? (3认同)

Ish*_*att 65

我认为有一种更简单的方法可以实现这一目标.

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")
Run Code Online (Sandbox Code Playgroud)

它通常打印像

Default GPU Device: /device:GPU:0
Run Code Online (Sandbox Code Playgroud)

这对我而言比起那些冗长的日志更容易.


ma3*_*oun 28

Tensorflow 2.0

从tensorflow 2.0开始,不再使用会话。测试GPU功能的一种仍然有效的方法是:

import tensorflow as tf

assert tf.test.is_gpu_available()
assert tf.test.is_built_with_cuda()
Run Code Online (Sandbox Code Playgroud)

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

  • 现在建议执行 `tf.config.list_physical_devices('GPU')` (27认同)
  • @joselquin TF v2.x 文档声明它应该可以工作:https://www.tensorflow.org/api_docs/python/tf/config/experimental/list_physical_devices ,我可以验证它是否对我有用。 (2认同)

小智 27

这会在训练时确认使用GPU的tensorflow吗?

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Run Code Online (Sandbox Code Playgroud)

产量

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
Run Code Online (Sandbox Code Playgroud)

  • 请为_why_添加一些解释,你的答案是有效的(`log_device_placement`做了什么以及如何在输出中看到CPU与GPU?).这将提高你的答案质量! (5认同)

Tim*_*lin 27

更新 TENSORFLOW >= 2.1。

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

tf.config.list_physical_devices('GPU') 
Run Code Online (Sandbox Code Playgroud)

从 TensorFlow 2.1 开始,tf.test.gpu_device_name()已弃用,取而代之的是上述内容。

然后,您可以在终端nvidia-smi中查看已分配了多少 GPU 内存;同时, usingwatch -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()
Run Code Online (Sandbox Code Playgroud)

  • 要持续查看使用了多少 GPU 内存,请使用“nvidia-smi -l 10”。 (3认同)

kar*_*spd 22

除了其他答案之外,以下内容可帮助您确保您的tensorflow版本包含GPU支持.

import tensorflow as tf
print(tf.test.is_built_with_cuda())
Run Code Online (Sandbox Code Playgroud)

  • 警告:这告诉您TensorFlow是否使用GPU编译.不是GPU是否被使用.(例如,如果未正确安装驱动程序,则使用CPU,即使"is_built_with_cuda()"为真.) (4认同)

f0n*_*zie 18

这应该给出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)
Run Code Online (Sandbox Code Playgroud)


sco*_*ang 14

我更喜欢使用nvidia-smi来监控GPU的使用情况.如果你开始编程它会显着上升,这是你的张量流使用GPU的强烈信号.

  • 您还可以使用手表nvidia-smi,每2秒更新一次屏幕 (2认同)

kma*_*o23 11

好的,首先ipython shell从终端和importTensorFlow 发布一个

$ 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
Run Code Online (Sandbox Code Playgroud)

现在,我们可以使用以下命令观察 GPU内存使用情况:

# realtime update for every 2s
$ watch -n 2 nvidia-smi
Run Code Online (Sandbox Code Playgroud)

由于我们只import编辑了TensorFlow,但尚未使用任何GPU,因此使用情况统计信息将为:

非gpu使用情况

观察GPU内存使用情况如何(~200MB).



现在,让我们在代码中加载GPU.如图所示tf documentation,做:

In [2]: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Run Code Online (Sandbox Code Playgroud)

现在,监视统计信息应显示更新的GPU使用内存,如下所示:

tf gpu-watch

观察我们来自ipython shell的Python进程如何使用7.7GB的GPU内存.


PS您可以在代码运行时继续观察这些统计数据,以了解GPU使用情况的强烈程度.


小智 9

使用张量流 2.0 >=

import tensorflow as tf
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


waf*_*cat 8

在Jupyter中运行以下命令,

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Run Code Online (Sandbox Code Playgroud)

如果您已经正确设置了环境,那么您将在运行“ 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
Run Code Online (Sandbox Code Playgroud)

您可以在这里看到我正在使用TensorFlow和Nvidia Quodro K620。

  • 一些用户可能希望确保 GPU 在 Jupyter 中可用。此外,这可以从 Python 脚本运行。 (2认同)

小智 8

我发现只是从命令行查询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 |
+-----------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

如果你的学习是后台进程,那么pid jobs -p应与pid匹配nvidia-smi


Ham*_*ger 8

对于 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
Run Code Online (Sandbox Code Playgroud)


Cs2*_*s20 7

使用Tensorflow的最新更新,您可以按以下步骤进行检查:

tf.test.is_gpu_available( cuda_only=False, min_cuda_compute_capability=None)
Run Code Online (Sandbox Code Playgroud)

这将返回True如果正在使用的GPU Tensorflow,并返回False否则。

如果需要设备device_name,可以键入:tf.test.gpu_device_name()。从这里获取更多详细信息


bLe*_*eDy 6

>>> 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')]
Run Code Online (Sandbox Code Playgroud)

正如@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
Run Code Online (Sandbox Code Playgroud)

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

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')]
Run Code Online (Sandbox Code Playgroud)


Aad*_*ava 6

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

tf.config.list_physical_devices('GPU')
Run Code Online (Sandbox Code Playgroud)


Hu *_*ixi 5

您可以通过运行以下代码来检查当前是否正在使用GPU:

import tensorflow as tf
tf.test.gpu_device_name()
Run Code Online (Sandbox Code Playgroud)

如果输出为'',则表示您CPU仅在使用;
如果输出是类似的内容/device:GPU:0,则表示GPU有效。


并使用以下代码检查GPU您正在使用的代码:

from tensorflow.python.client import device_lib 
device_lib.list_local_devices()
Run Code Online (Sandbox Code Playgroud)


Pau*_*ams 5

将其放在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()))
Run Code Online (Sandbox Code Playgroud)

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

最初在这里回答。


小智 5

以下内容还将返回您的 GPU 设备的名称。

import tensorflow as tf
tf.test.gpu_device_name()
Run Code Online (Sandbox Code Playgroud)


aja*_*esh 5

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

Tensorflow 1 测试

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))
Run Code Online (Sandbox Code Playgroud)


Cha*_*tor 5

对于 Tensorflow 2.0

import tensorflow as tf

tf.test.is_gpu_available(
    cuda_only=False,
    min_cuda_compute_capability=None
)
Run Code Online (Sandbox Code Playgroud)

来源在这里

另一个选择是:

tf.config.experimental.list_physical_devices('GPU')
Run Code Online (Sandbox Code Playgroud)


can*_*nin 5

张量流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))
Run Code Online (Sandbox Code Playgroud)