【Tensorflow+keras】解决 Fail to find the dnn implementation.

旧城等待, 2022-10-05 01:51 314阅读 0赞

1 引言

(1)环境

  1. Tensorflow 2.0
  2. Python 3.6

(2)问题

  1. from tensorflow.keras import *
  2. from tensorflow.keras.layers import Bidirectional,LSTM

在使用Bidirectional(LSTM )时,报错 [Derived] Fail to find the dnn implementation.

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 条评论,314人围观)

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

相关阅读