0
$\begingroup$

I'm running a bidirectional LSTM. But this error is appearing:

TypeError: object of type 'Bidirectional' has no len()

What's wrong in this code? Please help.

 import pandas as pd
 import numpy as np
 from tqdm import tqdm, trange
 import unicodedata

 from keras.preprocessing.sequence import pad_sequences
 from keras.utils import to_categorical
 from keras.models import Model
 from tensorflow.keras.layers import Input
 from tensorflow.keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional

 # Defining Constants

 # Maximum length of text sentences
 MAXLEN = 180
 # Number of LSTM units
 LSTM_N = 150
 # batch size
 BS=48

 input = Input(shape=(MAXLEN,))

 model = Embedding(input_dim=n_words, output_dim=MAXLEN, input_length=MAXLEN)(input)

 model = Dropout(0.2)(model)

 model = Bidirectional(LSTM(units=LSTM_N, return_sequences=True, recurrent_dropout=0.1))(model)

 out = TimeDistributed(Dense(n_tags, activation="softmax"))(model)  # softmax output layer

 model = Model(input, out)

 model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 history = model.fit(X, np.array(y), batch_size=BS, epochs=2, validation_split=0.05, verbose=1)
$\endgroup$

1 Answer 1

1
$\begingroup$

That code instantiates model over and over. model should be instantiated once and all other layers are added to that instance. Something like this:

from tensorflow.keras.models import Sequential

model = Sequential()
model.add(Embedding(input_dim=n_words, output_dim=MAXLEN, input_length=MAXLEN))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(units=LSTM_N, return_sequences=True, recurrent_dropout=0.1)))
model.add(TimeDistributed(Dense(n_tags, activation="softmax")))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.