25. Language Modeling and Recurrent Neural Networks
Language Modeling
Classic -gram ๋ชจ๋ธ
Language Modeling

- Next word๊ฐ ๋ฌด์์ธ์ง ์์ธกํ๋ Task
- ๊ณต์์ ์ ์
- Words sequence ๊ฐ ์ฃผ์ด์ก์ ๋, Next word ์ Probability distribution ๊ณ์ฐ
- ์ด๋ฅผ ์ํํ๋ System์ Language Model์ด๋ผ ์ง์นญ
- Text ์์ฒด์ ํ๋ฅ ์ ํ ๋นํ๋ ์์คํ ์ผ๋ก ๊ฐ์ฃผ ๊ฐ๋ฅ
- Language Model์ ๊ฐ์ง๊ณ ํ ์ ์๋ ๊ฒ?
- Score sentences: ๋ฌธ์ฅ์ ์์ฐ์ค๋ฌ์ ํ๊ฐ
- Jane went to the store. โ high
- Store to Jane went the. โ low
- Generate sentences(๋ฌธ์ฅ ์์ฑ)
- Score sentences: ๋ฌธ์ฅ์ ์์ฐ์ค๋ฌ์ ํ๊ฐ
while didn't choose end-of-sentence symbol
Calculate probability
Sample a new word from the probability distribution
You Use Language Models Every Day!
- ์๋ ์์ฑ
- ๊ฒ์ ์์ง
- ChatGPT
-Gram Language Models
- Deep learning ์ด์ ์ ๊ทผ๋ณธ์ ์ด๊ณ ๊ณ ์ ์ ์ธ Language Model ๊ตฌํ ๋ฐฉ์
- -gram: ๊ฐ์ ์ฐ์์ ์ธ words ๋ฉ์ด๋ฆฌ
- Unigrams: "the", "students"
- Bigrams: "the students", "student opened"
- Trigrams: "the students opened", "students opened their"
- Four-grams: "the students opened their"
- Idea: ๋น๋ ํต๊ณ๋ฅผ ์์งํ์ฌ next word ์์ธก์ ํ์ฉ

- Markov assumption
- ์ ์ค์ง ์์ ๊ฐ์ words์๋ง ์์กดํ๋ค๊ณ ๊ฐ์
- Large corpus(๋๊ท๋ชจ ์ธ์ด ์งํฉ)์์ ๊ฐ์๋ฅผ ์ธ์ด(Counting) ํ๋ฅ ๊ณ์ฐ(Statistical approximation)
- Count ratio๋ฅผ ํตํด ์กฐ๊ฑด๋ถ ํ๋ฅ ๊ทผ์ฌ
-Gram Language Models: Example
- 4-gram Language Model ํ์ต์ ๊ฒฝ์ฐ๋ฅผ ๊ฐ์
as the proctor started the clock, thestudents opened their ______
- Corpus ๋ด "students opened their" ๋ค์ ์ค๋ ๋จ์ด๋ค์ ๋น๋๋ฅผ ํ์ธํ์ฌ ํ๋ฅ ๋ถ์ฌ
- "books":
- "exams":
Generating Text with a -Gram Language Model
- ๊ฐ๋จํ trigram ์ธ์ด ๋ชจ๋ธ
- 170๋ง ์ด์์ ๋จ์ด ๋ชจ์ (Reuters: ๊ฒฝ์ , ๊ฒฝ์ ๋ด์ค)
- ์ธ์ด ๋ชจ๋ธ๋ก ํ ์คํธ๋ฅผ ์์ฑํ ์ ์์.

today the price of gold per ton, while production of shoe lasts and shoe industry, the bank intervened just after it considered and rejected an imf demand to rebuild depleted european stocks, sept 30 end primary 76 cts a share.
"์ค๋ ํค๋น ๊ธ๊ฐ์, ๊ตฌ๋ ๊ณจ ์ ์๊ณผ ์ ๋ฐ ์ฐ์ ์์ฐ์ด ์ง์๋๋ ๋์, ์ํ์ ๊ณ ๊ฐ๋ ์ ๋ฝ ์ฃผ์(์ฌ๊ณ )์ ์ฌ๊ฑดํ๋ ค๋ IMF ์๊ตฌ๋ฅผ ๊ฒํ ํ๊ณ ๊ฑฐ๋ถํ ์งํ ๊ฐ์ ํ๊ณ , 9์ 30์ผ ๋ง๊ฐ ์ฃผ๊ฐ๋ 76์ผํธ์ ๋๋ค."
- ๋๋ผ์ธ ์ ๋๋ก ๋ฌธ๋ฒ์ ์ด๋ค!
- ...ํ์ง๋ง ์ผ๊ด์ฑ์ด ์์ (๋งฅ๋ฝ์ด ๋ง์ง ์์).
- ์ธ์ด๋ฅผ ์ ๋ชจ๋ธ๋งํ๋ ค๋ฉด ํ ๋ฒ์ ์ธ ๋จ์ด ์ด์์ ๊ณ ๋ คํด์ผ ํจ.
- ํ์ง๋ง ์ ์ฆ๊ฐ์ํค๋ฉด ํฌ์์ฑ ๋ฌธ์ (sparsity problem)๋ฅผ ์ ํ์ํค๊ณ , ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ์ํค๊ฒ ๋จ.
Neural Language Models
Based on feed-forward NNs and RNNs
A (Fixed-Window) Neural Language Model
- Output distribution
- Hidden layer
- Concatenated word embeddings
- Words / One-hot vectors

- Yoshua Bengio et al. (2000)์ด ์ ์ํ ์ด๊ธฐ ๋ฒ์
- -gram Language Model ๋๋น ๊ฐ์ ์
- Sparsity problem(ํฌ์์ฑ ๋ฌธ์ ) ์์
- ๊ด์ธก๋ ๋ชจ๋ -grams๋ฅผ ์ ์ฅํ ํ์ ์์
- ๋จ์ ๋ฌธ์ ์
- Fixed window ํฌ๊ธฐ๊ฐ ๋๋ฌด ์์
- Window ํฌ๊ธฐ๋ฅผ ๋๋ฆฌ๋ฉด weights ๊ฐ ์ปค์ง
- ๊ณผ ๊ฐ ์๋ก ์์ ํ ๋ค๋ฅธ weights ์ ๊ณฑํด์ง (๋น๋์นญ์ฑ)
- ์์์ ๊ธธ์ด์ input์ ์ฒ๋ฆฌํ ์ ์๋ neural architecture ํ์์ฑ ๋๋
Recurrent Neural Networks (RNN)

- Core idea: ๋์ผํ weights ๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ ์ฉ
A Simple RNN Language Model

- Recurrent ๊ตฌ์กฐ๋ฅผ ํ์ฉํ language Model ๋์
RNN Language Models
- RNN์ ์ฅ์
- ๋ชจ๋ ๊ธธ์ด์ input ์ฒ๋ฆฌ ๊ฐ๋ฅ
- ์ด๋ก ์ ์ผ๋ก step ์ ์ฐ์ฐ์ ์์ฃผ ์ค๋์ step์ ์ ๋ณด ํ์ฉ ๊ฐ๋ฅ
- Input context๊ฐ ๊ธธ์ด์ ธ๋ model size๊ฐ ์ฆ๊ฐํ์ง ์์
- ๋ชจ๋ timestep์ ๋์ผํ weights๋ฅผ ์ ์ฉํ๋ฏ๋ก input ์ฒ๋ฆฌ ๋ฐฉ์์ ๋์นญ์ฑ ์กด์ฌ
- RNN์ ๋จ์
- Recurrent computation ์๋๊ฐ ๋๋ฆผ
- ์ค์ ๋ก๋ ๋จผ ๊ณผ๊ฑฐ์ ์ ๋ณด์ ์ ๊ทผํ๊ธฐ ์ด๋ ค์ (Vanishing gradient ๋ฑ)
Training an RNN Language Model

- ์ ์ฐจ
- Words sequence๋ก ๊ตฌ์ฑ๋ big corpus ์ค๋น
- RNN-LM์ ์ ๋ ฅํ์ฌ ๋งค step ๋ง๋ค output distribution ๊ณ์ฐ
- Loss function
- ์์ธก๋ ํ๋ฅ ๋ถํฌ ์ ์ค์ ๋ค์ ๋จ์ด (One-hot) ๊ฐ์ Cross-entropy
- ์ ์ฒด training set์ ๋ํด Average loss ๊ณ์ฐ
- ์ ์ฒด corpus์ ๋ํด ํ ๋ฒ์ loss์ gradients๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ ๋น์ฉ์ด ๊ณผ๋คํจ
- ์ค์ ๋ก๋ ๋ฅผ sentence (๋๋ document) ๋จ์๋ก ์ฒ๋ฆฌ
- Stochastic Gradient Descent (SGD)๋ฅผ ํ์ฉํ์ฌ ์์ chunk (Batch) ๋ฐ์ดํฐ์ ๋ํด loss ๋ฐ gradient ๊ณ์ฐ ํ weights update ๋ฐ๋ณต
Generating Text with an RNN Language Model

- ํน์ text style๋ก ํ์ต๋ RNN-LM์ ํตํด ํด๋น ์คํ์ผ์ text ์์ฑ ๊ฐ๋ฅ
- ์: Obama speeches, Harry Potter ์์ค ์คํ์ผ ๋ฑ
Recurrent Neural Networks for Other Applications
Tagging, classification, question answering, speech recognition
RNNs Can Be Used for Tagging

- ์: Part-of-speech tagging, Named Entity Recognition ๋ฑ
RNNs Can Be Used for Sentence Classification

- ์: Sentiment classification(๊ฐ์ ๋ถ์) ๋ฑ
RNN-LMs Can Be Used to Generate Text

- Speech recognition, Machine translation, Summarization ๋ฑ
Variants of RNNs
RNN์ ๋ณํ
Bidirectional and Multi-Layer RNNs: Motivation

- ํด๋น ์๋ ์ํ๋ฅผ ๋ฌธ์ฅ ๋ด์ "terribly"๋ผ๋ ๋จ์ด์ ๋ฌธ๋งฅ์ ํํ์ผ๋ก ๊ฐ์ฃผ ๊ฐ๋ฅ
- ์ด๊ฒ์ contextual representation(๋ฌธ๋งฅ์ ํํ)์ด๋ผ๊ณ ๋ถ๋ฆ.
- ์ด๋ฌํ ๋ฌธ๋งฅ์ ํํ์ ์ผ์ชฝ ๋ฌธ๋งฅ(์: "the movie was")์ ๋ํ ์ ๋ณด๋ง์ ํฌํจ
- ์ด ์์์์, "exciting"์ ์ค๋ฅธ์ชฝ ๋ฌธ๋งฅ์ ์์ผ๋ฉฐ ์ด๊ฒ์ด "terribly"์ ์๋ฏธ๋ฅผ ์์ ํ๊ณ ์์ (๋ถ์ ์์ ๊ธ์ ์ผ๋ก).
Bidirectional RNNs

- Forward ๋ฐ backward ์ ๋ณด๋ฅผ ๊ฒฐํฉํ์ฌ ํ์ฉ
Multi-Layer RNNs

- RNN์ ์ฌ๋ฌ ์ธต์ผ๋ก ์์ ๊ตฌ์ฑ
Long Short-Term Memory RNNs (LSTMs)
- 1997๋ Hochreiter์ Schmidhuber๊ฐ vanishing gradients problem์ ํด๊ฒฐ์ฑ ์ผ๋ก ์ ์ํ RNN์ ์ผ์ข
- Inputs sequence ๊ฐ ์ฃผ์ด์ง๋ฉด hidden states sequence ์ cell states ๋ฅผ ๊ณ์ฐ
- Timestep ์์์ ๊ณผ์

- Forget gate: ์ด์ cell state์์ ์ ์งํ ๊ฒ๊ณผ ์์ ๊ฒ์ ์ ์ด
- Input gate: ์๋ก์ด cell content ์ค ์ด๋ค ๋ถ๋ถ์ cell์ ๊ธฐ๋กํ ์ง ์ ์ด
- Output gate: cell์ ์ด๋ค ๋ถ๋ถ์ hidden state๋ก ์ถ๋ ฅํ ์ง ์ ์ด
(Sigmoid function: ๋ชจ๋ gate ๊ฐ์ 0๊ณผ 1 ์ฌ์ด)
- New cell content: cell์ ๊ธฐ๋ก๋ ์๋ก์ด ๋ด์ฉ
- Cell state: ์ด์ cell state์์ ์ผ๋ถ ๋ด์ฉ์ ์ง์ฐ๊ณ ("forget"), ์๋ก์ด cell content๋ฅผ ์ผ๋ถ ๊ธฐ๋ก("input")
- Hidden state: cell์์ ์ผ๋ถ ๋ด์ฉ์ ์ฝ์("output")
- ์ฐธ๊ณ ์ฌํญ
- ์ด๋ค์ ๋ชจ๋ ๋์ผํ ๊ธธ์ด ์ vector
- Gates๋ element-wise (๋๋ Hadamard) product ๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ฉ


Diagram Legend
(X): Pointwise Multiplication (Element-wise)(+): Pointwise Addition (The "Secret" regarding gradients)SIGMOID,TANH: Neural Network Layers (Activation Functions)->: Vector TransferCombine: Concatenation

