question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tf.keras.layers.Bidirectional: Please initialize `Bidirectional` layer with a `tf.keras.layers.Layer` instance

See original GitHub issue

Getting some strange error when using bidirectional LSTM in tf.keras. I have followed documentation of bidriectional, but getting the error.

Here is the code:

 rl = Reshape((-1, int(cnn_SE_featureMap.shape[2]) * int(cnn_SE_featureMap.shape[3])))(
        input) ####(type_spec=TensorSpec(shape=(None, 24, 9))
# lstm_l6 = tf.keras.layers.LSTM(128, activation='tanh', recurrent_initializer=tf.keras.initializers.Orthogonal(seed=1), dropout=0.5, recurrent_dropout=0.25)(lstm_l5)
lstm_l5 = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, activation='tanh',
                               recurrent_initializer=tf.keras.initializers.Orthogonal(), dropout=0.5,
                               recurrent_dropout=0.5, return_sequences=True)(rl)) 

Error: Traceback (most recent call last):

File "/home/anafees/.local/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3251, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-721b57a9dbb6>", line 1, in <module>
    lstm_l5 = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, activation='tanh',
  File "/home/anafees/.local/lib/python3.9/site-packages/keras/layers/wrappers.py", line 453, in __init__
    raise ValueError(
ValueError: Please initialize `Bidirectional` layer with a `tf.keras.layers.Layer` instance. Received: KerasTensor(type_spec=TensorSpec(shape=(None, 128), dtype=tf.float32, name=None), name='lstm_5/strided_slice_3:0', description="created by layer 'lstm_5'")

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
FrameMusecommented, Jun 21, 2022

Ok, I found solution in this answer on stackoverflow, Simply change tensorflow.keras to keras.

Reference from stackoverflow: You are mixing imports between the keras and tf.keras libraries, these are not the same library, and doing this is not supported.

You need to make all imports from one of the libraries:

from keras.layers import Input, LSTM, Dense, TimeDistributed, Activation, BatchNormalization, Dropout, Bidirectional
from keras.models import Sequential
from keras.utils import Sequence
from keras.layers import CuDNNLSTM
1reaction
Nafees-060commented, Jun 23, 2022

@FrameMuse Might be. But now tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64,dropout=0.5))(rl) is working for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

tf.keras.layers.Bidirectional | TensorFlow v2.11.0
Layer instance that meets the following criteria: Be a sequence-processing layer (accepts 3D+ inputs). Have a go_backwards , return_sequences ...
Read more >
Please initialize `Bidirectional` layer with a `Layer` instance ...
The tf.keras.layers.Bidirectional layer which is a wrapper layer for RNNs should be initialized with a Keras layer type as its argument.
Read more >
tf.keras.layers.Bidirectional | TensorFlow
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its...
Read more >
A Guide to Bidirectional RNNs With Keras - Paperspace Blog
This tutorial covers bidirectional recurrent neural networks: how they work, their applications, and how to implement a bidirectional RNN with Keras.
Read more >
How to set an initial state for a Bidirectional LSTM Layer in ...
Model, and met the same error. this PR may help you. Finally I built my model by tf.keras.layers.layer, and I'm still working on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found