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)
检查控制台是否有此类输出.
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)
这里
如果您有一个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)
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"]
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)
如果出现错误,则需要检查安装。
小智 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)
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)
kar*_*spd 22
除了其他答案之外,以下内容可帮助您确保您的tensorflow版本包含GPU支持.
import tensorflow as tf
print(tf.test.is_built_with_cuda())
Run Code Online (Sandbox Code Playgroud)
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的强烈信号.
kma*_*o23 11
好的,首先ipython shell
从终端和import
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
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内存使用情况如何(~200MB).
现在,让我们在代码中加载GPU.如图所示tf documentation
,做:
In [2]: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Run Code Online (Sandbox Code Playgroud)
现在,监视统计信息应显示更新的GPU使用内存,如下所示:
观察我们来自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)
在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。
小智 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
>>> 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)
使用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()
。从这里获取更多详细信息
>>> 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)
在新版本的 TF(>2.1) 中,检查 TF 是否使用 GPU 的推荐方法是:
tf.config.list_physical_devices('GPU')
Run Code Online (Sandbox Code Playgroud)
您可以通过运行以下代码来检查当前是否正在使用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)
将其放在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)
我发现下面的代码片段非常方便测试 gpu ..
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)
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)
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)
可以使用 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)
归档时间: |
|
查看次数: |
363111 次 |
最近记录: |