classifier-multi-label icon indicating copy to clipboard operation
classifier-multi-label copied to clipboard

训练结束后predict.py脚本获取不到标签问题

Open bree2eC opened this issue 3 years ago • 7 comments

训练结束后predict.py脚本获取不到标签问题 知乎上也有很多人说predict.py脚本获取到的标签为空,其实不是训练数据有问题或者轮次不够,作者的get_label 函数逻辑有一些小小的问题,我这里简单修改了一下,可以成功获取到标签,新的predict.py 的get_label 函数如下:

def get_label(sentence):
    """
    Prediction of the sentence's label.
    """
    feature = get_feature_test(sentence)
    fd = {MODEL.albert.input_ids: [feature[0]],
          MODEL.albert.input_masks: [feature[1]],
          MODEL.albert.segment_ids:[feature[2]],
          }
    prediction = MODEL.sess.run(MODEL.albert.predictions, feed_dict=fd)[0]
    print(prediction)
    r=[]
    for i in range(len(prediction)):
        if prediction[i]!=0.0:
            r.append(id2label(i))
    return r
    #return [id2label(l) for l in np.where(prediction==1)[0] if l!=0]

bree2eC avatar May 16 '21 15:05 bree2eC

建议打印prediction看看

hellonlp avatar May 31 '21 03:05 hellonlp

训练结束后predict.py脚本获取不到标签问题 知乎上也有很多人说predict.py脚本获取到的标签为空,其实不是训练数据有问题或者轮次不够,作者的get_label 函数逻辑有一些小小的问题,我这里简单修改了一下,可以成功获取到标签,新的predict.py 的get_label 函数如下:

def get_label(sentence):
    """
    Prediction of the sentence's label.
    """
    feature = get_feature_test(sentence)
    fd = {MODEL.albert.input_ids: [feature[0]],
          MODEL.albert.input_masks: [feature[1]],
          MODEL.albert.segment_ids:[feature[2]],
          }
    prediction = MODEL.sess.run(MODEL.albert.predictions, feed_dict=fd)[0]
    print(prediction)
    r=[]
    for i in range(len(prediction)):
        if prediction[i]!=0.0:
            r.append(id2label(i))
    return r
    #return [id2label(l) for l in np.where(prediction==1)[0] if l!=0]

您好,按照您的修改之后我的输出都是第一个标签了...我看了下任何句子的prediction几乎相等,这是什么原因呢...

nomel0921 avatar May 03 '22 08:05 nomel0921

请问你这个问题解决了吗?我也遇到了,prediction结果都是[]。

lonkecxd avatar May 24 '22 05:05 lonkecxd

我的第一个标签是空标签"|",所以会不一样

hellonlp avatar May 24 '22 06:05 hellonlp

大家有用起来么?要不要加个QQ好友交流一下?qq:1840658279

xtuyaowu avatar Mar 12 '23 01:03 xtuyaowu

prediction打印出来全是0......

rio5050 avatar May 06 '23 06:05 rio5050

是正常的,说明预测结果为空

hellonlp avatar May 16 '23 08:05 hellonlp