attention mechanism - beyondnlp/nlp GitHub Wiki
์ดํ ์ (attention)์ด๋
vanilla rnn์์ Vanishing Gradient Problem๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋์จ lstm์์๋ ๋ฌธ์์ด์ด ๊ธธ์ด์ง๋ฉด ํจ๊ณผ์ ์ผ๋ก ์ ๋ณด๋ฅผ ์์ถํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฌ lstm์ ์ฌ์ฉํ seq2seq๋ชจ๋ธ์์๋ ๋ฒ์ญ์ ํ์ง์ด ๋จ์ด์ง๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ๋ชจ๋ ์ ๋ณด๋ฅผ ๋ค ์ฌ์ฉํ ๋ ค๋๋ฐ ๋ฌธ์ ๊ฐ ์๋ค๊ณ ๋ ๋ณผ์ ์๋ค. ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํด๊ฒฐํ๊ธฐ ์ํด ๋์จ ์๊ณ ๋ฆฌ์ฆ์ด attention mechanism์ด๋ค.( attention๋ง๊ณ ๋ ์์์ ์ธ๊ธํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ bidirectional lstm์ผ๋ก๋ ์ด๋ ์ ๋ ํด๊ฒฐ์ด ๋๋ค. )
์ด๋ฅผ ์ข ๋ ์ดํด๊ฐ ์ฝ๊ฒ ์ค๋ช
ํ๋ฉด
๋๋ ๋งฅ์ฃผ๊ฐ ์ข๋ค๋ ์๋ฏธ์ ๋
์ผ์ด ๋ฌธ์ฅ(Ich mochte ein bier)๊ณผ ์์ด ๋ฌธ์ฅ(Iโd like a beer)์ seq2seq๋ฅผ ํตํด ๋ฒ์ญํ๋ค๊ณ ํ ๋
์ฌ๋์ ์ง๊ด์ ์ผ๋ก beer์ bier์๋ง ์ํฅ์ ๋ฐ๋ ๋ค๋ ๊ฒ์ ์ ์ ์๊ณ ์ด๋ฅผ ์๊ณ ๋ฆฌ์ฆํ ํ ๊ฒ์ด๋ค.
beer๋ฅผ ์์ธกํ ๋ bier์ด์ธ์ ๊ฒ์ ๋ณ๋ค๋ฅธ ์ ๊ฒฝ์ ์ธ ํ์๊ฐ ์๊ณ ์คํ๋ ค ์ฑ๋ฅ์ ์ ํ์ํค๋ ์์ธ์ด ๋๊ธฐ ๋๋ฌธ์ bier์ ์ง์คํ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
๊ทธ๋ผ ์ด๋ค ์ ๋ณด์ ๊ธฐ๋ฐํ์ฌ beer๋ฅผ ์์ธกํ ๋๋ bier์ ์ํฅ๋ฐ๋ ๋ค๋ ๊ฒ์ ์ ์ ์์๊น?
"encoder์์ bier๋ฅผ ์
๋ ฅ์ผ๋ก ํด์ ๋ง๋ ์ถ๋ ฅ ๋ฒกํฐ์ decoder์์ beer๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ๋ ๋ฒกํฐ๊ฐ ์๋ก ์ ์ฌํ ๊ฒ์ด๋ค"๋ผ๋ ๊ฐ์ ์์ ์ถ๋ฐํ๋ค
( ์ด ๋ถ๋ถ์ ๋ณด๋ค ๋ณด๋ SMT์์ word alignment์ ์ ์ฌํ ๊ฐ๋
์ด ์๋๊ฐ ํ๋ ์๊ฐ์ด ๋ค์๋ค )
how to implement attention
๊ทธ๋ฌ๋ฉด ์ด๋ป๊ฒ ์ด๋ฅผ ๊ตฌํํ ๊ฒ์ธ๊ฐ
- H(t)๋ ๋์ฝ๋์ ๋ฒกํฐ์ด๊ณ H(s)๋ ์ธ์ฝ๋์ ๋ฒกํฐ์ด๋ค ์ ๊ณต์์ฒ๋ผ
- Attention Weights๋ H(t)์ ๋ชจ๋ H(s)๋ฅผ ๋ด์ ํ์ฌ ๋์จ ๋ฒกํฐ๋ฅผ softmax๋ฅผ ์ทจํด ํ๋ฅ ์ ๊ตฌํ๋ค.
- C(t)=Context(t)๋ ๋ชจ๋ H(s)์ Attention Weights๋ฅผ ๊ณฑํด์ ๋ํ๋ค. ( ์ค์นผ๋ผ ๊ฐ์ด ๋์ฌ ๋ฏ )
- attention(H(t))์ Context(t)์ input(H(t))์ concatenationํ๊ณ tanh๋ฅผ ํต๊ณผํ ๊ฐ์ด๋ค. ( ๋ฏธ๋ถ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ํ์ต์ด ๊ฐ๋ฅํ ๋ฏ )
์ฌ๊ธฐ์ stanford nlp์ ์๋ฃ๋ฅผ ์ฐธ๊ณ ํ๋ฉด
keras code๋ฅผ ๋ณด๋ฉด ์๋์ ๊ฐ์ด ๊ตฌํ๋ผ ์๋ค.
inputs = Input(shape=(input_dim,))
#ATTENTION PART STARTS HERE
attention_probs = Dense(input_dim, activation='softmax', name='attention_vec')(inputs)
attention_mul = merge([inputs, attention_probs], output_shape=32, name='attention_mul', mode='mul')
#ATTENTION PART FINISHES HERE
attention_mul = Dense(64)(attention_mul)
output = Dense(1, activation='sigmoid')(attention_mul)
model = Model(input=[inputs], output=output)
input_dim * input_dim ์ matrix๊ฐ ์์ฑ์ด ๋๊ณ activation func์ softmax๋ฅผ ์ทจํ๊ณ ์ด maxtrix๋ฅผ attention_probs๋ก ๋ช ์นญํ๋ค. ์ด์ด์ inputs์ attention_probs๋ฅผ merge(ํ๋ ฌ๊ณฑ์ )ํ์ฌ attention_mul์ ์์ฑํ๊ณ ์ด๋ฅผ ๋ค์ Fully Connected Layer๋ก 64์ฐจ์ output์ ๋ง๋ค๊ณ ์ฌ๊ธฐ์ sigmoid๋ฅผ ๊ฑฐ์ณ 1์ฐจ์ output์ ๋ง๋ ๋ค.
๋ง์ง๋ง์ผ๋ก "A Brief Overview of Attention Mechanism" ์ ๋ฏธ๋์ ๋ธ๋ก๊ทธ ๊ธ ๋ง์ง๋ง์ ์๋์ ๊ฐ์ ๋ฌธ์ฅ์ด ์๋ค.
There are many variants in the cutting-edge researches, and they basically differ in the choice of score function and attention function, or of soft attention and hard attention (whether differentiable). But basic concepts are all the same.
์ต์ฒจ๋จ์ ์ฐ๊ตฌ ๋ถ์ผ์๋ ๋ง์ ๋ณํ๋ค์ด ์์ต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก scoreํจ์์ attention ํจ์์ ์ ํ์ด ๋ค๋ฅด๊ณ ๋๋ soft attention๊ณผ hard attention( ๋ฏธ๋ถ์ด ๊ฐ๋ฅํ์ง )์ด ๋ค๋ฆ ๋๋ค. ๊ทธ๋ฐ๋ฐ ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋ ์ ๋ชจ๋ ๋์ผํฉ๋๋ค.
- score( Hi, ^Hi )๋ fully connected network๋ก ํ์ต
- stanford nlp attention http://nlp.stanford.edu/pubs/emnlp15_attn.pdf
- A Brief Overview of Attention Mechanism https://medium.com/@Synced/a-brief-overview-of-attention-mechanism-13c578ba9129
- ์ดํ ์ ๋งค์ปค๋์ฆ https://ratsgo.github.io/from%20frequency%20to%20semantics/2017/10/06/attention/
- keras attention https://github.com/philipperemy/keras-attention-mechanism/blob/master/attention_dense.py
- backend ๊ฐ๋ฐ์์ neural machine translation https://www.slideshare.net/deview/224-backend-neural-machine-translation-67608580
- Attention Models (D2L12 Insight@DCU Machine Learning Workshop 2017) https://www.slideshare.net/xavigiro/attention-models-d2l12-insightdcu-machine-learning-workshop-2017
- Attention mechanisms with tensorflow https://www.slideshare.net/KeonKim/attention-mechanisms-with-tensorflow