我正在尝试在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中使用交叉验证,或者是否根本不需要使用?