sapo_cosmico
sapo_cosmico

Reputation: 6524

Incompatible input in Keras Layer LSTM

I'm trying to replicate the example on Keras's website:

# as the first layer in a Sequential model
model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
# now model.output_shape == (None, 32)
# note: `None` is the batch dimension.

# for subsequent layers, no need to specify the input size:
model.add(LSTM(16))

But when I run the following:

# only lines I've added: 
from keras.models import Sequential    
from keras.layers import Dense, LSTM  

# all else is the same: 
model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
model.add(LSTM(16))

However, I get the following:
ValueError: Input 0 is incompatible with layer lstm_4: expected ndim=3, found ndim=2

Versions:

Keras:      '2.0.5'  
Python:     '3.4.3'  
Tensorflow: '1.2.1'  

Upvotes: 1

Views: 463

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40516

LSTM layer as their default option has to return only the last output from a sequence. That's why your data loses its sequential nature. In order to change that try:

model.add(LSTM(32, input_shape=(10, 64), return_sequences=True))

What makes LSTM to return a whole sequence of predictions.

Upvotes: 2

Related Questions