我正在尝试使用 Keras 构建有状态的 LSTM,但我不明白如何在 LSTM 运行之前添加嵌入层。问题似乎是stateful
标志。如果我的网络不是有状态的,则添加嵌入层非常简单并且有效。
一个没有嵌入层的工作状态 LSTM 看起来像这样:
model = Sequential()
model.add(LSTM(EMBEDDING_DIM,
batch_input_shape=(batchSize, longest_sequence, 1),
return_sequences=True,
stateful=True))
model.add(TimeDistributed(Dense(maximal_value)))
model.add(Activation('softmax'))
model.compile(...)
添加嵌入层时,我将batch_input_shape
参数移动到嵌入层,即只有第一层需要知道形状?像这样:
model = Sequential()
model.add(Embedding(vocabSize+1, EMBEDDING_DIM,batch_input_shape=(batchSize, longest_sequence, 1),))
model.add(LSTM(EMBEDDING_DIM,
return_sequences=True,
stateful=True))
model.add(TimeDistributed(Dense(maximal_value)))
model.add(Activation('softmax'))
model.compile(...)
我知道的例外是Exception: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
所以我现在被困在这里。将词嵌入组合到有状态 LSTM 中的技巧是什么?