![article thumbnail image](https://blog.kakaocdn.net/dn/bafnJ6/btrJCpPHrW5/fansf7ttkMK4Q8cIQeF2F1/img.png)
Attention Model Intuition
[The problem of long sequences]
아주 긴 프랑스어 문장이 주어졌다고 해보자
초록 : 전체 문장을 읽고 외워서 활성화값에 store 해라
보라 : 영어 문장을 생성해라
-> 인간이라면 이렇게 하지 않을 것. 문장을 지나며 part by part으로 번역함. 아주 긴 문장을 통쨰로 외우는 것은 아주 어려우므로.
그래서 위 같이 encoder-decoder 로 생긴 모델은 문장이 짧을 때 Bleu Score가 높지만, 문장이 길어질수록 스코어가 떨어짐
*문장이 너무 짧을 때도 해석하기 어려우므로
[Attention model intuition]
"When you're trying to generate this output, what part of the input French sentence should you be looking at?"
- Attention Model : a set of attention weights 를 계산
*$s^{<0>}$ : 영어 문장을 생성하는 또다른 RNN을 두는데, 이것의 activation을 $a$ 대신 hidden state $s$로 표기하기로 함
- $\alpha^{<1, 1>}$ : 첫번째 output 단어를 생성할 때 첫번째 input 단어에 얼마나 attention 을 가져야 할지
- $\alpha^{<1, 2>}$ : 첫번째 output 단어를 생성할 때 두번째 input 단어에 얼마나 attention 을 가져야 할지
- $\alpha^{<1, 3>}$ : 첫번째 output 단어를 생성할 때 세번째 input 단어에 얼마나 attention 을 가져야 할지
...
--> 이 attention weight 가 context 가 되어 Jane 을 출력하게 한다
새로운 hidden state $s^{<1>}$ 가 있고, 새로운 set of attention weights 를 가질 거임
- $\alpha^{<2 1>}$ : 두번째 output 단어를 생성할 때 첫번째 input 단어에 얼마나 attention 을 가져야 할지
- $\alpha^{<2, 2>}$ : 두번째 output 단어를 생성할 때 두번째 input 단어에 얼마나 attention 을 가져야 할지
- $\alpha^{<2, 3>}$ : 두번째 output 단어를 생성할 때 세번째 input 단어에 얼마나 attention 을 가져야 할지
-> context가 된다
+ 여기에, 직전 output 'Jane'도 입력으로 들어감
이런 식으로 반복
$\alpha^{<3, t>}$ : 세번째 output 단어를 생성할 때 $t$번째 intput 단어에 얼마나 attention을 가져야 할지
- 이때 해당하는 time step(예시에 따르면 세번째 step) 은 프랑스어 문장에서 $t$ time step에 attention 가져야 한다
- input 문장에서 특정한 단어에 얼마나 attention 가질지는,
- $t$번째 time step에서의 bidirectional RNN 의 activation값 - forward activation $\overrightarrow{a}^{<t>}$ & backward activation $\overleftarrow{a}^{<t>}$
- 그리고 이전 step의 state값 $s^{<2>}$에 달려 있다
step마다 attention weights가 있으며, $t$번째 단어를 생성할 때 얼마나 attention 가져야 할지 --> $\alpha^{<t, t>}$
--> time step 마다 프랑스어 문장에서 local window 구간 내에서만 attention을 가질 수 있게 될 수도 있음 (문장 정보 전부가 아니라)
Attention Model
- 여기서 $\overleftarrow{a}^{<6>}$은 영벡터
- $a^{<t'>} = (\overleftarrow{a}^{<t'>}, \overleftarrow{a}^{<t'>})$ forward, backward activation값 총칭해서 부르기로 함: $t$번째 time step의 feature 벡터
- $t'$ : input 문장에서 단어를 인덱스 하는 데 쓰기로 함
그 다음 번역 문장을 생성하는 (foward) uni-directional RNN 가 있다 - state $s^{<t>}$
- 첫번째 time step 은 $y^{<1>}$를 출력한다
- 입력은 context $c$ 인데,
- $c$는 attention parameter $\alpha^{<1, 1>}, \alpha^{<1, 2>}, ...$에 달렸음
- attention parameter $\alpha$ : "these alpha parameters tells us how much the context would depend on features/activations we're getting from the different time steps"
- context를 define 하는 방법? weighted sum of the features from the different time steps - weighted by these attention weights($\alpha$)
$$\sum_{t'} \alpha^{<1, t'>} = 1$$
$$c^{<1>} = \sum_{t'} \alpha^{<1, t'>}a^{<t'>}$$
--> attention weights 곱하기 activations (위에서 forward,backward 총칭하기로 한)
- $\alpha^{<t, t'>}$ = amount of "attention" that $y^{<t>}$ should pay to $a^{<t'>}$
비슷한 방식으로 반복
$$c^{<2>} = \sum_{t'} \alpha^{<2, t'>}a^{<t'>}$$</t'>
그러면 attention weights 는 어떻게 계산되는가?
[Computing attention $\alpha^{<t,t'>}$]
$\alpha^{<t, t'>}$ = amount of "attention" that $y^{<t>}$ should pay to $a^{<t'>}$
$$\alpha^{<t, t'>} = \frac{\exp{(e^{<t, t'>})}}{\sum_{t'=1}^{T_{x}}\exp{(e^{<t, t'>})}}$$
--> $t'$으로 sum 했을 때 $1$이 되도록 softmax
- 이 식의 $e$는 어떻게 계산할까? 하나의 레이어로 이루어진 작은 신경망을 만들어볼 수 있다
- $s^{<t-1>}$ : $s^{<t>}$에 들어가는 이전 step의 hidden state
- $a^{<t'>}$ : features from time step $t'$
왜 이 두 값으로???
-> $a^{<t'>}$에 얼마나 attention 가질지 결정하기 위해서는 우선은 이전 time step의 hidden state activation $<s^{t-1}>$을 알아야 함 - 아직 현재 time step의 (current state) activation $<s^{t}>$은 계산되지 않은 상태
-> input RNN 에서 각 time step 들은 자신의 feature값들을 살펴봄
-> 당연히(?) $e^{<t, t'>}$ 및 $\alpha{<t, t'>}$ 는 $s^{<t-1>}$와 $a^{<t'>}$에 달려있게 됨
-> 그 function은 gradient descent 통해서 구함
단점? 이 알고리즘을 수행하려면 quadratic 하게 시간와 비용이 든다
만약 $T_{x}$ 길이의 input과 $T_{y}$의 output이 있다면 $\alpha^{<t, t'>}$ 파라미터 갯수는 $T_{x} \times T_{x}$
'인공지능 > DLS' 카테고리의 다른 글
[5.4.] Transformers (0) | 2022.08.14 |
---|---|
[5.3.] Speech Recognition - Audio Data (0) | 2022.08.13 |
[5.3.] Various Sequence To Sequence Architectures(1) (0) | 2022.08.11 |
[5.2.] Applications Using Word Embeddings (0) | 2022.08.11 |
[5.2.] Learning Word Embeddings: Word2vec & GloVe (0) | 2022.08.09 |