如何为 TextCategorizer 训练创建黄金数据?

How do I create gold data for TextCategorizer training?

我想用以下 (text, label) 对训练 TextCategorizer 模型。

标签颜色:

标签动物:

我正在复制 documentation for TextCategorizer 中的示例代码。

textcat = TextCategorizer(nlp.vocab)
losses = {}
optimizer = nlp.begin_training()
textcat.update([doc1, doc2], [gold1, gold2], losses=losses, sgd=optimizer)

doc 变量可能只是 nlp("The door is brown.") 等等。 gold1gold2 中应该有什么?我猜它们应该是 GoldParse 个对象,但我看不出你是如何在其中表示文本分类信息的。

根据此示例 train_textcat.py 如果您想训练 multi-label 模型,它应该类似于 {'cats': {'ANIMAL': 0, 'COLOR': 1}}。另外,如果你只有两个 类,你可以简单地使用 {'cats': {'ANIMAL': 1}} 作为标签 ANIMAL{'cats': {'ANIMAL': 0}} 作为标签 COLOR.

您可以将以下最小工作示例用于单类别文本分类;

import spacy

nlp = spacy.load('en')

train_data = [
    (u"That was very bad", {"cats": {"POSITIVE": 0}}),
    (u"it is so bad", {"cats": {"POSITIVE": 0}}),
    (u"so terrible", {"cats": {"POSITIVE": 0}}),
    (u"I like it", {"cats": {"POSITIVE": 1}}),
    (u"It is very good.", {"cats": {"POSITIVE": 1}}),
    (u"That was great!", {"cats": {"POSITIVE": 1}}),
]


textcat = nlp.create_pipe('textcat')
nlp.add_pipe(textcat, last=True)
textcat.add_label('POSITIVE')
optimizer = nlp.begin_training()
for itn in range(100):
    for doc, gold in train_data:
        nlp.update([doc], [gold], sgd=optimizer)

doc = nlp(u'It is good.')
print(doc.cats)