چرا مقیاسدهی با 1/sqrt(d_k)؟
Attention Is All You Need
- Question first: if you double the length of two random vectors, what happens to the spread of their dot product, and what does that do to softmax?
- The empirical observation cited in Section 3.2.1: for small d_k additive and dot-product attention perform similarly, but additive outperforms unscaled dot-product attention for larger d_k
- The paper's hypothesis: large dot products push softmax into regions with extremely small gradients
- Footnote 4 argument: if components of q and k are independent with mean 0 and variance 1, then q · k has mean 0 and variance d_k
- Dividing by sqrt(d_k) brings the variance back to 1
- Numerical demonstration: softmax of [1, 2, 3] versus softmax of [8, 16, 24] and how the second becomes nearly one-hot
- Connection to the scaling factor being the only difference between the paper's attention and standard dot-product attention
چرا امتیازها را scaling میکنیم؟
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><rect class="tm-soft" x="125" y="105" width="550" height="190" rx="18"/><text class="tm-text" x="400" y="155" text-anchor="middle" font-size="25" direction="rtl">دو برابر شدن بُعد بردارها</text><text class="tm-text-accent" x="400" y="220" text-anchor="middle" font-size="24" direction="rtl">پراکندگی dot product ؟</text><text class="tm-muted" x="400" y="270" text-anchor="middle" font-size="19" direction="rtl">اثر بر وزنهای softmax ؟</text><text class="tm-text" x="400" y="345" text-anchor="middle" font-size="21" direction="rtl">query و key ← امتیاز ← softmax ← وزنها</text><text class="tm-muted" x="400" y="380" text-anchor="middle" font-size="18" direction="rtl">جمع وزنها: \(1\)</text><text class="tm-text-gold" x="400" y="70" text-anchor="middle" font-size="20" direction="rtl">امتیاز بزرگ ← gradient کوچک</text><text class="tm-text-accent" x="400" y="420" text-anchor="middle" font-size="20" direction="rtl">پیش از softmax: تقسیم امتیاز بر \(\sqrt{d_k}\)</text></svg>
```
میانگین و واریانس dot product
مؤلفههای query و key مستقلاند؛ \(\mathbb{E}[q_i]=\mathbb{E}[k_i]=0\) و \(\operatorname{Var}(q_i)=\operatorname{Var}(k_i)=1\).
**تعریف dot product**
\[ S=\mathbf q\cdot\mathbf k=\sum_{i=1}^{d_k}q_i k_i \]
**میانگین هر جمله**
\[ \mathbb E[q_i k_i]=\mathbb E[q_i]\mathbb E[k_i]=0 \]
**مربع هر مؤلفه**
\[ \mathbb E[q_i^2]=\mathbb E[k_i^2]=1 \]
**واریانس هر جمله**
\[ \operatorname{Var}(q_i k_i)=\mathbb E[q_i^2]\mathbb E[k_i^2]-\bigl(\mathbb E[q_i k_i]\bigr)^2=1 \]
**میانگین و واریانس جمع**
\[ \mathbb E[S]=0,\qquad \operatorname{Var}(S)=\sum_{i=1}^{d_k}\operatorname{Var}(q_i k_i)=d_k \]
**مثال بُعد چهار**
\[ d_k=4\quad\Longrightarrow\quad \operatorname{Var}(S)=4,\qquad \sigma_S=\sqrt{4}=2 \]
**دو برابر شدن بُعد**
\[ d_k\to 2d_k\quad\Longrightarrow\quad \operatorname{Var}(S)\to 2d_k,\qquad \sigma_S\to\sqrt{2}\,\sigma_S \]
نتیجه به فرض استقلال وابسته است.
اشباع softmax و کاهش gradient
مقایسهٔ softmax پیش و پس از بزرگ شدن امتیازها
**امتیازهای اولیه**
\[ \mathbf{z}=(1,2,3),\qquad \operatorname{softmax}(\mathbf{z})_i=\frac{e^{z_i}}{\sum_j e^{z_j}} \]
**مقدارهای نمایی و جمع**
\[ (e^1,e^2,e^3)\approx(2.72,7.39,20.09),\qquad \sum_j e^{z_j}\approx 30.2 \]
**وزنهای اولیه**
\[ \operatorname{softmax}(1,2,3)\approx(0.09,0.24,0.67) \]
**بزرگ کردن امتیازها**
\[ 8\mathbf{z}=(8,16,24),\qquad (8,16,24)-24=(-16,-8,0) \]
**مقدارهای نمایی پس از انتقال**
\[ (e^{-16},e^{-8},e^0)\approx(0.00000011,0.000335,1) \]
**وزنهای جدید**
\[ \operatorname{softmax}(8,16,24)\approx(0.00000011,0.000335,0.999665) \]
تقریباً one-hot
**حساسیت هر وزن**
\[ \frac{\partial p_i}{\partial z_i}=p_i(1-p_i)\approx 0\quad (p_i\approx 0\ \text{or}\ p_i\approx 1) \]
gradient کوچک در ناحیهٔ اشباع
چرا تقسیم بر ریشه بُعد؟
امتیاز \(S=q\cdot k\) با واریانس \(d_k\)
**قاعده واریانس**
\[ \operatorname{Var}\!\left(\frac{S}{c}\right)=\frac{\operatorname{Var}(S)}{c^2} \]
**مثال با بُعد چهار**
\[ d_k=4,\qquad \operatorname{Var}(S)=4,\qquad \sqrt{d_k}=2 \]
**تقسیم بر ریشه بُعد**
\[ \operatorname{Var}\!\left(\frac{S}{2}\right)=\frac{4}{2^2}=1 \]
**تقسیم بر خود بُعد**
\[ \operatorname{Var}\!\left(\frac{S}{4}\right)=\frac{4}{4^2}=\frac14 \]
**مقایسه کلی**
\[ \operatorname{Var}\!\left(\frac{S}{\sqrt{d_k}}\right)=\frac{d_k}{d_k}=1,\qquad \operatorname{Var}\!\left(\frac{S}{d_k}\right)=\frac{d_k}{d_k^2}=\frac{1}{d_k} \]
**ترتیب ثابت، فاصله کمتر**
\[ 2<6,\qquad \frac{2}{2}=1<3=\frac{6}{2} \]
برنده ثابت میماند؛ تمرکز وزنها کمتر میشود.
بازگشت به دوره