我想训练带有以下(text, label)
对的TextCategorizer模型。
标签颜色:
标签 ANIMAL :
我正在复制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.")
等等。 gold1
和gold2
应该包含哪些内容?我猜他们应该是GoldParse个对象,但我不知道你是如何在那些中表示文本分类信息的。
答案 0 :(得分:6)
根据此示例train_textcat.py,如果您想训练多标签模型,它应该类似于{'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)