【问题标题】:How to train own model and test it with spacy如何训练自己的模型并用 spacy 测试它
【发布时间】:2019-02-28 17:11:05
【问题描述】:

我正在使用下面的代码来训练一个已经存在的 spacy ner 模型。但是,我在测试中没有得到正确的结果:

我错过了什么?

import spacy
import random
from spacy.gold import GoldParse
from spacy.language import EntityRecognizer

train_data = [
    ('Who is Rocky babu?', [(7, 16, 'PERSON')]),
    ('I like London and Berlin.', [(7, 13, 'LOC'), (18, 24, 'LOC')])
]

nlp = spacy.load('en', entity=False, parser=False)
ner = EntityRecognizer(nlp.vocab, entity_types=['PERSON', 'LOC'])

for itn in range(5):
    random.shuffle(train_data)
    for raw_text, entity_offsets in train_data:
        doc = nlp.make_doc(raw_text)
        gold = GoldParse(doc, entities=entity_offsets)

        nlp.tagger(doc)
        nlp.entity.update([doc], [gold])
Now, When i try to test the above model by using the below code, I don't get the expected output.

text = ['Who is Rocky babu?']

for a in text:
        doc = nlp(a)
        print("Entities", [(ent.text, ent.label_) for ent in doc.ents])
My output is as follows:

Entities []
whereas my expected output is as follows:

Entities [('Rocky babu', 'PERSON')]
Can someone please tell me what I'm missing ?

【问题讨论】:

  • 您能否更具体地说明您获得的结果和预期的结果?
  • 我的输出如下:目前,当我使用与测试数据相同的训练数据时,我得到的实体是空白的。实体 [].. 而我的期望如下:实体[('Rocky babu', 'PERSON')] 谁能告诉我我错过了什么?

标签: nltk spacy named-entity-recognition


【解决方案1】:

你能重试吗

nlp = spacy.load('en_core_web_sm', entity=False, parser=False)

如果由于您没有安装该模型而导致错误,您可以运行

python -m spacy download en_core_web_sm

首先在命令行上。

当然,请记住,要对模型进行适当的训练,您需要更多示例才能使模型能够泛化!

【讨论】:

  • 感谢您的回复。我更改了代码以包含您在上面给出的行,但仍在训练之后,而测试我的模型目前并未将 ROCKY BABU 识别为人。我有几个问题希望你能帮我解答。我上面的代码训练和测试是否正确?我的理解是否正确,使用同一行来训练/测试应该给我正确的实体?
  • 代码原则上是正确的,当我用nlp的代码行替换你对nlp = 的定义时,它运行正常。尽管代码在技术上是正确的,但请记住,对于现实世界的问题,您至少需要数百个示例来训练您的模型。在这种情况下,您的模型可能并不总是能够准确地复制每个训练示例。相反,ML 模型应该概括它在训练数据中看到的模式,而不会过度拟合相同的数据。
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 2019-05-24
  • 1970-01-01
  • 2019-11-08
  • 2016-08-17
相关资源
最近更新 更多