기존 NER 모델 학습시키기
spaCy 모델이 특정 데이터에서 잘 작동하지 않을 수 있어요. 한 가지 해결책은 우리 데이터로 모델을 다시 학습시키는 거예요. 이 연습 문제에서는 예측 성능을 향상하기 위해 NER 모델을 학습하는 과정을 실습해 볼게요.
nlp로 접근할 수 있는 spaCy en_core_web_sm 모델이 있으며, test 문자열에서 house를 엔티티로 올바르게 예측하지 못하고 있어요.
training_data가 주어졌을 때, 이 데이터를 두 번 반복(iterate)하면서 모델을 업데이트하는 단계를 작성하세요. 다른 파이프라인은 이미 비활성화되어 있고, optimizer도 사용할 준비가 되어 있어요. 에포크 수는 이미 2로 설정되어 있어요.
이 연습은 강의의 일부입니다
spaCy로 배우는 자연어 처리
연습 안내
optimizer객체를 사용하고, 각 에포크마다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])