Tensorflow使用GPU,如何查看Tensorflow是否正在使用GPU?

6

尝试安装tensorflow以与GPU一起工作。我看到的一些文档说,当检测到时,tensorflow会自带gpu支持。如果是这样,我应该使用什么命令来查看tensorflow是否正在使用我的GPU?我看到其他文档说需要安装tensorflow-gpu。我都尝试过了,但是不知道如何查看我的GPU是否正在被使用?

3个回答

8

打开命令行并运行Python

以下示例列出了主机上可见的GPU数量。 文档

import tensorflow as tf
devices = tf.config.list_physical_devices('GPU')
print(len(devices)) 

针对 CUDA 的文档 Docs

import tensorflow as tf
tf.test.is_built_with_cuda()

返回TensorFlow是否使用了CUDA(GPU)支持进行构建。 文档

您也可以使用以下函数进行检查,但它已被弃用**。

import tensorflow as tf

tf.test.is_gpu_available()

如果您的GPU可用,则两者都返回True。


4

在此处查找Tensorflow 2 GPU检查,

已弃用,https://www.tensorflow.org/api_docs/python/tf/test/is_gpu_available

https://www.tensorflow.org/api_docs/python/tf/config/list_physical_devices

检查Tensorflow是否支持CUDA(GPU),

https://www.tensorflow.org/api_docs/python/tf/test/is_built_with_cuda

Tensorflow GPU指南,

https://www.tensorflow.org/guide/gpu

代码

import tensorflow as tf

print(tf.config.list_physical_devices('GPU'))
# [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

print(tf.test.is_built_with_cuda)
# <function is_built_with_cuda at 0x7f4f5730fbf8>

print(tf.test.gpu_device_name())
# /device:GPU:0

print(tf.config.get_visible_devices())
# [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

0
print(tf.config.list_physical_devices('GPU'))

[物理设备(name='/physical_device:GPU:0', 设备类型='GPU')]

目前,你的回答不够清晰。请[编辑]并添加更多细节,以帮助他人理解这个回答如何解决所提出的问题。你可以在帮助中心找到更多关于如何撰写好的回答的信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接