Tensorflow:在 cpu 中使用在 CUDNNLSTM 中训练的模型

Dee*_*nka 6 python tensorflow

我已经使用 GPU 在 tensorflow 中使用 CUDNNLSTM 训练了一个模型。当我尝试在 cpu 中使用模型进行推理时,出现此错误:

Invalid argument: No OpKernel was registered to support Op 'CudnnRNN' with these attrs.  Registered devices: [CPU], Registered kernels:
  <no registered kernels>

     [[Node: cudnn_lstm/CudnnRNN = CudnnRNN[T=DT_FLOAT, direction="bidirectional", dropout=0, input_mode="linear_input", is_training=false, rnn_mode="lstm", seed=87654321, seed2=4567](Reshape_1, cudnn_lstm/zeros, cudnn_lstm/zeros_1, cudnn_lstm/opaque_kernel/read)]]
Run Code Online (Sandbox Code Playgroud)

那么,我们如何在cpu中使用这个模型呢?

tu_*_*ous 5

请查看 CuDNN LSTM 层的 tensorflow 源代码中的注释:https : //github.com/tensorflow/tensorflow/blob/r1.6/tensorflow/contrib/cudnn_rnn/python/layers/cudnn_rnn.py

从第 83 行开始,他们已经描述了如何做,你想要什么。基本上,在使用 CuDNN 层进行训练后,您需要将权重转移到使用 CuDNN 兼容 LSTM 单元制作的模型。这样的模型将在 CPU 和 GPU 上运行。此外,据我所知,tensorflow 中的 CuDNN LSTM 层是时间主要的,所以不要忘记转置您的输入(我不确定最新的 tensorflow 版本中的这一点,请确认这一点)。

有关基于上述的简短完整示例,请查看 melgor 的要点:

https://gist.github.com/melgor/41e7d9367410b71dfddc33db34cba85f?short_path=29ebfc6

  • 请看一下@Yu-Yang 和 fchollet 的讨论 https://github.com/keras-team/keras/pull/8307 看来确实有问题,适当的更改已合并到最新的 keras 分支. 如果更新 keras 后,它仍然不适合您,您可以执行以下两项操作之一。1. 将更改(在上面的 PR 中突出显示)复制到本地 Keras 文件。或 2. 从同一链接获取整个更新的 Keras 存储库。希望能帮助到你! (2认同)