ฝึกโมเดล NER ที่มีอยู่แล้ว
บางครั้งโมเดล spaCy อาจทำงานได้ไม่ดีกับข้อมูลเฉพาะของเรา วิธีแก้คือการฝึกโมเดลด้วยข้อมูลของเราเอง ในแบบฝึกหัดนี้ จะได้ฝึกการ train โมเดล NER เพื่อปรับปรุงประสิทธิภาพการทำนาย
โมเดล en_core_web_sm ของ spaCy ที่เข้าถึงผ่านตัวแปร nlp ยังไม่สามารถทำนายคำว่า house ให้เป็น entity ได้อย่างถูกต้องในสตริง test
โดยมี training_data ให้พร้อมแล้ว ให้เขียนขั้นตอนเพื่ออัปเดตโมเดลนี้โดยวนซ้ำข้อมูล 2 รอบ pipeline อื่น ๆ ถูก disable ไว้แล้ว และ optimizer พร้อมใช้งาน โดยจำนวน epoch ตั้งไว้เป็น 2 แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การประมวลผลภาษาธรรมชาติด้วย spaCy
คำแนะนำการฝึกหัด
- ใช้ object
optimizerและสำหรับแต่ละ epoch ให้สุ่มลำดับชุดข้อมูลด้วยแพ็กเกจrandomจากนั้นสร้าง object ประเภทExample - อัปเดตโมเดล
nlpด้วย attribute.updateและกำหนด argumentsgdให้ใช้ 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])