Search code examples
pythonkeraslstmtext-classificationvariable-length

"ValueError: The shape of the input to "Flatten" is not fully defined" with variable length LSTM


Here's my code:

    from keras.layers import LSTM, Bidirectional, Dense, Input, Flatten
    from keras.models import Model

    input = Input(shape=(None, 100))
    lstm_out = Bidirectional(LSTM(10, return_sequences=True))(input)
    something = Flatten()(lstm_out)
    output = Dense(22, activation='softmax')(something)

    model = Model(inputs=input, outputs=output)
    model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])

I'm building an LSTM with variable input through this stackoverflow question. But now my model is saying ValueError: The shape of the input to "Flatten" is not fully defined (got (None, 20). How can I fix this?

Thanks in advance


Solution

  • You can't fix this particular problem because you can pass a variable size vector to a Dense layer. Why? Because it has a fixed size weights matrix, i.e. the kernel W.

    You should instead look at layers that can handle variable length sequences such as RNNs. For example you can let the LSTM learn a representation over the entire sequence:

    input = Input(shape=(None, 100))
    lstm_out = Bidirectional(LSTM(10))(input) # the LSTM produces a single fixed size vector
    output = Dense(22, activation='softmax')(lstm_out) # Dense classifies it
    

    If you want more capacity in your model you can chain RNN layers so long as the last one doesn't return sequences:

    lstm_out = Bidirectional(LSTM(10, return_sequences=True))(input)
    lstm_out = Bidirectional(LSTM(10))(lstm_out) # this LSTM produces a single vector