-
8-1_(attention_ RNN을 이용한 sequence to sequence의 문제)_ NMT_No_Sampling.ipynbpytorch를 이용한 자연어입문 2024. 6. 21. 15:02
출처 : 파이토치를 이용한 자연어 처리 https://product.kyobobook.co.kr/detail/S000001810395
Q1. attention 왜 나왔는지
sequence to sequence(encoder에서 vector통해 decoder나옴)의 Bottle neck문제(문장의 길이가 긴데 다 담지 못하는 문제)
RNN 장기의존성문제, gradient vanishing문제를 해결해보려 나온게 attention
Q2. Qurery, key, value
query 는 decoder에서 나오는 벡터
key, value 는 encoder에서 나온 벡터 (모든시점의 h)
-------------------------------------------------------------------------
Key-Value 자료형에 대한 이해를 가지고 어텐션 함수에 대해서 설명해보겠습니다.
어텐션을 함수로 표현하면 주로 다음과 같이 표현됩니다.
Attention(Q, K, V) = Attention Value어텐션 함수는 주어진 '쿼리(Query)'에 대해서 모든 '키(Key)'와의 유사도를 각각 구합니다. 그리고 구해낸 이 유사도를 키와 맵핑되어있는 각각의 '값(Value)'에 반영해줍니다. 그리고 유사도가 반영된 '값(Value)'을 모두 더해서 리턴합니다. 여기서는 이를 어텐션 값(Attention Value)이라고 하겠습니다.
지금부터 배우게 되는 seq2seq + 어텐션 모델에서 Q, K, V에 해당되는 각각의 Query, Keys, Values는 각각 다음과 같습니다.
Q = Query : t 시점의 디코더 셀에서의 은닉 상태 K = Keys : 모든 시점의 인코더 셀의 은닉 상태들 V = Values : 모든 시점의 인코더 셀의 은닉 상태들
============================================================
RNN을 이용한 sequence to sequence의 문제
Summary
연속된 data = sequence
encoder to decoder에서
가운데에 고정된 vector(예, 128차원)를 통해 decoder로 나가서
vector는 고정된 사이즈라 문장의 길이에 영향을 받지 않는 문제가 있다.
고정길이 vector
-Bottle neck problem (문장은 엄청긴데 적은데 담을수 있냐는 문제)
-Gradient Vanishing 문제 (Rnn이라서)
-Long Term Dependency
아래그림
나는 --> I
n개의 vocab size중 1개가 crossentropy를 통해 loss값을 줄여나가면서 학습된다.
W x [at : st]concat = output
아래그림) W x [at : st]concat = output 해서 각 decoder의 timestep에서 나온 단어
아래그림)
teacher forcing
학습중 decoder에 정답을 입력으로 제공
'pytorch를 이용한 자연어입문' 카테고리의 다른 글
01. PyTorch_NLP_Basics_1.ipynb (0) 2024.09.18 8-2 (0) 2024.06.28 8-0 (0) 2024.06.14 7_3_(batch)Model2_Conditioned_Surname_Generation (0) 2024.06.07 7_3_Model1_Unconditioned_Surname_Generation.ipynb (0) 2024.05.31