我尝试训练数据集:
[('文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据.', {'实体': [(7, 19, '人'), (89, 91, '人'), (98, 101, '人')]}), ('"文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据.', {'实体': [(119, 137, '人')]}),]
n_iter = 8
nlp = spacy.blank('en')
ner = nlp.create_pipe('ner')
for _, annotations in TRAIN_DATA:
for _s, _e, label in annotations.get('entities', []) :
print('Adding label - "', label, '"')
ner.add_label(label)
from spacy.training.example import Example
from spacy.util import minibatch, compounding
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
with nlp.disable_pipes(*other_pipes):
optimizer = nlp.begin_training()
for itn in range(n_iter):
random.shuffle(TRAIN_DATA)
losses = {}
for batch in spacy.util.minibatch(TRAIN_DATA, size=compounding(4.0, 32.0, 1.001)):
for text, annotations in batch:
doc = nlp.make_doc(text)
example = Example.from_dict(doc, annotations)
nlp.update([example], drop=0.35,losses=losses, sgd=optimizer)
print('losses -', losses)
结果是 损失 - {}
直到这么多次迭代
谁知道哪一个是错的?
最佳答案
您实际上还没有将 NER 组件添加到您的管道中。将 nlp.create_pipe
替换为:
ner = nlp.add_pipe("ner")
(请注意,您在此设置中训练的是单个示例而不是成批示例,因此批处理代码没有做任何有用的事情。查看 NER demo projects 了解更多示例,了解如何执行此操作使用 train CLI,它具有更灵活和优化的训练循环。)
关于python - spacy 3 train 自定义 ner 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69085123/