如何訓練
- 初始化模型權重使其變成隨機值:調用
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