【问题标题】:How can I check if keras/tensorflow is using cuDNN?如何检查 keras/tensorflow 是否正在使用 cuDNN?
【发布时间】:2018-02-01 15:37:40
【问题描述】:

我已经安装了 CUDA 和 cuDNN,但是最后一个不起作用,在 theano 中给出了很多错误消息。现在我在 Keras/Tensorflow 中训练中等大小的深度卷积网络,没有收到任何 cuDNN 错误消息。如何检查是否正在使用 cuDNN?

【问题讨论】:

    标签: tensorflow keras cudnn


    【解决方案1】:

    tl;dr:如果 tensorflow-gpu 有效,则使用 CuDNN。

    TensorFlow 的预构建二进制文件(至少从 1.3 版开始)链接到 CuDNN 库。如果缺少 CuDNN,则会出现错误消息告诉您 ImportError: Could not find 'cudnn64_7.dll'. TensorFlow requires that this DLL be installed...

    根据TensorFlow install documentation for version 1.5,即使您从源代码构建,也必须安装 CuDNN 以支持 GPU。在 CuDNN 不可用的情况下,TensorFlow 代码中仍有很多回退——据我所知,它在以前的版本中曾经是可选的。

    Here are two lines from the TensorFlow source that explicitly tell and force that CuDNN is required for gpu acceleration.

    需要安装一个特殊的 GPU 版本的 TensorFlow 才能使用 GPU(和 CuDNN)。确保安装的 python 包是tensorflow-gpu 而不仅仅是tensorflow

    您可以使用conda list tensorflow(或仅pip list,如果您不使用anaconda)列出包含“tensorflow”的包,但请确保您激活了正确的环境。

    当您在支持 GPU 的情况下运行脚本时,它们将像这样开始:

    Using TensorFlow backend.
    
    2018- ... C:\tf_jenkins\...\gpu\gpu_device.cc:1105] Found device 0 with properties:
    name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7845
    

    要测试它,只需在控制台中输入:

    import tensorflow as tf
    tf.Session()
    

    要检查您是否从 Python 环境中“看到”了 CuDNN 并由此验证正确的 PATH 变量,您可以尝试以下操作:

    import ctypes
    ctypes.WinDLL("cudnn64_7.dll") # use the file name of your cudnn version here.
    

    【讨论】:

    • 是的,我有 tensorflow-gpu,它正在使用 GPU。我的问题取决于 cuDNN 的具体用法。据我了解,GPU 的使用可以独立于 cuDNN 发生,因为它只是一个 GPU 加速库,具有改进的 gpu 使用库,对吗?
    • @hirschme 即使您从源代码构建 tensorflow,您也必须指定 CuDNN,因此对于当前版本,没有它就无法构建它。看起来它与非常早期版本的 tensorflow 不同,但我说的是当前版本,即 1.5。
    • @hirschme 我在回答中添加了更多信息,解释了为什么我认为没有 CuDNN 的 tensorflow-gpu 目前无法工作。如果您仍有疑问,您可以尝试添加 Conv2D-Layer,因为 TensorFlow 会明确捕获这种情况并报错:github.com/tensorflow/tensorflow/blob/…
    【解决方案2】:

    您可能还想了解 GPU 优化的 Keras 层。

    • CuDNNLSTM
    • CuDNNGRU

    它们明显更快: https://keras.io/layers/recurrent/#cudnnlstm

    我们看到从 LSTM 到 CuDNNLSTM Keras 层有 10 倍的改进。

    注意: 我们还看到机器上的 VMS(虚拟内存)使用量增加了 10 倍。所以需要权衡取舍。

    【讨论】:

    • 如果您在答案中包含比较以证明 明显更快 声明,那会更好。
    • 我们看到从 LSTM 到 CuDNNLSTM Keras 层的改进提高了 10 倍。
    • @lolbas 不需要。众所周知,CuDNN 的实现比基于循环的实现快 3 到 10 倍。
    猜你喜欢
    • 1970-01-01
    • 2017-11-16
    • 2020-07-06
    • 2019-08-17
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2023-02-06
    相关资源
    最近更新 更多