使用Spacy进行命名实体识别的交叉验证

3
我正在尝试在5000万个样本上训练自定义NER模型。我正在使用带有20次迭代的minibatch进行建模。我想了解是否应该使用交叉验证来获得更准确的样本外精度。如果是,那么交叉验证步骤应该在哪里进行?如果不是,则如何拆分/分配我的训练和测试数据,因为我正在使用注释和6个自定义实体,很难跟踪每个训练和测试数据中注释标签的百分比,并均匀地分布它们。
这是我用于训练的代码 -
def train_spacy(data, iterations):
    TRAIN_DATA = data

    # create blank Language class
    nlp = spacy.blank('en')  

    # create the built-in pipeline components and add them to the pipeline
    # nlp.create_pipe works for built-ins that are registered with spaCy
    if 'ner' not in nlp.pipe_names:
        ner = nlp.create_pipe('ner')
        nlp.add_pipe(ner, last=True)

    # Add LABELS
    for _, annotations in TRAIN_DATA:
         for ent in annotations.get('entities'):
            ner.add_label(ent[2])

    # Get names of other pipes to disable them during training
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']

    # only train NER
    with nlp.disable_pipes(*other_pipes):  
        optimizer = nlp.begin_training()
        for itn in range(iterations):
            print("Starting iteration " + str(itn))

            for batch in batches:
                texts, annotations = zip(*batch)
                nlp.update(texts, annotations, sgd=optimizer, 
                           drop=0.20,losses=losses)
            print('Losses', losses)

    return nlp


if __name__ == "__main__":

    # Train formatted data
    model = train_spacy(data, 10)

我认为交叉验证步骤应该发生在for循环迭代的某个地方,但我不确定。有人能否详细说明如何在Spacy NER中使用交叉验证,或者是否根本不需要使用?

1个回答

2
理想情况下,您应该将训练数据集的一部分拆分为“开发集”,并使用该集合中的所有实体来调整超参数。
如果您随机选择比例(确保不偏向日期或名称),则预期实体的分布也大致相同。最好不要过度设计此拆分,而是进行真正的随机抽样。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接