TensorFlow:将 CRF 用于 NER形状不匹配[tensorflow_addons]

问题描述

我正在尝试在 CoNLL-2003 数据集上为 NER 构建 Bi-LSTM CRF 模型

我使用字符嵌入和 glove 嵌入对单词进行了编码,对于每个标记,我都有一个大小为 341 的嵌入

这是我的模型:

auth
def get_model(embed_size,max_seq_len,num_labels):

    #model
    input = Input(shape=(max_seq_len,embed_size),name="Input_Layer")
    model = Bidirectional(LSTM(units=75,return_sequences=True),name="Bi-LSTM")(input)  # variational biLSTM
    model = Timedistributed(Dense(75,activation="relu"),name="Bi-LSTM-out")(model)  # a dense layer as suggested by neuralNer
    crf = CRF(num_labels,name='CRF-layer')  # CRF layer
    out = crf(model)  # output
    model = Model(input,out)
    model.summary(line_length=150)

    f1 = tfa.metrics.F1score(num_classes=num_labels)

    model.compile(optimizer="adam",loss='categorical_crossentropy',metrics=['accuracy',f1])

    return model

模型摘要

model = get_model(embed_size=341,max_seq_len=16,num_labels=9)
model.fit(
        train_x,train_y
    )

输入形状: x 是 ______________________________________________________________________________________________________________________________________________________ Layer (type) Output Shape Param # ====================================================================================================================================================== Input_Layer (InputLayer) [(None,16,341)] 0 ______________________________________________________________________________________________________________________________________________________ Bi-LSTM (Bidirectional) (None,150) 250200 ______________________________________________________________________________________________________________________________________________________ Bi-LSTM-out (Timedistributed) (None,75) 11325 ______________________________________________________________________________________________________________________________________________________ CRF-layer (CRF) [(None,16),(None,9),),(9,9)] 783 ====================================================================================================================================================== Total params: 262,308 Trainable params: 262,308 Non-trainable params: 0 ______________________________________________________________________________________________________________________________________________________ 而 y 是 ((3250,341) 我正在训练 3250 个数据点,每个序列长度为 16,每个标记嵌入 341 个维度,并且可能有 9 个标签

现在我得到的错误是:

(3250,9))

我相信这是因为 CRF 输出ValueError: Shapes (None,9) and (None,16) are incompatible

有没有办法只获取输出的第二个元素?

或者有什么其他方法可以解决这个问题?

我正在使用来自 [(None,9)]

的 tf 2.0 + 和 CRF

我已经在 tf 1.15 中使用来自 keras-contrib 的 CRF 实现了这一点 [不想那样]

基于@MyStackRunnethOver 注释添加回溯:

from tensorflow_addons.layers import CRF

最后,我将在上午 8 点到晚上 8 点 [IST] 之间检查和实施这个问题以及所有可能/建议的解决方案,直到它得到解决,所以请帮忙!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)