【发布时间】:2021-11-23 12:30:26
【问题描述】:
在阅读了docs 并完成了tutorial 之后,我想我会做一个小演示。原来我的模型不想训练。这是代码
import spacy
import random
import json
TRAINING_DATA = [
["My little kitty is so special", {"KAT": True}],
["Dude, Totally, Yeah, Video Games", {"KAT": False}],
["Should I pay $1,000 for the iPhone X?", {"KAT": False}],
["The iPhone 8 reviews are here", {"KAT": False}],
["Noa is a great cat name.", {"KAT": True}],
["We got a new kitten!", {"KAT": True}]
]
nlp = spacy.blank("en")
category = nlp.create_pipe("textcat")
nlp.add_pipe(category)
category.add_label("KAT")
# Start the training
nlp.begin_training()
# Loop for 10 iterations
for itn in range(100):
# Shuffle the training data
random.shuffle(TRAINING_DATA)
losses = {}
# Batch the examples and iterate over them
for batch in spacy.util.minibatch(TRAINING_DATA, size=2):
texts = [text for text, entities in batch]
annotations = [{"textcat": [entities]} for text, entities in batch]
nlp.update(texts, annotations, losses=losses)
if itn % 20 == 0:
print(losses)
当我运行它时,输出表明我学到的东西很少。
{'textcat': 0.0}
{'textcat': 0.0}
{'textcat': 0.0}
{'textcat': 0.0}
{'textcat': 0.0}
这感觉错误。应该有错误或有意义的标签。预测证实了这一点。
for text, d in TRAINING_DATA:
print(text, nlp(text).cats)
# Dude, Totally, Yeah, Video Games {'KAT': 0.45303162932395935}
# The iPhone 8 reviews are here {'KAT': 0.45303162932395935}
# Noa is a great cat name. {'KAT': 0.45303162932395935}
# Should I pay $1,000 for the iPhone X? {'KAT': 0.45303162932395935}
# We got a new kitten! {'KAT': 0.45303162932395935}
# My little kitty is so special {'KAT': 0.45303162932395935}
感觉我的代码缺少了一些东西,但我不知道是什么。
【问题讨论】:
-
Here 他们使用了 2000 个示例。你确定机器学习适用于 6 个例子吗?您的所有三个猫示例都对猫使用了不同的词。我会从 10 个不同的例子开始,只用一个词来形容一只猫。
-
当然,但是 textcat 类别报告零损失,这不应该是这样。
-
你的训练循环和数据看起来是正确的——我想我发现了问题:尝试将
{"textcat": [entities]}更改为{"cats": entities}(如果你传入一个dict,也可以将see here作为预期的键注释)。当你更新文本分类器时,它会寻找一个键"cats"——但那个键不存在,只有"textcat"。所以你基本上什么都没有更新文本分类器,最后只得到了随机初始化的权重(来自nlp.begin_training)。