【问题标题】:Keras LSTM, expected 3 but got array with shape []Keras LSTM,预期为 3,但得到了形状为 [] 的数组
【发布时间】:2018-09-21 11:43:07
【问题描述】:

我正在尝试从带注释的文本中找出与单词相关的标签。我正在使用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 模型所需的适当维度。

【问题讨论】:

  • 我不了解您的模型数据。 sequence_length 和 n_words 的值是多少。你能添加model.summary()的输出吗?但无论如何,您的输出将大小为 50(最后一层的大小)。

标签: keras lstm rnn named-entity-extraction


【解决方案1】:

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

如何修复这个问题?

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

2) 去掉TimeDistributed层,直接应用Dense层。

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-07-18
    • 2020-06-18
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 2019-03-11
    • 2018-09-16
    • 2019-09-04
    • 2018-01-11
    相关资源
    最近更新 更多