-
[yongggg's] NLP Metric for Language modelMetric 2023. 6. 2. 14:02
안녕하세요! 이번 장에서는 만들어진 Language Model을 평가하는 Metric에 대해 설명하는 시간을 갖도록 하겠습니다.
물론 언어 전문가가 모든 결과물들을 평가하면 좋겠지만, 요즘 시대와 같이 큰 데이터를 모두 사람이 평가하기엔 무리가 있기 때문에 좋은 Metric을 찾아 모델을 평가하는 것이 중요합니다. 설명 시작하겠습니다.
Language Model (LM)
먼저 LM의 문장 예측 방법은 다음과 같다.
LM은 임의의 token들의 sequence 자체에 확률을 부여하며, token 들의 sequence가 존재할 가능성이 높은 것들을 문장으로 생성한다. 대부분의 언어 모델은 이전 token(context)가 주어졌을 때, 각 token들의 확률의 곱으로 이 확률을 추정할 수 있다.
$$ P(w_{1}, w_{2}, ..., w_{n})=p(w_{1})p(w_{2}|w_{1})p(w_{3}|w_{2},w_{1})...p(w_{n}|w_{1},w_{2},w_{3},...,w_{n-1})=\prod_{i=1}^{n}p(w_{i}|w_{1},...w_{i-1})$$
예를 들어, 다음의 문장을 예측할 확률을 구한다고 하자, 이 때 확률 $P(S)$는 다음과 같이 구할 수 있다.
$$ S \; = \; 우리는 \; 어디에 \; 가나요 \\ P(S)=P(우리는) \times P(어디에|우리는) \times P(가나요|우리는, 어디에)$$
ROUGE(Recall-Oriented Understudy for Gisting Evaluation)
ROUGE-N
BLEU는 기계 번역의 성능이 얼마나 뛰어난가를 측정하기 위해 사용되는 대표적인 방법으로 측정 기준은 n-gram과 precision에 기반한다.
여기서 n-gram이란 'n개의 연속적인 단어 나열'을 의미한다.
예를들어 "An adorable little boy is spreading smiles" 이라는 문장에서 1~4 grams은 다음과 같다.
- 1-gram(unigrams) : an, adorable, little, boy, is, spreading, smiles
- 2-gram(bigrams) : an adorable, adorable little, little boy, boy is, is spreading, spreading smiles
- 3-gram(trigrams) : an adorable little, adorable little boy, little boy is, boy is spreading, is spreading smiles
- 4-gram : an adorable little boy, adorable little boy is, little boy is spreadig, boy is spreading smiles
이렇게 단어들을 쪼갠 뒤, 정답 문장의 n-grams 리스트의 개수를 분모로,
정답 문장의 n-grams 리스트와 예측 문장의 n-grams 리스트의 교집합 개수를 분자로 하여 Rouge-N score를 구할 수 있다.
ROUGE-L
가장 긴 공통 시퀀스 (LCS)를 기반으로 한다. 두 시퀀스 사이의 LCS란 최대 길이를 가지는 공통 하위 Sequence를 말한다. 따라서 예측 및 실제 요약문에 LCS가 있다는 것은 예측 요약 문과 실제 요약문이 일치하는 것이라고 볼 수 있다.
- 재현율 $R_{LCS} = {LCS(예측, 실제) \over 실제 \; 요약문의 \; 전체 \; 단어수}$
- 정밀도 $P_{LCS} = {LCS(예측, 실제) \over 예측 \; 요약문의 \; 전체 \; 단어수)}$
- F-measure $ F_{LCS} = {(1+b^{2})R_{LCS}P_{LCS} \over R_{LCS}+b^{2} P_{LCS}}$
F-measure에서 $b는 정밀도와 재현율의 가중치를 조절하는 데 사용되며, 이 F-measure가 ROUGE_L이다.
BLEU Score(Bilingual Evaluation Understudy Score)
위의 ROUGE score는 Reference Setence의 단어가 Generated Sentence에 포함되는 정도를 나타냈지만,
BLEU score는 Generated Sentence의 단어가 Reference Sentence에 포함되는 정도를 나타낸다.
Unigram Precision
- Candidate1 : It is a guide to action which ensures that the military always obeys the commands of the party.
- Candidate2 : It is to insure the troops forever hearing the activity guidebook that party direct.
- Reference1 : It is a guide to action that ensures that the military will forever heed Party commands.
- Reference2 : It is the guiding principle which guarantees the military forces always being under the command of the Party.
- Reference3 : It is the practical guide for the army always to heed the directions of the party.
$$ Unigram Precision = {the \; number \; of \; Candi \; words(unigrams) \; which \; in \; any \; Refere \over the \; total \; number \; of \; words \; in \; the \; Ca}$$
위 수식은 다음과 같은 예제에서, 말이 안되는 문장에 대해 score가 높아지는 모순적인 현상이 나오기 때문에 이를 방지하기 위해 Modified Uigram Precision을 사용한다.
- Candidate : the the the the the the the
- Reference1 : the cat is on the mat
- Reference2 : there is a cat on the mat
$$ Count_{clip} = \min(Count, Max \; Refer \;Count) \\ Modified \; Unigram \; Precision = {Candi의 \; 각 \; 유니그램에 \; 대해 \; Count_{clip}을 \; 수행한 \; 값의 \; 총 \; 값 \over Ca의 \; 총 \; 유니그램 \; 수}$$