0

我正在尝试从带注释的文本中找出与单词相关的标签。我正在使用一个bidirectional LSTM. 我有X_train它的形状(1676, 39)Y_train相同的形状(1676, 39)

input = Input(shape=(sequence_length,))
model = Embedding(input_dim=n_words, output_dim=20,
              input_length=sequence_length, mask_zero=True)(input)
model = Bidirectional(LSTM(units=50, return_sequences=True,
                       recurrent_dropout=0.1))(model)
out_model = TimeDistributed(Dense(50, activation="softmax"))(model) 
model = Model(input, out_model)
model.compile(optimizer="rmsprop", loss= "categorical_crossentropy", metrics=["accuracy"])
model.fit(X_train, Y_train, batch_size=32, epochs= 10,
                validation_split=0.1)

执行此操作时,我收到错误:

ValueError: Error when checking target: expected time_distributed_5 to have 3 dimensions, but got array with shape (1676, 39).

我无法找出如何提供 Keras LSTM 模型所需的适当尺寸。

4

1 回答 1

0

结果,在LSTM你设置return_sequences=True的层中,该层的输出是一个形状为 [ batch_size * 39 * 50 ] 的张量。然后将此张量传递给TimeDistributed层。TimeDistributed在每个时间戳上应用密集层。该层的输出也是 [ batch_size * 39 * 50 ]。如您所见,您通过 3 维张量进行预测,而您的基本事实是 2 维 (1676, 39)。

如何解决问题?

1)return_sequences=True从 LSTM 参数中删除。

2) 移除TimeDistributed层并直接应用密集层。

inps = keras.layers.Input(shape=(39,))
embedding = keras.layers.Embedding(vocab_size, 16)(inps)
rnn = keras.layers.LSTM(50)(embedding)
dense = keras.layers.Dense(50, activation="softmax")(rnn)
prediction = keras.layers.Dense(39, activation='softmax')(dense)
于 2018-09-21T15:36:28.033 回答