如何训练
- 初始化模型权重使其变成随机值:调用
nlp.begin_training
方法; - 查看当前权重的表现:调用
nlp.update
方法 - 比较预测结果和真实的标签;
- 计算如何调整权重来改善预测结果;
- 微调模型权重;
- 重复上述步骤;
循环训练:
for i in range(10):
random.shuffle(TRAINING_DATA)
for batch in spacy.util.minibatch(TRAINING_DATA):
texts = [text for text, annoation in batch]
annotations = [annotation for text, annotation in batch]
nlp.update(texts, annotations)
nlp.to_disk(path_to_model)
训练一个新的模型:
nlp = spacy.blank("zh")
ner = nlp.create_pipe("ner")
nlp.add_pipe(ner)
ner.add_label("GADGET")
nlp.begin_training()
for itn in range(10):
random.shuffle(examples)
for batch in spacy.util.minibatch(examples, size=2):
texts = [text for text, annoation in batch]
annotations = [annotation for text, annotation in batch]
nlp.update(texts, annotations)
模型训练会出现的问题:
- 将之前的正确预测结果混合进来
TRAINING_DATA = [
("...", {"entities": [(0,1, "WEBSITE")]}),
("...", {"entities": [(0,1, "PERSON")]})
]
- 模型不能学会所有的东西
选择那些能从本地语境中反映出类别的类型;
更通用的标签要好过更特定的标签;
可以用规则将通用标签转换为特定种类;
LABELS = ["CLOTHING", "BAND"]
手动标注工具:
- Brat
- Prodigy