Juan
Juan

Reputation: 194

How to use Conv2D and LSTM layers with Keras?

I have a model which works with Conv2D using Keras but I would like to add a LSTM layer. This is the data I am using:

My model without LSTM is:

inputs = Input(name='input',shape=(334,35,1))
layer = Conv2D(64, kernel_size=3,activation='relu',data_format='channels_last')(inputs)
layer = Flatten()(layer)
predictions = Dense(5, activation='softmax')(layer)
network = Model(inputs=inputs, outputs=predictions)
network.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

What is the correct way of adding a LSTM layer just before the Dense layer?

I tried to use TimeDistributed or Reshape/Permute but I always get errors.

Upvotes: 0

Views: 9322

Answers (2)

Juan
Juan

Reputation: 194

The method explained by the user deKeijzer works. I found another way to solve the problem. It is to use a Reshape layer (reshaping by (334,35)) just after the last Conv2D layer and then add LSTM layers.

Upvotes: 0

deKeijzer
deKeijzer

Reputation: 510

It seems like your question is similar to one that i had yesterday. The answer can be found here: Keras functional API: Combine CNN model with a RNN to to look at sequences of images

Upvotes: 0

Related Questions