Positional Encoding
Attention Is All You Need
- Question first: if you shuffle the words of a sentence, does the output of plain self-attention for each word change? What does that imply?
- Why order must be injected: no recurrence and no convolution means attention alone has no notion of position (Section 3.5)
- Positional encodings have dimension d_model so they can be added to input embeddings at the bottom of encoder and decoder stacks
- Formulas: PE(pos, 2i) = sin(pos / 10000^(2i/d_model)), PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
- Each dimension is a sinusoid; wavelengths form a geometric progression from 2π to 10000 · 2π
- Hand computation: PE values for pos = 0, 1, 2 at i = 0 and a higher i, with d_model = 512
- The relative-position hypothesis: for any fixed offset k, PE(pos+k) is a linear function of PE(pos)
- Learned positional embeddings gave nearly identical results (Table 3 row E); sinusoids chosen because they may extrapolate to longer sequences than seen in training
ترتیب در self-attention ساده
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><text class="tm-text" x="400" y="45" text-anchor="middle" font-size="22" direction="rtl">جابهجایی کلمهها</text><text class="tm-text2" x="120" y="120" text-anchor="middle" font-size="19" direction="rtl">پیش از جابهجایی</text><rect class="tm-accent" x="240" y="85" width="140" height="60" rx="12"/><text class="tm-text" x="310" y="123" text-anchor="middle" font-size="21" direction="rtl">گربه</text><rect class="tm-gold" x="440" y="85" width="140" height="60" rx="12"/><text class="tm-text" x="510" y="123" text-anchor="middle" font-size="21" direction="rtl">توپ</text><text class="tm-text2" x="120" y="240" text-anchor="middle" font-size="19" direction="rtl">پس از جابهجایی</text><rect class="tm-gold" x="240" y="205" width="140" height="60" rx="12"/><text class="tm-text" x="310" y="243" text-anchor="middle" font-size="21" direction="rtl">توپ</text><rect class="tm-accent" x="440" y="205" width="140" height="60" rx="12"/><text class="tm-text" x="510" y="243" text-anchor="middle" font-size="21" direction="rtl">گربه</text><text class="tm-muted" x="400" y="325" text-anchor="middle" font-size="20" direction="rtl">embedding هر کلمه همراه آن میماند</text><text class="tm-text-accent" x="400" y="385" text-anchor="middle" font-size="21" direction="rtl">خروجیها نیز همراه کلمهها جابهجا میشوند</text><text class="tm-text2" x="310" y="170" text-anchor="middle" font-size="16">query · key · value</text><text class="tm-text2" x="510" y="170" text-anchor="middle" font-size="16">query · key · value</text><text class="tm-text2" x="310" y="290" text-anchor="middle" font-size="16">query · key · value</text><text class="tm-text2" x="510" y="290" text-anchor="middle" font-size="16">query · key · value</text><text class="tm-text-green" x="400" y="425" text-anchor="middle" font-size="18" direction="rtl">خروجی گربه ثابت است؛ ترتیب امتیازها تغییر میکند</text></svg>
```
ساخت و افزودن positional encoding
**جمع، نه اتصال**
\(\mathrm{embedding}+\mathrm{PE}\longrightarrow\mathrm{input}\)
هر سه بردار دارای \(512\) مؤلفهاند.
**فرمول هر جفت مؤلفه**
\[\mathrm{PE}(pos,2i)=\sin\left(\frac{pos}{10000^{2i/d_{\mathrm{model}}}}\right)\]
\[\mathrm{PE}(pos,2i+1)=\cos\left(\frac{pos}{10000^{2i/d_{\mathrm{model}}}}\right)\]
زاویه برحسب رادیان است.
**جایگاه و شمارهٔ جفت**
\(pos\): جایگاه token؛ \(i\): شمارهٔ جفت مؤلفهها
برای \(i=0\): \(\mathrm{PE}(pos,0)=\sin(pos)\) و \(\mathrm{PE}(pos,1)=\cos(pos)\)
محاسبهٔ دو مؤلفه در سه جایگاه
\(d_{\mathrm{model}}=512\)، \(i=0\)
**فرمول جفت مؤلفه**
\[ PE(pos,2i)=\sin\!\left(\frac{pos}{10000^{2i/d_{\mathrm{model}}}}\right),\quad PE(pos,2i+1)=\cos\!\left(\frac{pos}{10000^{2i/d_{\mathrm{model}}}}\right) \]
**محاسبهٔ مخرج**
\[ \frac{2i}{d_{\mathrm{model}}}=\frac{0}{512}=0,\quad 10000^0=1 \]
**جفت مؤلفهٔ اول**
\[ PE(pos,0)=\sin(pos),\quad PE(pos,1)=\cos(pos) \]
**جایگاه صفر**
\[ \bigl(PE(0,0),PE(0,1)\bigr)=(\sin 0,\cos 0)=(0,1) \]
**جایگاه یک**
\[ \bigl(PE(1,0),PE(1,1)\bigr)=(\sin 1,\cos 1)\approx(0.8415,0.5403) \]
زاویه بر حسب رادیان
**جایگاه دو**
\[ \bigl(PE(2,0),PE(2,1)\bigr)=(\sin 2,\cos 2)\approx(0.9093,-0.4161) \]
زاویه بر حسب رادیان
**جفت ثابت، جایگاه متغیر**
\[ i=0,\quad pos\in\{0,1,2\} \]
نوسان آهسته در مؤلفههای بالاتر
\(d_{\mathrm{model}}=512,\quad i=128\)
**توان مخرج**
\[ \frac{2i}{d_{\mathrm{model}}}=\frac{2\cdot128}{512}=\frac12 \]
**مخرج و زاویه**
\[ 10000^{1/2}=100,\qquad \theta=\frac{pos}{100} \]
**جایگاه صفر**
\[ \theta=\frac{0}{100}=0,\qquad PE(0,256)=\sin(0)=0,\quad PE(0,257)=\cos(0)=1 \]
**جایگاه یک**
\[ \theta=\frac{1}{100}=0.01,\qquad PE(1,256)=\sin(0.01)\approx0.0100,\quad PE(1,257)=\cos(0.01)\approx0.99995 \]
**جایگاه دو**
\[ \theta=\frac{2}{100}=0.02,\qquad PE(2,256)=\sin(0.02)\approx0.0200,\quad PE(2,257)=\cos(0.02)\approx0.99980 \]
**مقایسهٔ گام زاویه**
\[ \Delta\theta_{i=0}=1,\qquad \Delta\theta_{i=128}=\frac{1}{100} \]
مخرج بزرگتر، نوسان آهستهتر
**طول موج مشترک هر جفت**
\[ \lambda_i=2\pi\cdot10000^{2i/d_{\mathrm{model}}},\qquad \lambda_{128}=200\pi \]
**بازهٔ طول موج**
\[ 2\pi\leq\lambda_i<10000\cdot2\pi,\qquad \lambda_{255}=2\pi\cdot10000^{510/512} \]
کران بالایی حد بازه است.
دو انگیزه برای انتخاب sinusoid
برای یک جفت مؤلفه، زاویهٔ جایگاه \(pos\) را \(\theta\) و تغییر زاویه برای offset ثابت \(k\) را \(\delta\) بنامیم.
**تغییر زاویه**
\[ \theta \longmapsto \theta+\delta \]
**مؤلفهٔ sine**
\[ \sin(\theta+\delta)=\sin\theta\cos\delta+\cos\theta\sin\delta \]
**مؤلفهٔ cosine**
\[ \cos(\theta+\delta)=\cos\theta\cos\delta-\sin\theta\sin\delta \]
**تبدیل خطی برای offset ثابت**
\[ \begin{pmatrix}\sin(\theta+\delta)\\\cos(\theta+\delta)\end{pmatrix}=\begin{pmatrix}\cos\delta&\sin\delta\\-\sin\delta&\cos\delta\end{pmatrix}\begin{pmatrix}\sin\theta\\\cos\theta\end{pmatrix} \]
آسانتر شدن یادگیری توجه نسبی، فرضیه است.
**تعمیم و شواهد**
\[ \mathrm{BLEU}:\quad 25.7\quad\text{vs.}\quad 25.8 \]
تعمیم به دنبالههای بلندتر احتمالی است؛ positional embedding یادگرفتنی در برابر مدل پایه، جدول 3 ردیف E.
Back to course