keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.05k stars 19.48k forks source link

How to define Dense layer between Conv layer and LSTM layer? #4403

Closed hassaku closed 7 years ago

hassaku commented 8 years ago

I try to implement the following model called LSDNN.

CONVOLUTIONAL, LONG SHORT-TERM MEMORY, FULLY CONNECTED DEEP NEURAL NETWORKS http://static.googleusercontent.com/media/research.google.com/ja//pubs/archive/43455.pdf

Input -> Conv(+Pool) -> Dense (to reduce dimension) -> LSTM -> Dense -> Output

model = Sequential()
model.add(Convolution1D(..., input_shape=(timesteps, data_dim)))
model.add(MaxPooling1D(...))
model.add(Dense(...))  # to reduce dimension
model.add(LSTM(...))
model.add(Dense(...))
model.add(Dense(nb_classes, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

model.fit(...)  # Exception: Input 0 is incompatible with layer dense_1: expected ndim=2, found ndim=3

If the first Dense layer is removed, exception not occurs. But dimension isn't reduced.

How to use Dense layer keeping input shape for LSTM? Thank you for any help someone can provide.

carlthome commented 8 years ago

LSTM() expects input of shape (nb_timesteps, nb_features) but Dense outputs 1D so use Reshape() to add a dummy dimension after the fully-connected layer or use TimeDistributed(Dense()).

hassaku commented 7 years ago

Thank you for your advice. It's solved by using TimeDistributed now.