python - 如何正确更新 spaCy 中的模型?

标签 python machine-learning spacy nlp

我想用新实体更新模型。我正在加载“pt”NER 模型,并尝试更新它。 在做任何事情之前,我尝试了这句话:“meu nome é Mário e hoje eu vou para academia”。 (在英语中,这句话是“我的名字是马里奥,今天我要去健身房)。 在整个过程之前我得到了这个:

Entities [('Mário', 'PER')]
Tokens [('meu', '', 2), ('nome', '', 2), ('é', '', 2), ('Mário', 'PER', 3), ('e', '', 2), ('hoje', '', 2), ('eu', '', 2), ('vou', '', 2), ('pra', '', 2), ('academia', '', 2)]

好吧,Mário 是一个名字,而且是正确的。 但我希望模型将“hoje(今天)”识别为日期,然后我运行下面的脚本。

运行脚本后,我尝试了相同的设置并得到了以下结果:

Entities [('hoje', 'DATE')]
Tokens [('meu', '', 2), ('nome', '', 2), ('é', '', 2), ('Mário', '', 2), ('e', '', 2), ('hoje', 'DATE', 3), ('eu', '', 2), ('vou', '', 2), ('pra', '', 2), ('academia', '', 2)]

模型将“hoje”识别为日期,但完全忘记了马里奥作为人。

from __future__ import unicode_literals, print_function
import plac
import random
from pathlib import Path
import spacy
from spacy.util import minibatch, compounding

# training data
TRAIN_DATA = [
    ("Infelizmente não, eu briguei com meus amigos hoje", {"entities": [(45, 49, "DATE")]}),
    ("hoje foi um bom dia.", {"entities": [(0, 4, "DATE")]}),
    ("ah não sei, hoje foi horrível", {"entities": [(12, 16, "DATE")]}),
    ("hoje eu briguei com o Mário", {"entities": [(0, 4, "DATE")]})
]


@plac.annotations(
    model=("Model name. Defaults to blank 'en' model.", "option", "m", str),
    output_dir=("Optional output directory", "option", "o", Path),
    n_iter=("Number of training iterations", "option", "n", int),
)

def main(model="pt", output_dir="/model", n_iter=100):
    """Load the model, set up the pipeline and train the entity recognizer."""
    if model is not None:
        nlp = spacy.load(model)  # load existing spaCy model
        print("Loaded model '%s'" % model)
    else:
        nlp = spacy.blank("pt")  # create blank Language class
            print("Created blank 'en' model")

    doc = nlp("meu nome é Mário e hoje eu vou pra academia")
    print("Entities", [(ent.text, ent.label_) for ent in doc.ents])
    print("Tokens", [(t.text, t.ent_type_, t.ent_iob) for t in doc])

    # 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)
    # otherwise, get it so we can add labels
    else:
        ner = nlp.get_pipe("ner")

    # 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"]
    with nlp.disable_pipes(*other_pipes):  # only train NER
        # reset and initialize the weights randomly – but only if we're
        # training a new model
        if model is None:
            nlp.begin_training()
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            losses = {}
            # batch up the examples using spaCy's minibatch
            batches = minibatch(TRAIN_DATA, size=compounding(4.0, 32.0, 1.001))
            for batch in batches:
                texts, annotations = zip(*batch)
                nlp.update(
                    texts,  # batch of texts
                    annotations,  # batch of annotations
                    drop=0.5,  # dropout - make it harder to memorise data
                    losses=losses,
                )
            print("Losses", losses)

    # test the trained model
   # for text, _ in TRAIN_DATA:
    doc = nlp("meu nome é Mário e hoje eu vou pra academia")
    print("Entities", [(ent.text, ent.label_) for ent in doc.ents])
    print("Tokens", [(t.text, t.ent_type_, t.ent_iob) for t in doc])

    # save model to output directory
    if output_dir is not None:
        output_dir = Path(output_dir)
        if not output_dir.exists():
            output_dir.mkdir()
        nlp.to_disk(output_dir)
        print("Saved model to", output_dir)

        # test the saved model
        print("Loading from", output_dir)
        nlp2 = spacy.load(output_dir)
        # for text, _ in TRAIN_DATA:
        #     doc = nlp2(text)
        #     print("Entities", [(ent.text, ent.label_) for ent in doc.ents])
        #     print("Tokens", [(t.text, t.ent_type_, t.ent_iob) for t in doc])

最佳答案

在训练数据中,您需要将“Mario”称为“PER”。如果您错过了,该模型将从新的训练数据中学习,将“Mario”排除为“PER”。

(注意:您应该提及训练数据中句子中存在的所有实体,而不仅仅是新实体。)

关于python - 如何正确更新 spaCy 中的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55993817/

相关文章:

python - 遍历字典时使用 keys() 函数的优点

python - Autobahn 和 Twisted,调用 wamp 客户端方法并通过 HTTP 获取结果

python - 根据 pandas 中的另一列对唯一值进行排序

machine-learning - 如何将 ML VectorUDT 功能从 .mllib 转换为 .ml 类型

SpaCy 相似度得分没有意义

python - 在python中实现合并排序时出现错误

powershell - 找不到 Azure Get-AzureRmMlService Powershell

c++ - 在 C++ 中处理类间内存释放

python - spaCy 的正则表达式与 Python 的正则表达式不同

python-2.7 - 使用 pytextrank 时 Spacy 中的值错误(textrank 的 Python 实现)