[Boostcamp] Day 19. 곧 주말이야

Jaeyeon Kim·2023년 3월 30일
1

Naver Boostcamp AI Tech

목록 보기
19/29
post-thumbnail

오늘 배워간 것

LSTM

LSTM의 input은 ctc_{t}(cell state vector)와 hth_{t} 이고, 식은 아래와 같다.

ct,ht=LSTM(xt,ct1,ht1)c_{t}, h_{t} = LSTM(x_{t}, c_{t-1}, h_{t-1})

LSTM의 핵심은 cell state vector이다.
hidden state vectorcell state vector를 한 번 더 가공하여 필요한 정보만 뽑은 벡터이다.

Forget gate
ft=σ(Wf[ht1,xt]+bf)f_{t} = \sigma(W_{f} \cdot [h_{t-1}, x_{t}] + b_{f})
이전 time 스텝에서 넘어온 정보 중 일정 부분만 유지하도록 한다.

Input gate
it=σ(Wi[ht1,xt]+bi)i_{t} = \sigma(W_{i} \cdot [h_{t-1}, x_{t}] + b_{i})

Gate gate
Ct~=tanh(Wc[ht1,xt]+bc)\widetilde{C_{t}} = tanh(W_{c} \cdot [h_{t-1}, x_{t}] + b_{c})

Output gate
ot=σ(Wo[ht1,xt]+bo)o_{t} = \sigma(W_{o} \cdot [h_{t-1}, x_{t}] + b_{o})

New Cell State
Ct=ftCt1+itCt~C_{t} = f_{t} \cdot C_{t-1} + i_{t} \cdot \widetilde{C_{t}}

New hidden state
ht=ottanh(Ct)h_{t} = o_{t} \cdot tanh(C_{t})

GRU

Cell state와 Hidden state의 일원화

GRU의 Hidden state는 LSTM의 Cell state와 유사하다.
zt=σ(Wz[ht1,xt])z_{t} = \sigma(W_{z} \cdot [h_{t-1}, x_{t}])
rt=σ(Wr[ht1,xt])r_{t} = \sigma(W_{r} \cdot [h_{t-1}, x_{t}])
ht~=tanh(W[rtht1,xt])\widetilde{h_{t}} = tanh(W \cdot [r_{t} \cdot h_{t-1}, x_{t}])
ht=(1zt)ht1+ztht~h_{t} = (1 - z_{t}) \cdot h_{t-1} + z_{t} \cdot \widetilde{h_{t}} : 가중치의 합이 항상 1로 나옴

역전파를 진행할 때 이전 cell state에 forget gate를 곱하여 업데이트하고, 필요한 정보를 덧셈을 통해서 만들기 때문에 Gradient vanishing/explode 문제를 해결한다.

Seq2Seq

Encoder와 Decoder로 이루어진다.
Decoder의 입력은 <SoS> 토큰으로 시작하고 <EoS> 토큰으로 끝난다.

Attention

디코더에서 어떤 단어에 집중해서 가져올 지 정함

결과가 별로면 다시 attention 수정 수행

보통 다음 단어를 예측하는 과정을 통해 생성을 진행한다.
하지만 그런 경우 한 단어라도 잘못 예측하면 원래 생성하려고 했던 문장과 다른 결과가 나온다.

각 단어가 등장할 확률에 대해서 모두 곱하고 로그를 씌워서 최대 확률을 가지는 경우를 찾아낸다.

k가 5일 때의 beam search의 예시이며, k는 보통 5에서 10으로 둔다고 한다.


<출처: https://opennmt.net/OpenNMT/translation/beam_search/>

score의 식은 다음과 같다.

score(y1,...,yt)=i=1tlogPLM(yiy1,...,yi1,x)score(y_{1},...,y_{t}) = \sum_{i=1}^{t}logP_{LM}(y_{i}|y_{1},...,y_{i-1}, x)

그렇기 때문에 길이가 긴 문장일수록 점수가 낮아지게 된다.
이를 보완해주기 위해서 길이에 따라서 정규화 해준다.

score(y1,...,yt)=1ti=1tlogPLM(yiy1,...,yi1,x)score(y_{1},...,y_{t}) = \frac{1}{t}\sum_{i=1}^{t}logP_{LM}(y_{i}|y_{1},...,y_{i-1}, x)

BLEU

precision=#(correctwords)length_of_predictionprecision = \frac{\#(correct\,words)}{length\_of\_prediction}
recall=#(correctwords)length_of_referencerecall = \frac{\#(correct\,words)}{length\_of\_reference}
Fmeasure=precisionrecall12(precision+recall)F-measure = \frac{precision*recall}{\frac{1}{2}(precision+recall)}

위와 같은 지표는 순서가 달라져도 성능이 높게 나오므로 새로운 지표가 필요하다.
그렇게 등장한 BLEU(BiLingual Evaluation Understudy)
재현율보다는 정확도를 더 고려한 점수.

BLEU=min(1,length_of_predictionlength_of_reference)(i=14precisioni)14BLEU = min(1, \frac{length\_of\_prediction}{length\_of\_reference})(\prod_{i=1}^{4}precision_i)^{\frac{1}{4}}

전항은 Brevity penalty로 길이값을 비교해서 가중치를 준다.
길이가 짧은 문장들이 높은 점수가 나오는 것을 방지하기 위함
위 식이 시사하는 바는, 1-gram 부터 4-gram precision을 모두 계산해서 연속된 단어들이 잘 나오는지 확인하는 것이다.


서두르지 말고,
한 발짝씩 나아가기

profile
낭만과 열정으로 뭉친 개발자 🔥

0개의 댓글