You are reading immutable version 39. The current guide may be newer.

Study the paper

Attention Is All You Need

Lessons, visuals, quizzes, flashcards, and resources—organized in teaching order.

All activities

Injecting Sequence Order: Positional Encodings

Sinusoidal Positional Encoding (Even Dimensions)

Sinusoidal Positional Encoding (Even Dimensions)

Source equation

PE(pos,2i)=sin(pos/100002i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})

In non-recurrent architectures like the Transformer, sequence order is not implicitly captured by the network structure. To inject positional information, sinusoidal positional encodings are added to the input embeddings. This equation defines the encoding value for even dimensions of the positional vector.

Sources

equation

P​E(p​o​s,2​i)=s​i​n​(p​o​s/100002​i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})

PE(pos,2i)=sin(pos/100002i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})

Sources

equation

P​E(p​o​s,2​i)=s​i​n​(p​o​s/100002​i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})

The positional encoding uses a combination of sine and cosine functions of different frequencies. For any given position pospos and even dimension index 2i2i, the value is computed using a sine function whose wavelength forms a geometric progression from 2π2\pi to 100002π10000 \cdot 2\pi. This allows the model to easily learn to attend by relative positions, since for any fixed offset kk, PE(pos+k)PE_{(pos+k)} can be represented as a linear function of PE(pos)PE_{(pos)}.

Sources

equation

P​E(p​o​s,2​i)=s​i​n​(p​o​s/100002​i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
Implementation detail

Illustrative Calculation

Let's calculate the positional encoding value for:

  • Position pos=1pos = 1
  • Dimension index 2i=02i = 0 (meaning i=0i = 0)
  • Model dimension dmodel=512d_{\text{model}} = 512

Step 1: Calculate the denominator 100002i/dmodel=100000/512=100000=110000^{2i/d_{\text{model}}} = 10000^{0/512} = 10000^0 = 1

Step 2: Calculate the argument of the sine function θ=pos100002i/dmodel=11=1\theta = \frac{pos}{10000^{2i/d_{\text{model}}}} = \frac{1}{1} = 1

Step 3: Compute the sine value PE(1,0)=sin(1)0.84147PE_{(1, 0)} = \sin(1) \approx 0.84147

Sources

equation

P​E(p​o​s,2​i)=s​i​n​(p​o​s/100002​i/dmodel)\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
\displaystyle PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}})
pospos
The position of the token in the sequence · scalar
ii
The index of the dimension (where $2i$ represents even dimensions) · scalar
dmodeld_{\text{model}}
The total dimensionality of the model's hidden states · scalar
PE(pos,2i)PE_{(pos,2i)}
The positional encoding value at position $pos$ for the even dimension $2i$ · scalar