python - spacy 3 train 自定义 ner 模型

标签 python named-entity-recognition spacy-3

我尝试训练数据集:

[('文本数据文本数据文本数据文本数据文本数据文本数据文本数据文本数据.', {'实体': [(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/

相关文章:

python - python中两个不同数据帧的散点图数据

java - 斯坦福 NER - 提取多词实体

python - spaCy,准备训练数据 : doc. char_span 返回 'None'

python - DocBin to_bytes/to_disk 被杀死

python从两个目录文件中导入一个

python - 将 Pandas 数据框单元格中的字典解析为新行单元格(新列)

java - 如何在 OpenNLP 中通过 NER 识别印度名字?

python-3.x - 使用 Spacy 训练 NER 从 Resume 中提取技能。过渡中的 U-entity_name 是什么意思

python - 使用 SpaCy Displaced 可视化定制 NER 标签

python - 将系列中的值映射到列上以替换 nan 值 pandas