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

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

这是我的代码:

    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'])

我正在构建一个具有可变输入的 LSTM this Whosebug question。但现在我的模型说 ValueError: The shape of the input to "Flatten" is not fully defined (got (None, 20)。我该如何解决这个问题?

提前致谢

您无法解决 这个特殊问题,因为您可以将可变大小的向量传递到Dense 层。 为什么?因为它有一个固定大小的权重矩阵,即内核W

您应该改为查看可以处理可变长度序列的层,例如 RNN。例如,您可以让 LSTM 学习整个序列的表示:

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

如果你想在你的模型中增加容量,你可以链接 RNN 层,只要最后一个不是 return 序列:

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