3 题: 如何检查keras是否使用gpu版本的tensorflow?

在...创建的问题 Thu, Jun 15, 2017 12:00 AM

当我运行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之前,我正在使用GPU版本的tensorflow。

此外,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
    
41
  1. 2018-04-19 04:11:57Z
  2. @ Foreever您确实意识到我发布此问题后发布了重复的问题?
    2018-04-19 04:43:01Z
  3. 对不起。我将此添加为重复只是因为其他线程有更好的答案。无论删除重复的评论。
    2018-04-23 02:00:16Z
  4. 醇>
    3答案                              3 跨度>                         

    您正在使用GPU版本。您可以列出可用的张量流设备(另请参阅问题):

     
    from tensorflow.python.client import device_lib
    print(device_lib.list_local_devices())
    

    在你的情况下,cpu和gpu都可用,如果使用exporflow的cpu版本,则不会列出gpu。在你的情况下,如果不设置张量流设备(with tf.device("..")),tensorflow会自动选择你的gpu!

    此外,您的sudo pip3 list清楚地显示您正在使用tensorflow-gpu。如果您有tenoflow cpu版本,名称将类似于tensorflow(1.1.0)

    检查问题,了解有关警告的信息。

        
    69
    2017-06-14 14:40:32Z

    为了让Keras使用GPU,必须采取许多措施。 Keras(以及TF和PyTorch)默默地回归CPU,这通常是我想要的。

    我在开发箱上改变了很多东西,有双启动,多个环境等。另外在Jupyter中,很容易连接到错误的内核,可能没有为GPU配置。

    为了减轻这种困惑,在我的家用笔记本中,我有一点点验证,我喜欢放在靠近顶部的单元格中:

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

    修改强>

    在更新环境后运行此代码可能也是个好主意。我做了一个conda update --all,在输出结束时我注意到了

     
    The following packages will be SUPERSEDED by a higher-priority channel:
    
      pytorch            pytorch::pytorch-1.0.1-py3.7_cuda100_~ --> pkgs/main::pytorch-1.0.1-cpu_py37h39a92a0_0
    
    
    Proceed ([y]/n)?
    

    我继续进行升级,并且毫不奇怪,PyTorch退出使用GPU。关键是,运行时不仅会回退到CPU,而且程序包管理器也可能决定将二进制文件还原为CPU。它的任务是在您的环境中安装一组兼容的库,它不关心GPU与CPU。

    (然后我可以通过运行conda install pytorch torchvision cudatoolkit=10.0 -c pytorch恢复GPU访问。)

        
    20
    2019-06-02 18:33:21Z
    1. 今天应该是正确答案。
      2018-12-02 21:04:16Z
    2. 醇>

    要找出您的操作和张量分配给哪些设备,请创建会话,并将log_device_placement配置选项设置为True。

     
    # Creates a graph.
    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)
    # Creates a session with log_device_placement set to True.
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    # Runs the op.
    print(sess.run(c))
    
    你笑了你会看到以下输出:  
    Device mapping:
    /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
    id: 0000:05:00.0
    b: /job:localhost/replica:0/task:0/device:GPU:0
    a: /job:localhost/replica:0/task:0/device:GPU:0
    MatMul: /job:localhost/replica:0/task:0/device:GPU:0
    [[ 22.  28.]
     [ 49.  64.]]
    

    有关详细信息,请参阅使用带张量流的GPU 链接

        
    3
    2018-10-26 04:18:44Z
来源放置 这里