Scaled Dot-Product Attention: کوئری، کلید و مقدار
Attention Is All You Need
- Prediction prompt: given one query vector and three key vectors, which key will get the largest weight, and what happens to the output if two keys are equally similar?
- Inputs: queries and keys of dimension d_k, values of dimension d_v (Section 3.2.1, Figure 2 left)
- Step by step: dot products of the query with all keys, divide each by sqrt(d_k), apply softmax to get weights, take the weighted sum of values
- Matrix form, Equation (1): Attention(Q, K, V) = softmax(Q K^T / sqrt(d_k)) V
- Shapes: Q is (n_q x d_k), K is (n_k x d_k), V is (n_k x d_v), Q K^T is (n_q x n_k), output is (n_q x d_v)
- Softmax is applied row-wise: each query's weights over all keys sum to 1
- Worked hand example with 2 to 3 tokens and d_k = 4, computing every intermediate number
- Comparison with additive attention: similar theoretical complexity, but dot-product attention is much faster and more space-efficient because it uses optimized matrix multiplication
وزن از سازگاری query و key
\(q=(1,0)\)، \(k_1=(2,0)\)، \(k_2=(2,0)\)، \(k_3=(0,1)\)
**ضرب داخلی**
\[ q\cdot k_i=\sum_{j=1}^{2}q_jk_{ij} \]
**امتیازهای سازگاری**
\[ \begin{aligned}q\cdot k_1&=1\cdot2+0\cdot0=2\\q\cdot k_2&=1\cdot2+0\cdot0=2\\q\cdot k_3&=1\cdot0+0\cdot1=0\end{aligned} \]
**مقایسهٔ وزنها**
\[ w_1=w_2>w_3>0 \]
**جمع وزندار valueها**
\[ v_1=10,\quad v_2=20,\quad v_3=30,\qquad o=10w_1+20w_2+30w_3 \]
وزن برابر برای دو value اول؛ سهم مثبت برای سومی
شکل ماتریسهای query، key و value
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><rect class="tm-soft" x="70" y="95" width="270" height="90" rx="12"/><text class="tm-text-accent" x="205" y="128" font-size="22" text-anchor="middle">\(Q: n_q \times d_k\)</text><text class="tm-text" x="205" y="160" font-size="18" text-anchor="middle" direction="rtl">هر سطر: یک query</text><rect class="tm-soft" x="460" y="95" width="270" height="90" rx="12"/><text class="tm-text-accent" x="595" y="128" font-size="22" text-anchor="middle">\(K: n_k \times d_k\)</text><text class="tm-text" x="595" y="160" font-size="18" text-anchor="middle" direction="rtl">هر سطر: یک key</text><text class="tm-muted" x="400" y="220" font-size="18" text-anchor="middle" direction="rtl">طول مشترک: \(d_k\)</text><rect class="tm-green" x="460" y="260" width="270" height="90" rx="12"/><text class="tm-text-green" x="595" y="293" font-size="22" text-anchor="middle">\(V: n_k \times d_v\)</text><text class="tm-text" x="595" y="325" font-size="18" text-anchor="middle" direction="rtl">هر سطر: value متناظر</text><line class="tm-line-accent" x1="595" y1="185" x2="595" y2="260"/><text class="tm-text2" x="420" y="365" font-size="17" text-anchor="middle" direction="rtl">\(d_v\) مستقل از \(d_k\)</text><text class="tm-text" x="400" y="415" font-size="20" text-anchor="middle" direction="rtl">مثال: \(Q: 2\times4\quad K: 2\times4\quad V: 2\times2\)</text><rect class="tm-accent" x="70" y="260" width="270" height="90" rx="12"/><text class="tm-text" x="205" y="294" font-size="20" text-anchor="middle" direction="rtl">خروجی: \(n_q\) سطر</text><text class="tm-text" x="205" y="325" font-size="17" text-anchor="middle" direction="rtl">یک سطر برای هر query</text><line class="tm-line-accent" x1="205" y1="185" x2="205" y2="260"/></svg>
```
محاسبهٔ امتیازهای query و key
ماتریسهای ورودی \(Q\) و \(K\)
**سطرهای ورودی**
\[ Q=\begin{bmatrix}1&0&0&0\\0&1&0&0\end{bmatrix},\qquad K=\begin{bmatrix}2&0&0&0\\0&2&0&0\end{bmatrix} \]
**ترانهادهٔ keyها**
\[ K^{T}=\begin{bmatrix}2&0\\0&2\\0&0\\0&0\end{bmatrix},\qquad (2\times4)^{T}=4\times2 \]
**امتیازهای query اول**
\[ (QK^{T})_{11}=1\cdot2+0\cdot0+0\cdot0+0\cdot0=2,\qquad (QK^{T})_{12}=0 \]
**امتیازهای query دوم**
\[ (QK^{T})_{21}=0,\qquad (QK^{T})_{22}=2 \]
**ماتریس امتیاز و شکل آن**
\[ QK^{T}=\begin{bmatrix}2&0\\0&2\end{bmatrix},\qquad (2\times4)(4\times2)\longrightarrow 2\times2 \]
**شکل کلی امتیازها**
\[ QK^{T}\in\mathbb{R}^{n_q\times n_k} \]
از امتیاز تا وزن softmax
امتیازها: \(QK^{T}=\begin{bmatrix}2&0\\0&2\end{bmatrix}\)، با شکل \(2\times2\)
**مقیاس**
\[ d_k=4,\qquad \sqrt{d_k}=2 \]
**امتیازهای scale شده**
\[ S=\frac{QK^{T}}{\sqrt{d_k}}=\begin{bmatrix}1&0\\0&1\end{bmatrix}\in\mathbb{R}^{2\times2} \]
**softmax سطری**
\[ w_{ij}=\frac{e^{S_{ij}}}{\sum_{\ell=1}^{2}e^{S_{i\ell}}} \]
**توانهای سطر اول**
\[ (e^1,e^0)\approx(2.718,1) \]
**وزنهای سطر اول**
\[ \frac{(e^1,e^0)}{e^1+e^0}\approx\frac{(2.718,1)}{3.718}\approx(0.731,0.269) \]
**وزنهای سطر دوم**
\[ \frac{(e^0,e^1)}{e^0+e^1}\approx(0.269,0.731) \]
**ماتریس وزنها**
\[ W\approx\begin{bmatrix}0.731&0.269\\0.269&0.731\end{bmatrix}\in\mathbb{R}^{2\times2},\qquad \sum_{j=1}^{2}w_{ij}=1 \]
از وزنها تا خروجی Attention
ماتریس وزنهای سطری: \(A\approx\begin{bmatrix}0.731&0.269\\0.269&0.731\end{bmatrix}\)
**valueها**
\[ V=\begin{bmatrix}1&0\\0&1\end{bmatrix} \]
**خروجی query اول**
\[ 0.731\begin{bmatrix}1&0\end{bmatrix}+0.269\begin{bmatrix}0&1\end{bmatrix}\approx\begin{bmatrix}0.731&0.269\end{bmatrix} \]
**خروجی query دوم**
\[ 0.269\begin{bmatrix}1&0\end{bmatrix}+0.731\begin{bmatrix}0&1\end{bmatrix}\approx\begin{bmatrix}0.269&0.731\end{bmatrix} \]
**خروجی و شکل آن**
\[ AV\approx\begin{bmatrix}0.731&0.269\\0.269&0.731\end{bmatrix}\in\mathbb{R}^{2\times2},\qquad AV\in\mathbb{R}^{n_q\times d_v} \]
وزنها در \(V\) ضرب میشوند، نه در \(K\).
**فرمول کامل Attention**
\[ \operatorname{Attention}(Q,K,V)=\operatorname{softmax}\!\left(\frac{QK^{\mathsf T}}{\sqrt{d_k}}\right)V \]
softmax سطری است.
**مقایسه با additive attention**
\[ \text{Attention} \]
پیچیدگی نظری مشابه؛ در عمل، ضرب ماتریسی بهینه سریعتر و کمحافظهتر است.
بازگشت به دوره