【Tensorflow 2】解决tensorflow.python.framework.errors_impl.UnknownError: [_Derived_] Fail to find the

旧城等待, 2022-11-06 09:51 226阅读 0赞

1 问题

环境

Deepin 20 OS linux系统
Tensorflow2.0
Python3.6
GPU Rtx 2070
cuda 10.0
cudnn 10.0

在使用双向LSTM时

  1. tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),

报以下错误信息

tensorflow.python.framework.errors_impl.UnknownError: [Derived] Fail to find the dnn implementation.
[[{ {node CudnnRNN}}]]
[[bi_complex_lstm/bidirectional/forward_lstm/StatefulPartitionedCall]] [Op:__inference_keras_scratch_graph_34502]
Function call stack:

2 解决

因为在Tensorflow2.0中使用keras的API时,该版本的keras已经不支持直接使用Bidirectional(LSTM ),三种解决办法。
(1)第一种方法
继续使用在Tensorflow2.0的keras API,用兼容的替代的写法

  1. from tensorflow.keras import *
  2. from tensorflow.keras.layers import Bidirectional
  3. Bidirectional(tf.keras.layers.RNN(tf.keras.layers.LSTMCell(n_BiLSTM_1),return_sequences=True))

(2)第二种方法
使用支持该方法的原装Keras:2.1.6

  1. # 安装
  2. pip install keras ==2.1.6
  3. # 使用
  4. from keras.layers import Bidirectional,LSTM

(3)第三种方法
继续使用在Tensorflow2.0的keras API,但加入表示兼容的代码

  1. from tensorflow.keras.layers import Bidirectional,LSTM
  2. from tensorflow.compat.v1 import ConfigProto
  3. from tensorflow.compat.v1 import InteractiveSession
  4. config = ConfigProto()
  5. config.gpu_options.allow_growth = True
  6. session = InteractiveSession(config=config)

发表评论

表情:
评论列表 (有 0 条评论,226人围观)

还没有评论,来说两句吧...

相关阅读