【问题标题】:Bi-directional LSTM for entity recognition用于实体识别的双向 LSTM
【发布时间】:2019-08-15 14:43:39
【问题描述】:

在一篇论文之后,我使用词嵌入作为实体识别的特征向量。

我尝试使用 Keras 构建网络,但遇到了我似乎无法解决的维度问题。

举以下例句:

["I went to the shop"]

句子有5个词,计算特征矩阵后,我留下一个维度矩阵:(1, 120, 1000) == (#examples, sequence_length, embedding)

请注意,sequence_length 在未完成时会附加 0. 填充。在本例中,实际的sequence_length 为 5。

我的网络架构如下:

enc = encode() 
claims_input = Input(shape=(120, 1000), dtype='float32', name='claims')
x = Masking(mask_value=0., input_shape=(120, 1000))(claims_input)
x = Bidirectional(LSTM(units=512, return_sequences=True, recurrent_dropout=0.2, dropout=0.2))(x)
x = Bidirectional(LSTM(units=512, return_sequences=True, recurrent_dropout=0.2, dropout=0.2))(x)
out = TimeDistributed(Dense(8, activation="softmax"))(x)

model = Model(inputs=claims_input, output=out)
model.compile(loss="sparse_categorical_crossentropy", optimizer='adam', metrics=["accuracy"])
model.fit(enc, y)

架构很简单,我屏蔽了特定的时间步长,运行两个双向 LSTM,然后是一个 softmax 输出。在这种情况下,我的y 变量是一个(9,8) one-hot-encoded 矩阵,对应于每个单词的黄金标签。

当尝试fit() 这个模型时,我遇到了与TimeDistributed() 层相关的维度问题,我不确定如何解决,甚至开始调试。

错误:ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, but got array with shape (9, 8)

任何帮助将不胜感激。

【问题讨论】:

  • 根据您的型号,y 应该是(sentences, 120, 8)
  • 在这种情况下我应该填充我的 y 矩阵吗?
  • 是的,你应该这样做。
  • 这是我尺寸错误的原因吗?
  • 如果答案解决了您的问题,请接受点击答案旁边的复选标记 (✔) 将其标记为“已回答” - 请参阅 What should I do when someone answers my question?

标签: python keras lstm recurrent-neural-network named-entity-recognition


【解决方案1】:

您正在进行实体识别。因此,输入序列中的每个元素都将被分配一个实体(可能其中一些为空)。如果您的模型采用形状为 (120, n_features) 的输入样本,则输出也必须是长度为 120 的序列,即每个元素一个实体。因此,您提供给模型的标签(即y)的形状必须为(n_samples, 120, n_entities)(或(n_samples, 120, 1),如果您使用稀疏标签)。

旁注:TimeDistributed(Dense(...))Dense(...) 之间没有区别,就像Dense layer is applied on the last axis

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 1970-01-01
    • 2013-10-19
    • 2016-01-09
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多