*사용된 모든 영문 image의 출처는 cs231n 강의 자료입니다.*
<Character-level Language Model>
1. Character-level Language Model
Character-level Language Model
RNN의 기본 구조

- h(t) = f[h(t-1), x(t)]
e.g. lstm(h(t-1), x(t))
RNN의 Hidden State 계산

Q. Why use tanh activation function in RNN?

→ RNN은 모든 timestep에서 동일한 paramter set 연산 수행하기 때문에,
(tanh activation의 y값 범위: -1 ~ 1)
if 0 근처 값의 입력이 들어왔을때:
Sigmoid, ReLU → 동일 연산 수행 시, 양수값으로 계속 shifting → Gradient Exploding 발생
tanh → 동일한 연산이 진행되어도 0 근처의 입력값을 계속 유지
∴ RNN은 tanh을 activation function으로 사용한다.
Character-level
- a, b, c, ····, z + 특수문자 + <SOS>, <BOS> 등의 token # <SOS>, <BOS> = Sentence의 시작을 알리는 token
e.g. [<SOS>] I study math.

→ 공백, 특수문자, token 등을 모두 하나의 character로 취급
Character-level Language Model
- Example of training sequence 'hello'
- Input: 'hello'
- Vocabulary: [h, e, l, o] # Unique set

h₂ = tanh(Whh·h₁ + Wxh·x₂)


- 이전 state인 h(t-1) vector와 현재 input인 x(t) vector를 concat # (7 x 1)
- 가중치 W를 concat된 matrix에 곱 # (3 x 7)
- Matrix W는 h(t-1)에 할당하는 W(h(t-1) → h(t))과 x(t)에 해당하는 W(x(t) → h(t))로 나뉘어 행렬곱되는 것과 같다고 볼 수 있다.
∴ h(t)는 Whh·h(t-1) + Wxh·x(t)에 tanh 활성화 함수를 적용시킨 형태가 된다.

y₁´ = softmax(Why·h₁)

- 현재 hidden state vector인 h₁과 가중치 Why에 곱하여 y₁´ 출력
- 현재 state의 Input이 x₁에 해당하는 character 'h'였으므로 target character는 'e'가 나오도록 Weight를 train

- Train 후 model에 test 시, (input character = 'hell')
→ Predict된 'e'를 다음 time step의 input으로 전달 → 최종 time step에서 'o' 출력
'NLP' 카테고리의 다른 글
| RNNs with Attention (0) | 2022.06.24 |
|---|---|
| Language Model & AWD techniques (0) | 2022.06.22 |
| Word Embedding - Word2Vec, Glove, Doc2Vec (0) | 2022.06.20 |
| Topic Modeling (0) | 2022.06.17 |
| Bag-of-Words (0) | 2022.06.16 |