訓練既有的 NER 模型
一個 spaCy 模型在特定資料上可能表現不佳。解法之一是用我們的資料來訓練模型。在這個練習中,你會實作訓練一個 NER 模型,以提升它的預測表現。
已提供可作為 nlp 使用的 spaCy en_core_web_sm 模型,但它無法在 test 字串中正確預測 house 為實體。
給定 training_data,請撰寫步驟,在遍歷資料兩次的過程中更新這個模型。其他管線已停用,optimizer 也可直接使用。epoch 數已設定為 2。
本練習屬於課程
使用 spaCy 的自然語言處理
練習說明
- 使用
optimizer物件,並在每個 epoch 中,透過random套件隨機洗牌資料集,然後建立Example物件。 - 使用
.update屬性更新nlp模型,並將sgd參數設定為使用該 optimizer。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
nlp = spacy.load("en_core_web_sm")
print("Before training: ", [(ent.text, ent.label_) for ent in nlp(test).ents])
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
nlp.disable_pipes(*other_pipes)
optimizer = nlp.create_optimizer()
# Shuffle training data and the dataset using random package per epoch
for i in range(epochs):
random.____(training_data)
for text, ____ in training_data:
doc = nlp.____(____)
# Update nlp model after setting sgd argument to optimizer
example = Example.____(____, ____)
nlp.____([____], sgd = ____)
print("After training: ", [(ent.text, ent.label_) for ent in nlp(test).ents])