我已经使用 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中使用这个模型呢?
请查看 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