Keras incompatible input dimensions when unrolling LSTM

Viewed 272

Why does the following code give ValueError: Input 0 is incompatible with layer dense_14: expected min_ndim=2, found ndim=1? It works when I remove unroll=True, a param that I wouldn't expect to affect the LSTM's output dimension.

from keras.models import Sequential
from keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(
  100,
  batch_input_shape=(1, 1, 17),
  unroll=True
))
model.add(Dense(1))

I suppose it has to do with this:

In [9]: from keras.models import Sequential
   ...: from keras.layers import LSTM, Dense
   ...: from keras import backend as K
   ...: def LSTM_output_dimensions(*args,**kwargs):
   ...:   model = Sequential()
   ...:   model.add(LSTM(
   ...:     *args,
   ...:     **kwargs
   ...:   ))
   ...:   return K.ndim(model.outputs[0])
   ...:

In [10]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17))
Out[10]: 2

In [11]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17),return_sequences=True)
Out[11]: 3

In [12]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17),unroll=True)
Out[12]: 1

In [13]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17),unroll=True,return_sequences=True)
Out[13]: 2
1 Answers
Related