Language model 이란 text generate 를 위한 modeling 이다.
Language model 자체를 input, output, task 으로 나눌 수 있는데, 이 경우 output 은 항상 text 일 것이다.

conditional language model 의 경우, 원래 context 를 제외한 added context가 존재한다.
원 context 를 사용하는 language model 의 경우, 다음 단어를 예측, 학습하는데 사용된다.
conditional language model 의 경우, 위 그림처럼 task 를 수행하는 방법으로 학습된다.


sequence to sequence 의 경우, input sequence 를 output sequence 로 변환하는데 사용되는 Neural Network 이다. 구조의 경우, 크게 Encoder-Decoder 구조로 이루어져 있다.
(conditional language model)

그림에서 argmax 의 경우, decoder 가 output sequence를 생성할 때 각 단계에서 다음 토큰을 선택하게 하는 연산자이다. 
Encoder
Decoder
정리해보자면, Seq2seq 모델의 경우
Attention 이란, 입력값인 Query 와 비슷한 값을 가진 , 비슷한 내용을 가진 Key 를 찾아 이에 해당하는 Value 를 찾는 과정이다

Bottleneck problem
Encoder state 에서 Context vector 의 경우, 입력 벡터 를 받아들여 고정된 길이 벡터 를 반환하는데, source sentence 에 모든 정보를 담아야한다. 이때, Information 의 bottleneck problem 이 발생한다.
Linear interaction distance
Lack of parallelizability
Decoder 의 각 step 에서 encoder 와 direct connection 을 통해, Encoder 의 일부 내용을 직접적으로 참조할 수 있게 만들어준다.
Query : Time step t의 hidden state
Key : Encoder Hidden state (embedded)
Value : Encoder Hidden state (embedded) (same as Key)

(기본구조)
다음 Q,K,V 에 대해서 각 step 별 attention score 인
이후 softmax 연산을 통해 attention distribution
encoder hidden state 가 value 이므로, 이를 attention distribution 과의 weighted sum 을 통해 attention output 을 얻는다.
이유경 멘토님 자료
https://www.phontron.com/class/anlp2022/index.html
https://web.stanford.edu/class/cs224n/
https://kh-kim.gitbook.io/natural-language-processing-with-pytorch