当前位置: 首页 > 面试题库 >

我如何检查keras是否使用tensorflow的gpu版本?

水睿
2023-03-14
问题内容

运行keras脚本时,得到以下输出:

Using TensorFlow backend.
2017-06-14 17:40:44.621761: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use SSE4.1 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:40:44.621783: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use SSE4.2 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:40:44.621788: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use AVX instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:40:44.621791: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use AVX2 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:40:44.621795: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use FMA instructions, but these are 
available 
on your machine and could speed up CPU computations.
2017-06-14 17:40:44.721911: I 
tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful 
NUMA node read from SysFS had negative value (-1), but there must be 
at least one NUMA node, so returning NUMA node zero
2017-06-14 17:40:44.722288: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 
with properties: 
name: GeForce GTX 850M
major: 5 minor: 0 memoryClockRate (GHz) 0.9015
pciBusID 0000:0a:00.0
Total memory: 3.95GiB
Free memory: 3.69GiB
2017-06-14 17:40:44.722302: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-06-14 17:40:44.722307: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-06-14 17:40:44.722312: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating 
TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, 
pci bus id: 0000:0a:00.0)

这是什么意思?我是否正在使用GPU或CPU版本的Tensorflow?

在安装keras之前,我正在使用Tensorflow的GPU版本。

sudo pip3 list显示tensorflow-gpu(1.1.0)和没有什么像tensorflow-cpu

运行[此stackoverflow问题]中提到的命令,将得到以下信息:

The TensorFlow library wasn't compiled to use SSE4.1 instructions, 
but these are available on your machine and could speed up CPU 
computations.
2017-06-14 17:53:31.424793: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use SSE4.2 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:53:31.424803: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use AVX instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:53:31.424812: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use AVX2 instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:53:31.424820: W 
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow 
library wasn't compiled to use FMA instructions, but these are 
available on your machine and could speed up CPU computations.
2017-06-14 17:53:31.540959: I 
tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful 
NUMA node read from SysFS had negative value (-1), but there must be 
at least one NUMA node, so returning NUMA node zero
2017-06-14 17:53:31.541359: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 
with properties: 
name: GeForce GTX 850M
major: 5 minor: 0 memoryClockRate (GHz) 0.9015
pciBusID 0000:0a:00.0
Total memory: 3.95GiB
Free memory: 128.12MiB
2017-06-14 17:53:31.541407: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-06-14 17:53:31.541420: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-06-14 17:53:31.541441: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating 
TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, 
pci bus id: 0000:0a:00.0)
2017-06-14 17:53:31.547902: E 
tensorflow/stream_executor/cuda/cuda_driver.cc:893] failed to 
allocate 128.12M (134348800 bytes) from device: 
CUDA_ERROR_OUT_OF_MEMORY
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce 
GTX 850M, pci bus id: 0000:0a:00.0
2017-06-14 17:53:31.549482: I 
tensorflow/core/common_runtime/direct_session.cc:257] Device 
mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce 
GTX 850M, pci bus id: 0000:0a:00.0

问题答案:

您正在使用GPU版本。您可以列出可用的tensorflow设备(也请检查此问题):

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices()) # list of DeviceAttributes

编辑:

使用tensorflow> =1.4时,您可以运行以下功能:

import tensorflow as tf
tf.test.is_gpu_available() # True/False

# Or only check for gpu's with cuda support
tf.test.is_gpu_available(cuda_only=True)

编辑2:

上面的功能在中已弃用tensorflow > 2.1。相反,您应该使用以下功能:

import tensorflow as tf
tf.config.list_physical_devices('GPU')

注意:

在您的情况下,cpu和gpu均可用,如果使用tensorflow的cpu版本,则不会列出gpu。在您的情况下,无需设置tensorflow设备(withtf.device("..")),tensorflow将自动选择您的GPU!

此外,您sudo pip3 list清楚地表明您正在使用tensorflow-gpu。如果您拥有tensoflow
cpu版本,则名称将类似于tensorflow(1.1.0)

检查此问题以获取有关警告的信息。



 类似资料:
  • 当我运行keras脚本时,我得到以下输出: 这是什么意思?我是否使用GPU或CPU版本的tenstorflow? 在安装keras之前,我使用的是tensorflow的GPU版本。 另外显示了,与完全不同。 运行[this stackoverflow question]中提到的命令,可以得到以下结果:

  • 我知道在安装tensorflow时,您可以安装GPU或CPU版本。如何检查安装了哪一个(我使用linux)。 如果安装了GPU版本,如果GPU不可用,它会自动在CPU上运行吗?如果GPU可用,是否需要设置特定的字段或值来确保它在GPU上运行?

  • 与此问题相同,但适用于caffe。我想要一个命令,我可以把我的python脚本,以检查是否使用gpu。 当我的模型运行时,我检查了nvidia smi,我看到python被认为是一个进程,但使用情况不适用。 我还试着经营咖啡馆。设置_mode_cpu()命令时,考虑到时间会非常不同,但有命令的时间和没有命令的时间相同。

  • 问题内容: 如何在Python 3.6 x64中使用 TensorFlow GPU 版本而不是 CPU 版本? Python正在使用我的 CPU 进行计算。 我可以注意到它,因为我有一个错误: 您的CPU支持该TensorFlow二进制文件未编译为使用的指令:AVX2 我已经安装了tensorflow和tensorflow-gpu。 如何切换到GPU版本? 问题答案: 遵循本教程Tensorflo

  • 问题内容: 我想知道是否正在使用我的GPU。在此过程中,可以检测是否有来自GPU的任何活动,但是我想要在脚本中编写一些东西。 有办法吗? 问题答案: 这将起作用: 这告诉我GPU正在被使用。

  • 我想知道Pytork是否正在使用我的GPU。使用可以检测在这个过程中是否有来自GPU的任何活动,但是我想要用脚本编写一些东西。 有没有办法做到这一点?