부스트캠프 week6 day2

Dae Hee Lee·2021년 9월 7일
0

BoostCamp_NLP

목록 보기
3/14

RNN 설명 블로그

RNN

이전 시점의 hidden state와 현재 시점의 입력 데이터로 현재 시점의 출력 데이터를 만들어내는 순환적인 구조의 Neural Network이다. 매 타임 스텝에서 같은 W를 공유하는 것이 특징.

How to calculate the hidden state of RNNs
• We can process a sequence of vectors by applying a recurrence formula at every time
step
ht1h_{t-1}: old hidden-state vector
𝑥t𝑥_t: input vector at some time step
hth_t : new hidden-state vector
𝑓W𝑓_W : RNN function with parameters W
𝑦t𝑦_t: output vector at time step t

WxhW_{xh} : WxtW_{x_t}hth_t로 변환
Wht1W_{h_{t-1}} : WhtW_{h_t}hth_t로 변환
WhyW_{hy} : hth_tyty_t로 변환

Types of RNN

  • One-to-one
    - Standard Neural Network
    • Not Sequence Data
  • One-to-Many
    - Image Captioning
    • 입력은 time step이 아니지만 이미지를 입력으로 받아서 텍스트 데이터 출력
    • 매 타임 스텝에 0행렬을 추가
  • Many-to-one
    - Sentiment Classification
    • sequence data를 받아서 마지막에 나온 결과값을 통해 긍정/부정 등의 분류 진행
  • Many-to-many
    - Machine Translation
    • encoding, decoding의 형태
    • 입력이 끝나고 나면 출력 시퀀스 데이터 시작
  • Many-to-many
    - Video Classification on Frame Level
    - 딜레이가 없는 입력이 들어올 때마다 출력하는 형태

Character-level Language Model

언어 모델이라는 것은 현재 주어진 문자열이나 단어를 보고 다음 단어를 예측하는 모델이다. 따라서 character-level에서 진행할 수 있다.

참고로, 첫 시점의 이전 hidden state인 h0h_0는 처음에 0 벡터로 주어진다.

BPTT

RNN's Problem

Multiplying the same matrix at each time step during backpropagation causes gradient vanishing or exploding

LSTM

Long Short Term Memory

LSTM 설명
Gradient문제를 해결하고 먼 time step의 정보도 기억할 수 있게끔 만들어졌다.

기존의 Hidden state를 단기 기억이라고 표현한다면 이 기억을 길게 해준다는 의미로 Long Short Term Memory라고 부른다.
LSTM은 이전 시점에서 Hidden state와 Cell state두 개의 값을 넘겨받는다.

(ct,ht)=LSTM(xt,Ct1,ht1)(c_t,h_t) = LSTM(x_t, C_{t-1}, h_{t-1})

Cell state가 Hidden state보다 좀 더 완성되어있는 정보를 담고 있는 벡터이다. Hidden state 벡터는 Cell state를 한 번 더 가공한 정보를 담고 있는 벡터이고, 해당 time step에서 output vector로 활용된다.

Forget Gate

ft=σ(Wf[ht1,xt]+bf)f_t = \sigma(W_f\cdot [h_{t-1},x_t] + b_f)

Input Gate

it=σ(Wi[ht1,xt]+bi)i_{t}=\sigma\left(W_{i} \cdot\left[h_{t-1}, x_{t}\right]+b_{i}\right)
C~t=tanh(WC[ht1,xt]+bC)\widetilde{C}_{t}=\tanh \left(W_{C} \cdot\left[h_{t-1}, x_{t}\right]+b_{C}\right)
Ct=ftCt1+itC~tC_{t}=f_{t} \cdot C_{t-1}+i_{t} \cdot \widetilde{C}_{t}

Output Gate

ot=σ(Wo[ht1,xt]+bo)o_t = \sigma(W_o\cdot [h_{t-1},x_t] + b_o)
ht=ottanh(Ct)h_t = o_t\cdot tanh(C_t)
CtC_t가 가진 많은 정보를 filtering하는 느낌으로 생각할 수 있다.
ex) 따옴표가 열린 문장에서 Cell state에는 따옴표가 열려있는 정보, hidden state에는 각 단어의 정보가 포함되었다고 볼 수 있다.

이 때, hth_t는 output layer의 입력 값이다.

GRU

LSTM에서 Cell state와 Hidden state를 일원화한 형태이다.
특히, LSTM이 Forget gate와 Input gate로 나뉘어져 있던 것을 zt,(1zt)z_t, (1-z_t)의 형태로 나누어서 계산하는 것이 특징이다.

zt=σ(Wz[ht1,xt])z_{t}=\sigma\left(W_{z} \cdot\left[h_{t-1}, x_{t}\right]\right)
rt=σ(Wr[ht1,xt])r_{t}=\sigma\left(W_{r} \cdot\left[h_{t-1}, x_{t}\right]\right)
h~t=tanh(W[rtht1,xt])\tilde{h}_{t}=\tanh \left(W \cdot\left[r_{t} \cdot h_{t-1}, x_{t}\right]\right)
ht=(1zt)ht1+zth~th_{t}=\left(1-z_{t}\right) \cdot h_{t-1}+z_{t} \cdot \widetilde{h}_{t}
c.f) Ct=ftCt1+itC~tC_{t}=f_{t} \cdot C_{t-1}+i_{t} \cdot \widetilde{C}_{t} in LSTM

즉, 가중 평균의 형태로 ht1,h~th_{t-1},\tilde{h}_{t}를 더해주는 형식이다.

더 알아볼 것

Teacher Forcing의 장/단점 및 특징을 알아보자.

https://simonjisu.github.io/nlp/2018/07/05/packedsequence.html

Further Reading

RNN/LSTM/GRU 기반의 Language Model에서 초반 time step의 정보를 전달하기 어려운 점을 완화할 수 있는 방법
Bidirectional, Attention

profile
Today is the day

0개의 댓글