data:analysis:attention

This is an old revision of the document!


Attention

https://www.perplexity.ai/
Attention의 계산 방법은 다음과 같습니다:
1. Query, Key, Value 벡터 생성:

  1. Query (Q): 현재 디코더의 hidden state
  2. Key (K): 인코더의 hidden states
  3. Value (V): 인코더의 hidden states

2. Attention Score 계산:

  1. Query와 각 Key 간의 유사도를 계산합니다.
  2. 일반적으로 내적(dot product)을 사용: Score = Q · K^T

3. Softmax 적용:

  1. Attention Score에 softmax를 적용하여 가중치를 정규화합니다.
  2. Weight = softmax(Score)

4. 가중합 계산:

  1. 정규화된 가중치와 Value를 곱하여 가중합을 구합니다.
  2. Output = Σ(Weight * V)

5. Multi-Head Attention (선택적):

  1. 여러 개의 attention head를 병렬로 계산하고 결과를 연결합니다.

이 과정을 통해 모델은 입력 시퀀스의 각 부분에 대해 서로 다른 중요도를 부여하며, 문맥을 더 잘 이해할 수 있게 됩니다. Self-attention의 경우 Q, K, V가 모두 동일한 입력에서 생성되며, cross-attention에서는 Q가 다른 소스(예: 디코더)에서 생성됩니다.
Citations:
[1] https://skyil.tistory.com/256
[2] https://zzziito.tistory.com/56
[3] https://pongdangstory.tistory.com/389
[4] https://wikidocs.net/22893
[5] https://velog.io/%40sjinu/%EA%B0%9C%EB%85%90%EC%A0%95%EB%A6%AC-Attention-Mechanism

Q. attention 문장과 벡터 예시로 계산법 보여줘
Attention의 계산 방법은 다음과 같습니다:

1. Query, Key, Value 벡터 생성:

  1. Query (Q): 현재 디코더의 hidden state
  2. Key (K): 인코더의 hidden states
  3. Value (V): 인코더의 hidden states

2. Attention Score 계산:

  1. Query와 각 Key 간의 유사도를 계산합니다.
  2. 일반적으로 내적(dot product)을 사용: Score = Q · K^T

3. Softmax 적용:

  1. Attention Score에 softmax를 적용하여 가중치를 정규화합니다.
  2. Weight = softmax(Score)

4. 가중합 계산:

  1. 정규화된 가중치와 Value를 곱하여 가중합을 구합니다.
  2. Output = Σ(Weight * V)

5. Multi-Head Attention (선택적):

  1. 여러 개의 attention head를 병렬로 계산하고 결과를 연결합니다.

이 과정을 통해 모델은 입력 시퀀스의 각 부분에 대해 서로 다른 중요도를 부여하며, 문맥을 더 잘 이해할 수 있게 됩니다. Self-attention의 경우 Q, K, V가 모두 동일한 입력에서 생성되며, cross-attention에서는 Q가 다른 소스(예: 디코더)에서 생성됩니다.
Citations:
[1] https://skyil.tistory.com/256
[2] https://zzziito.tistory.com/56
[3] https://pongdangstory.tistory.com/389
[4] https://wikidocs.net/22893
[5] https://velog.io/%40sjinu/%EA%B0%9C%EB%85%90%EC%A0%95%EB%A6%AC-Attention-Mechanism

Enter your comment:
Z U B C T
 
  • Last modified: 2025/07/07 14:12