> For the complete documentation index, see [llms.txt](https://lswkim322.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lswkim322.gitbook.io/til/til-ml/boostcamp/p-stage-mrc/2-extraction-based-mrc.md).

# (2강) Extraction-based MRC

> * 추출 기반으로 기계독해를 푸는 방법에 대해 학습
> * 추출 기반으로 기계독해 문제에 접근한다는 것의 의미
> * 추출 기반 기계독해를 어떻게 풀수 있는지
>
> **Futher Reading**
>
> * [SQuAD 데이터셋 둘러보기](https://rajpurkar.github.io/SQuAD-explorer/)
> * [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805)
> * [The Illustrated BERT, ELMo, and co. (How NLP Cracked Transfer Learning](http://jalammar.github.io/illustrated-bert/))
> * [Huggingface datasets](https://huggingface.co/datasets)

## 1. Extraction-based MRC

### 1.1 Extraction-based MRC 문제 정의

질문(Question)의 답변(answer)이 항상 주어진 지문 내에 span 으로 존재하는 케이스

### 1.2. Overview

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2FuRw24UUADGaAsSw7W6PF%2Fimage.png?alt=media\&token=4962b8c6-4370-4734-86c3-55af278979cc)

## 2. Pre-processing

### 2.1. Tokenization

텍스트를 작은 단위 (Token)으로 나누는 것

* 띄어쓰기 기준, 형태소, subword 등 여러 단위의 토큰 기준이 사용됨
* 최근엔 Out-Of-Vocabulary(OOV) 문제를 해결해주고 정보학적으로 이점을 가진 Byte Pair Encoding 을 주로 사용함
* 본강에선 BPE방법론중 하나인 WordPiece Tokenizer를 사용
  * 미국 군대 내 두번째로 높은 직위는 무엇인가
  * ‘미국’, ‘군대’, ‘내’, ‘두번째’, ‘##로’, 높은, ‘직’, ‘##위는’, ’무엇인가’, ‘?’

### 2.2. Special Token

* **\[CLS]: 문장의 함축적 의미를 담거나 시작을 의미하는 Token**
* **\[SEP]: Question과 Context를 구분하는 Token**
* **\[PAD]: 문장의 길이를 맞추기 위해 사용하는 의미가 없는 Token**
* **\[UNK]: Unknown Token**

### **2.3. Attention mask**

* 입력 시퀸스 중에서 attention을 연산할 때 무시할 토큰을 표시
* 0은 무시, 1은 연산에 포함
* 보통 \[PAD]와 같은 의미가 없는 특수 토큰을 무시하기 위해 사

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2FSOVlAIMfIthBHuqEWVjO%2Fimage.png?alt=media\&token=cfcc9c2b-67fd-488f-a20c-a75e5797a6cc)

### 2.4. Token Type IDs

입력이 2개이상의 시퀸스 일 때 (예: 질문 & 지문), 각각에게 ID를 부여하여 모델이 구분해서 해석하도록 유도

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2F3yWxzUZMCMg0U24zovYB%2Fimage.png?alt=media\&token=9554dc69-35da-4177-95b6-3ffaddc93504)

### 2.5. 모델 출력값

* Extraction 모델은 문서내 존재하는 연속된 단어 token이 정답이므로 token(span)의 시작과 끝 위치를 예측하도록 학습한다. 즉, Token Classification문제로 치환한다.

## 3. Fine-tuning

### 3.1. Fine-tuning BERT

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2FlU5FDb9wqPLL38LeWoPo%2Fimage.png?alt=media\&token=9b6e83f3-53c6-45d1-8754-bde7ef1565cd)

## 4. Post-processing

### 4.1. 불가능한 답 제거하기

* End position이 start position 보다 앞에 있는 경우
* 예측한 위치가 context를 벗어난 경우 (e.g. question 위치 쪽에 답이 나온 경우)
* 미리 설정한 `max_answer_length` 보다 길이가 더 긴 경우

### 4.2. 최적의 답안 찾기

* Start/End position prediction 에서 score(logits)가 가장 높은 N개를 각각 찾는다.
* 불가능한 start/end 조합을 제거한다
* 가능한 조합들을 score의 합이 큰 순서대로 정렬한다.
* score가 가장 큰 조합을 최종 예측으로 선정한다.
* Top-k가 필요한 경우 차례대로 내보낸다.

## 관련 코드 Link

{% embed url="<https://drive.google.com/file/d/1b2XVRQoaCA6QBvYIszrLMwTbsK2sDhor/view?usp=sharing>" %}
