【问题标题】:"ValueError: The shape of the input to "Flatten" is not fully defined" with variable length LSTM“ValueError:“Flatten”的输入形状未完全定义”,具有可变长度 LSTM
【发布时间】:2019-04-01 08:58:51
【问题描述】:

这是我的代码:

    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 stackoverflow question。但现在我的模型说ValueError: The shape of the input to "Flatten" is not fully defined (got (None, 20)。我该如何解决这个问题?

提前致谢

【问题讨论】:

  • 如果在图创建时时间步数为unknown,则不能应用Flatten - 最后一个Dense 层有多少输入单元?

标签: python keras lstm text-classification variable-length


【解决方案1】:

您无法修复这个特殊问题,因为您可以将可变大小的向量传递给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 层,只要最后一层不返回序列即可:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 2019-08-23
    • 2019-07-06
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    相关资源
    最近更新 更多