Summary

$$
\text{input : }X\in\R^{N\times{d_{model}}}\\
\\\,\\
Q = XW_Q\\
K = XW_K\\
V = XW_V\\\,\\
Attention(Q, K, V) = softmax\left(\frac{QK^T}{\sqrt{d_k}}\right)V\\\text{concat and Mul }\mathbf W_0
$$
Input
$$
X\in\R^{N\times{d_{model}}}
$$
- $X$라고 표기하겠다. 토큰의 수 X Embedding Dim의 크기를 갖는다.
- ViT같은 경우에는 $N\times (P^2\cdot C)$ 가 될 것이다.
- $N$은 patch의 수
- $P^2\cdot C$ 는 하나의 patch가 갖는 픽셀의 수
Q, K, V 구하기
$$
Q = XW_Q\\
K = XW_K\\
V = XW_V\\
$$
Input $X$에 $d_{model} \times (d_{model}/h)$ 크기를 갖는 $W_Q,W_K,W_V$를 곱해서 얻는다.
- $d_{model}$은 Embedding Dimensino이고 ViT에서는 $P^2\cdot C$일 것이다.
- $h$는 헤더의 수다
- $Q, K, V$각각의 크기는 $N\times {(d_{model}/h)}$ 가 될 것이다.
Softmax 계산
$$
\mathrm{SoftMax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
$$
- $\sqrt{d_k}$는 $K$의 차원의 수이다. $\sqrt{d_{model}/h}$ 값을 갖는다 된다.
- $QK^T$를 Attention Energy라고 한다.
- softmax의 출력 크기는 $N\times N$이 될 것이다.
- V에 곱한다