NLP - Language Model

5 minute read

언어모델Permalink

  • 다음 문장 다음에 이어질 단어는?
    • Please turn your homework
      • in or the ?
  • 다음 두 문장 중 나타날 확률이 더 높은 것은?
    • all of a sudden I notice three guys standing on the sidewalk.
    • on guys all I of notice sidewalk three a sudden standing the.
  • 목표: 문장이 일어날 확률을 구하는 것
  • 왜 필요한가?
    • 기계번역 (machine translation)
      • P(high winds tonight) > P(large winds tonight)
    • 맞춤법 검사 (spell correction)
      • The office is about fifteen minuets from my house
        • P(about fifteen minutes from) > P(about fifteen minuets from)
    • 음성인식 (speech recognition)
      • P(I saw a van) » P(eyes awe of an)
  • 언어모델: 연속적인 단어들 (sequence of words)에 확률을 부여하는 모델 P(W)=p(w1,w2,ldots,wn)
  • 관련된 일: 연속적인 단어들이 주어졌을 떄 그 다음 단어의 확률을 구하는 것 P(wn|w1,w2,ldots,wn1)

P(W) 구하기Permalink

  • 결합확률 구하기 P(its, water, is, so, transparent, that)
  • chain rule 을 사용해보자

Chain rulePermalink

  • 조건부 확률
    • P(B|A)=P(A,B)P(A)
    • P(A,B)=P(A)P(B|A)
  • 두 개 이상의 확률변수들의 경우
    • P(A,B,C,D)=P(A)P(B|A)P(C|A,B)P(D|A,B,C)
    • 풀이
      • P(A,B,C,D)=P(A,B,C) P(D|A,B,C)=P(A,B) P(C|A,B) P(D|A,B,C)=
  • 일반적인 경우
    • P(x1,x2,,xn)=P(x1) P(x2|x1) P(x3|x1,x2) P(xn|x1,,xn1)
  • P(w1,,wb)=iP(wi|w1w2,wi1)
    • P("its water is so transparent")=P(its)×P(water|its)×P(is|its water)×P(so|its water is)×P(transparent|its water is so)
  • 조건부 확률 P(w|h)
    • P(the|its water is so transparent that)=Count(its water is so transparent that the)Count(its water is so transparent that)
    • 문제는?
      • 가능한 문장의 개수가 너무 많음
      • 이것을 계산할 수 있는 충분한 양의 데이터를 가지지 못할 것임.
  • Markov Assumption
    • “한 단어의 확률은 그 단어 앞에 나타나는 몇 개의 단어들에만 의존한다”라는 가정
      • P(the|its water is so transparent that)P(the|that)
      • P(the|its water is so transparent that)P(the|transparent that)
    • P(wi|w1,wi1)iP(wi|wik,wi1)
      • 바로 직전 k개의 단어에만 의존하는 조건부확률로 근사하겠다.
    • P(w1,,wn)iP(wi|wik,wi1)
      • 단순화 시키면 곱의 형태로 나타나게 됨.
  • Unigram 모델
    • P(w1,,wn)iP(wi)
    • 이 모델로 생성된 문장 예제들
      • fifth, an, of, futures, the, an incorporated, a, a, the, inflation, most, dollars, quarter, in, is, mass
  • Bigram 모델
    • P(wi|w1,wi1)P(wi|wi1)
    • 바로 직전의 단어에 대해서 조건부확률
  • N-gram 모델
    • 이것을 trigrams, 4-grams, 5-grams로 확장할 수 있음
    • 멀리 떨어진 단어들간의 관계를 완벽하게 모델링하진 못한다.
    • 하지만 많은 경우 n-gram 만으로도 좋은 결과를 얻을 수 있음

Bigram 확률 계산Permalink

Maximum likelihood estimationPermalink

P(wi|wi1)=Count(wi1,wi)Count(wi1)
P(wi|wi1)=c(wi1,wi)c(wi1)

왜 이것이 MLE 인가Permalink

문장이 여러개가 주어진 Data: D
parameter: θ
parameter 가 주어졌을 때 data가 나타날 확률:

P(D|θ)

MLE 유도Permalink

P(D|θ)

우선, θ에 대해 정의를 해야함.

두 개의 단어, a와 b가 주어졌을 때.

P(a|b)=αab: 단어 b가 주어졌을 때 그 다음에 나타나는 단어 a의 확률

P(a)=βa: 단어 a의 확률

D={W1,,WN}: D = 문장 들의 집합. Wi = 문장

문장일 경우 대문자, 단어일 경우 소문자로 사용할 것임.

P(D|θ)=i=1NP(Wi)

여기서 θ{αab,βa}를 다 포함한 것

두 문장을 예로 들면,

W1="b a b"
W2="a b a c"

P(D|θ)=P(W1)P(W2)=P("b a b")P("a b a c")=βbαabαba βaαbaαabαca

P("b a b")=βbαabαba
P("a b a c")=βaαbaαabαca

로그를 적용해보면,
lnP(D|θ)=2lnαab+2lnαba+lnαca+βa+βb

확률로 만드는 제약조건,

aVαab=1

b 는 고정이고 a 값만을 Vocabulary에 속해있는 모든 단어들에 대해서 더하는 것.

aVP(a|b)=aVP(a,b)P(b)=1P(b)aVP(a,b) # 결합확률(P(a, b))을 a에 관해서 합하게 되면 b에 관한 Marginal Probability가 되는 것=1P(b)P(b)=1

따라서 이러한 제약조건이 Parameter(θ)에 적용되어야 함.

이 조건 하에서 P(D|θ)=i=1NP(Wi)을 최대화 시키는 방법을 찾아야 함.

라그랑주 방법을 사용하면 쉽게 해결됨.

αab=C(b,a)lnαab+λ(aVαab1)

  • 이 식이 최대화되는 αab 를 구하면 됨.
  • C(b,a): “b a” 가 나오는 빈도
αablnab=C(b,a)αab+λ=0

0으로 놓고 풀면

λαab=C(b,a)

이렇게 되고,

모든 a 에 관해서 양변에 추가를 하면

aVλαab=aVC(b,a) λaVαab=aVC(b,a)

여기서 aVαab=1 이므로, 좌변은 lambda만 남는다.

λ=aVC(b,a)

이 값을 다시 위의 식(αablnab=C(b,a)αab+λ=0)에 대입

αab=C(b,a)aVC(b,a)

이 식은 앞에서 MLE 처음에 언급한 P(wi|wi1)=c(wi1,wi)c(wi1)과 동일한 것을 알 수 있음.

P(a)=βa을 문장시작 기호에 대해서도 정의하면,
<s>: 문장 시작 기호 라고 했을때,  αa<s> 로 사용할 수 있다.

예제Permalink

P(wi|wi1)=c(wi1,wi)c(wi1)

<s> I am Sam</s>
<s> Sam I am </s>
<s> I do not like green eggs and ham </s>

P(I|<s>)=23=0.67P(Sam|<s>)=13=0.33P(am|I)=23=0.67P(<s>|Sam)=12=0.5P(Sam|am)=12=0.5P(do|I)=13=0.33

  • Bigram 빈도수 bigram-freq.png
  • Bigram 확률 bigram-probs.png
P(<s> i want english food </s>)=P(i|<s>)P(want|i)P(english|want)P(food|english)P(</s>|food)=0.25×0.33×0.0011×00.5×00.68=0.000031

모델평가Permalink

  • 외재적 평가 (extrinsic evaluation)
    • 언어모델은 일반적으로 그 자체가 목표이기보다 특정 과제르 ㄹ위한 부분으로서 쓰여지게 됨
    • 따라서 언어모델이 좋은지 판단하기 위해선 그 과제의 평가지표를 사용하는 경우가 많음
    • 예를 들어, 맞춤법 검사를 위해서 두 개의 언어모델 A, B를 사용한다고 할 때
      • 각 모델을 사용해서 얼마나 정확하게 맞춤법 오류를 수정할 수 있는 지 계산
      • 정확도가 높은 언어모델을 최종적으로 사용
  • 내재적 평가 (intrinsic evaluation)
    • 외재적 평가는 시간이 많이 걸리는 단점
    • 언어모델이 학습하는 확률자체를 평가 못함: perplexity(3.2.1 Perplexity in SLP book)
    • 이 기준으로 최적의 언어모델이 최종 과제를 위해서는 최적이 아닐 수도 있음
    • 하지만 언어모델의 학습과정에 버그가 있었는지 빨리 확인하는 용도로 사용 가능.

PerplexityPermalink

  • 좋은 언어모델이란?
    • 테스트 데이터를 높은 확률로 예측하는 모델
    • perplexity: 확률의 역수를 단어의 개수로 정규화한 값
    • perplexity를 최소화하는 것이 확률을 최대화 하는 것
  • PP(W)=P(w1w2wN)1N=1P(w1wN)N
  • use chain rule
    • PP(W)=i=1N1P(wi|w1wi1)N
  • bigram perplexity
    • PP(W)=i=1N1P(wi|wi1)N

AppendixPermalink

ReferencePermalink

Speech and Language Processing : https://web.stanford.edu/~jurafsky/slp3/ed3book_dec302020.pdf

Leave a comment