Input Shape Error in Second-layer (but not first) of Keras LSTM

前端 未结 1 378
旧时难觅i
旧时难觅i 2020-12-19 16:43

I am trying to build an LSTM model, working off the documentation example at https://keras.io/layers/recurrent/

from keras.models import Sequential
from kera         


        
相关标签:
1条回答
  • 2020-12-19 17:08

    Thanks to patyork for answering this on Github:

    the second LSTM layer is not getting a 3D input that it expects (with a shape of (batch_size, timesteps, features). This is because the first LSTM layer has (by fortune of default values) return_sequences=False, meaning it only output the last feature set at time t-1 which is of shape (batch_size, 32), or 2 dimensions that doesn't include time.

    So to offer a code example of how to use a stacked LSTM to achieve many-to-one (return_sequences=False) sequence classification, just make sure to use return_sequences=True on the intermediate layers like this:

    model = Sequential()
    model.add(LSTM(32, input_dim=64, input_length=10, return_sequences=True))
    model.add(LSTM(24, return_sequences=True))
    model.add(LSTM(16, return_sequences=True))
    model.add(LSTM(1,  return_sequences=False))
    
    model.compile(optimizer = 'RMSprop', loss = 'categorical_crossentropy')
    

    (no errors)

    0 讨论(0)
提交回复
热议问题