【发布时间】: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