TypeError:“双向”类型的对象没有len()

时间:2019-12-24 13:33:38

标签: tensorflow lstm

这是我正在运行的代码:

   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)

我正在运行bidirectional LSTM。但是出现此错误:

  

TypeError:“双向”类型的对象没有len()

此代码有什么问题?请帮忙。

2 个答案:

答案 0 :(得分:0)

您没有进行一致的导入。只需更改

from keras.models import Model

from tensorflow.keras.models import Model

答案 1 :(得分:0)

更改自

from tensorflow.keras.layers import Input

from keras.layers import Input

解决了我的问题。