훈련 데이터 준비

spaCy로 배우는 자연어 처리

Azadeh Mobasher

Principal data scientist

훈련 단계

 

  1. 입력 데이터를 주석 달아 준비합니다
  2. 모델 가중치를 초기화합니다
  3. 현재 가중치로 몇 개 예측합니다
  4. 예측을 정답과 비교합니다
  5. 옵티마이저로 성능이 좋아지도록 가중치를 계산합니다
  6. 가중치를 조금 업데이트합니다
  7. 3단계로 돌아갑니다.
spaCy로 배우는 자연어 처리

데이터 주석 및 준비

  • 첫 단계는 요구 형식으로 훈련 데이터를 준비하는 것입니다
  • 데이터를 수집한 뒤 주석(annotate) 합니다
  • 주석은 의도, 엔터티 등을 라벨링하는 것을 뜻합니다
  • 주석된 데이터 예시는 다음과 같습니다:
annotated_data = {
"sentence": "An antiviral drugs used against influenza is neuraminidase inhibitors.",
"entities": {
             "label": "Medicine",
             "value": "neuraminidase inhibitors",
    }
}
spaCy로 배우는 자연어 처리

데이터 주석 및 준비

  • 또 다른 주석 데이터 예시입니다:

 

annotated_data = {
"sentence": "Bill Gates visited the SFO Airport.",
"entities": [{"label": "PERSON", "value": "Bill Gates"}, 
             {"label": "LOC", "value": "SFO Airport"}]
}
spaCy로 배우는 자연어 처리

spaCy 훈련 데이터 형식

  • 데이터 주석은 모델이 학습할 내용을 위해 훈련 데이터를 준비합니다
  • 훈련 데이터셋은 딕셔너리로 저장해야 합니다:
training_data = [
("I will visit you in Austin.", {"entities": [(20, 26, "GPE")]}),
("I'm going to Sam's house.", {"entities": [(13,18, "PERSON"), (19, 24, "GPE")]}),
("I will go.", {"entities": []})
]

세 개의 예시 쌍:

  • 각 예시 쌍의 첫 요소는 문장입니다
  • 두 번째 요소는 주석된 엔터티와 시작·끝 문자 인덱스 목록입니다
spaCy로 배우는 자연어 처리

훈련용 Example 객체 데이터

  • 원시 텍스트를 그대로 spaCy에 넣을 수는 없습니다

  • 각 훈련 예시에 대해 Example 객체를 만들어야 합니다

import spacy
from spacy.training import Example

nlp = spacy.load("en_core_web_sm")

doc = nlp("I will visit you in Austin.")

annotations = {"entities": [(20, 26, "GPE")]} example_sentence = Example.from_dict(doc, annotations)
print(example_sentence.to_dict())
spaCy로 배우는 자연어 처리

연습해 봅시다!

spaCy로 배우는 자연어 처리

Preparing Video For Download...