Search code examples
python-3.xtensorflowmachine-learninglstmkeras-layer

How to connect LSTM with Dense?


When trying to connect LSTM with Dense, it gives an error (when trying to train):

input = Input(shape=(x_train.shape[1], None))
X = Embedding(num_words, max_article_len)(input)
X = LSTM(128, return_sequences=True, dropout = 0.5)(X)
X = LSTM(128)(X)
X = Dense(32, activation='softmax')(X)

model = Model(inputs=[input], outputs=[X])
...
>>> ValueError: Error when checking target: expected dense to have shape (32,) but got array with shape (1,)

I tried different connection options, but the error repeats:

X, h, c = LSTM(128, return_sequences=False, return_state=True, dropout = 0.5)(X)
X = Dense(32, activation='softmax')(X)
>>> ValueError: Error when checking target: expected dense to have shape (32,) but got array with shape (1,)

Any solution options on the functional API / Sequential?

Data conversion code:

train = pd.read_csv('train.csv')
articles = train['text']
y_train = train['lang']

num_words = 50000
max_article_len = 20

tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(articles)

sequences = tokenizer.texts_to_sequences(articles)
x_train = pad_sequences(sequences, maxlen=max_article_len, padding='post')

x_train.shape
>>> (18974, 100)
y_train.shape
>>> (18974,)

Solution

  • The last parameter must be set to False;

    X = LSTM(128, return_sequences=True, dropout = 0.5)(X)
    X = LSTM(128, return_sequences=False)(X)
    

    If you still have issues, then the problem must be with your input shape.